none
wpf 多个窗口如何使用同一个资源字典 RRS feed

  • 问题

  • <Application x:Class="FlipCourse.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                 xmlns:converter="clr-namespace:FlipCourse.Converter"
                 StartupUri="MainWindow.xaml" 
                 DispatcherUnhandledException="Application_DispatcherUnhandledException" 
                 Startup="Application_Startup"
                 Exit="App_OnExit">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Template/ListItemTemplate.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    <Window x:Class="FlipCourse.MainWindow">
    <ListBox ItemsPanel="{DynamicResource ItemsPanelTemplate}" VerticalAlignment="Bottom">
                        <ListBoxItem Style="{StaticResource SmallCheckListboxItemStyle}" Margin="10 0"></ListBoxItem>
                        <ListBoxItem Style="{StaticResource MediumCheckListboxItemStyle}" Margin="10 0"></ListBoxItem>
                        <ListBoxItem Style="{StaticResource LargeCheckListboxItemStyle}" Margin="10 0"></ListBoxItem>
                    </ListBox></Window >
    <Window x:Class="FlipCourse.Window2">
    <ListBox ItemsPanel="{DynamicResource ItemsPanelTemplate}" VerticalAlignment="Bottom">
                        <ListBoxItem Style="{StaticResource SmallCheckListboxItemStyle}" Margin="10 0"></ListBoxItem>
                        <ListBoxItem Style="{StaticResource MediumCheckListboxItemStyle}" Margin="10 0"></ListBoxItem>
                        <ListBoxItem Style="{StaticResource LargeCheckListboxItemStyle}" Margin="10 0"></ListBoxItem>
                    </ListBox></Window>
    在window2使用运行时就会报异常中断
    2014年1月17日 6:27

答案

  • 你好,

    这样看你遇到怎样的错误信息,我仿照你的代码,在资源字典中定义了一些ItemsPanel和ListBoxItem 样式:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        >
        <Style TargetType="{x:Type ListBoxItem}" x:Key="SmallCheckListboxItemStyle">
            <Setter Property="Background" Value="LightBlue" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Cursor" Value="Hand"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type ListBoxItem}" x:Key="MediumCheckListboxItemStyle">
            <Setter Property="Background" Value="Orange" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Cursor" Value="Hand"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type ListBoxItem}" x:Key="LargeCheckListboxItemStyle">
            <Setter Property="Background" Value="LawnGreen" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Cursor" Value="Hand"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    
        <ItemsPanelTemplate x:Key="ListBoxHorizontalStackTemplate">
            <StackPanel Width="{Binding (FrameworkElement.ActualWidth),
                                      RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
                      Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ResourceDictionary>

    有两个窗口: MainWindow 和 Window1, 在App.xaml.cs中使用如下代码使程序启动时打开两个窗口:

    public partial class App : Application
    {
            protected override void OnStartup(StartupEventArgs e)
            {
                base.OnStartup(e);
    
                Window1 window = new Window1();
                window.Show();
            }
    }

    XAML:

    <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Template/Dictionary1.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
    </Window.Resources>
    <Grid>
            <ListBox ItemsPanel="{DynamicResource ListBoxHorizontalStackTemplate}"  >
                <ListBoxItem Style="{StaticResource SmallCheckListboxItemStyle}" Margin="10 0">1</ListBoxItem>
                <ListBoxItem Style="{StaticResource MediumCheckListboxItemStyle}" Margin="10 0">2</ListBoxItem>
                <ListBoxItem Style="{StaticResource LargeCheckListboxItemStyle}" Margin="10 0">3</ListBoxItem>
            </ListBox>
    </Grid>

    截图:

    所以多窗口使用同一资源字典是没有问题的


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月20日 10:00
    版主

全部回复

  • Hi skczjun,

    本论坛是Store App开发者论坛,WPF问题请移步至WPF论坛,多谢对MSDN的支持。

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年1月20日 3:42
  • 你好,

    这样看你遇到怎样的错误信息,我仿照你的代码,在资源字典中定义了一些ItemsPanel和ListBoxItem 样式:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        >
        <Style TargetType="{x:Type ListBoxItem}" x:Key="SmallCheckListboxItemStyle">
            <Setter Property="Background" Value="LightBlue" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Cursor" Value="Hand"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type ListBoxItem}" x:Key="MediumCheckListboxItemStyle">
            <Setter Property="Background" Value="Orange" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Cursor" Value="Hand"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type ListBoxItem}" x:Key="LargeCheckListboxItemStyle">
            <Setter Property="Background" Value="LawnGreen" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Cursor" Value="Hand"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    
        <ItemsPanelTemplate x:Key="ListBoxHorizontalStackTemplate">
            <StackPanel Width="{Binding (FrameworkElement.ActualWidth),
                                      RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
                      Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ResourceDictionary>

    有两个窗口: MainWindow 和 Window1, 在App.xaml.cs中使用如下代码使程序启动时打开两个窗口:

    public partial class App : Application
    {
            protected override void OnStartup(StartupEventArgs e)
            {
                base.OnStartup(e);
    
                Window1 window = new Window1();
                window.Show();
            }
    }

    XAML:

    <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Template/Dictionary1.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
    </Window.Resources>
    <Grid>
            <ListBox ItemsPanel="{DynamicResource ListBoxHorizontalStackTemplate}"  >
                <ListBoxItem Style="{StaticResource SmallCheckListboxItemStyle}" Margin="10 0">1</ListBoxItem>
                <ListBoxItem Style="{StaticResource MediumCheckListboxItemStyle}" Margin="10 0">2</ListBoxItem>
                <ListBoxItem Style="{StaticResource LargeCheckListboxItemStyle}" Margin="10 0">3</ListBoxItem>
            </ListBox>
    </Grid>

    截图:

    所以多窗口使用同一资源字典是没有问题的


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月20日 10:00
    版主