如何组织应用程序范围的资源?

时间:2013-03-13 09:50:14

标签: wpf visual-studio-2012 styles resourcedictionary

以下是该方案:

我一直在使用VS 2010使用MEF编写相当大的WPF项目。这个解决方案中有多个项目,我的所有样式都有一个ResourceDictionary

AppStyles.xaml ResourceDictionary位于我的启动项目中,它包含了我项目的所有样式。在我的App.xaml中,我有以下内容:

<Application x:Class="Dionysus.Shell.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         DispatcherUnhandledException="UnhandledException" ShutdownMode="OnMainWindowClose">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>

            <ResourceDictionary Source="AppStyles.xaml"/>

    </ResourceDictionary>
</Application.Resources>

现在,这一切都完美地适用于VS 2010的设计模式,当我调试时,正确的样式将应用于我的所有控件。但我最近转向VS 2012,设计师不再工作了!当我调试时,它完美无缺,但每当我使用我的一种样式时如下:

<Button Name="btnForward" Height="23" Margin="10,0" Style="{StaticResource DionysusButton}" Grid.Row="2" Grid.Column="0"/>

我在VS 2012设计器中遇到以下错误:

The resource "DionysusButton" could not be resolved.

我对于为什么会发生这种情况一无所知,我已将我的Style重命名,甚至ResourceDictionary也无效。

我在VS 2012中应该采用不同的方式吗?或者这是VS 2012的问题吗?

我发现很多帖子都说我将x:Name AppStyles.xaml的{​​{1}}属性更改为ResourceDictionary会解决此问题,但我不能这样做不是x:Key的有效选项。

提前完成

- 更新

我正在使用Marc的答案,除了默认样式外,一切似乎都很顺利。我有以下风格:

ResourceDictionary

这是我的主要 <Style TargetType="DataGrid" x:Name="DataGridStyle"/>

ResourceDictionary

如果我没有在我的主<ResourceDictionary x:Name="Main" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Styles/AppStyling.xaml" /> </ResourceDictionary.MergedDictionaries> <Style TargetType="DataGrid" BasedOn="{StaticResource DataGridStyle}"/> 中添加Style标记,则它可以正常工作,但该样式并未明显应用于所有项目。当我添加它时,我得到以下异常:

  

为'System.Windows.Markup.StaticResourceHolder'提供一个异常

想法?

1 个答案:

答案 0 :(得分:2)

您是否尝试使用PRISM中导出到shell中的模块中app.xaml中的样式?模块不应该引用您的应用程序/ shell项目(启动项目)?然后他们可能只是不“知道”包含样式的项目?虽然问题是为什么它在VS2010中起作用然后...请让我知道,如果我误解了你的MEF场景,我会调整我的答案。

资源和棱镜非常棘手,而且我喜欢以下方法,我之前发布过,但它也可能对你的情况有所帮助。对于PRISM解决方案而言,比将资源放在app.xaml中更合适:

我通常创建一个单独的样式项目,我从项目中引用,我想要样式。样式项目有一个固定的结构:

Styling project

对于每个控件,我都会创建一个样式ResourceDictionary。例如我的按钮:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="PrimaryButtonStyle" TargetType="Button">
    </Style>

    <Style x:Key="ToolbarButton" TargetType="Button">
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Margin" Value="3"/>
        <Setter Property="Background" Value="Transparent"></Setter>
    </Style>
</ResourceDictionary>

在一个主ResourceDictionary中,我合并了所有其他词典,在本例中是IncaDesign.xaml文件,您可以在上图中看到:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:Commons.Controls;assembly=Commons">

    <ResourceDictionary.MergedDictionaries>

        <ResourceDictionary Source="Converter/Converter.xaml" />
        <ResourceDictionary Source="Styles/Button.xaml" />
        <ResourceDictionary Source="BitmapGraphics/Icons.xaml" />

    </ResourceDictionary.MergedDictionaries>

    <!-- Default Styles -->
    <Style TargetType="Button" BasedOn="{StaticResource PrimaryButtonStyle}"></Style>
</ResourceDictionary>

注意我如何定义自动应用的默认样式,除非您另行指定。在每个窗口或控件中,您想要设置样式,您只需要引用这个ResourceDictionary。请注意源的定义,它是对程序集(/Commons.Styling;component...

的引用
<UserControl.Resources>        
    <ResourceDictionary>            
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Commons.Styling;component/IncaDesign.xaml" />
        </ResourceDictionary.MergedDictionaries>  
        <!-- Here you can added resources which belong to the UserControl only -->
        <!-- For example: -->
        <DataTemplate x:Kex="UcTemplate" /> 
    </ResourceDictionary>        
</UserControl.Resources>

现在将自动设置默认样式,如果要显式访问资源,可以使用StaticResource执行此操作。

<Viewbox Height="16" Width="16" Margin="0,0,10,0">
    <ContentControl Content="{StaticResource FileIcon32}" />
</Viewbox>

在我看来,这是一个非常好的解决方案,适用于非常复杂的解决方案,包括模块化解决方案,例如使用PRISM构建。