不同程序集中的应用程序级资源

Ton*_*res 5 wpf resources xaml app.xaml

如果App.xaml与视图位于一个单独的程序集中,则此问题涉及Visual Studio(2008)WPF Designer显然无法处理位于App.xaml级别的资源的使用.

为了简化问题的解释,我创建了一个测试应用程序.此应用程序有两个程序集:View和Start.View程序集包含一个名为Window1的xaml窗口,Start程序集包含App.xaml文件.Start程序集中的App.xaml文件将其StartupUri设置为View程序集中的Window1.这些文件都没有代码隐藏(除了标准构造函数和InitializeComponent()调用).

此示例的代码如下:

App.xaml中:

<Application x:Class="Start.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="pack://application:,,,/View;component/Window1.xaml"
         >
<Application.Resources> 

    <!-- Warning Text Style -->
    <Style x:Key="WarningTextStyle" TargetType="TextBlock">
        <Setter Property="FontWeight" Value="Bold" />
    </Style>

</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

Window1.xaml:

<Window x:Class="View.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" 
    Height="300" 
    Width="300"
    >

    <Grid>
        <TextBlock Text="This is test text" Style="{StaticResource WarningTextStyle}" />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

Window1.xaml文件包含一个引用应用程序级WarningTextStyle的TextBlock.此代码在运行时正常工作,因为Window正确查找了App级资源; 然而,在设计时,设计师抱怨它无法找到WarningTextStyle.

有没有人知道这个问题的清洁和可扩展的解决方案?

我对大型应用程序的标准方法是将我的应用级资源组织到资源字典文件中,然后在App.xaml中合并这些字典.要解决我上面描述的问题,我必须将这些资源字典合并到每个视图的资源中.这似乎非常低效,如果我以后添加另一个资源字典,那么我需要将新字典合并到每个视图中.

一个银弹解决方案将重新指导设计师找到应用级资源.合理的解决方法是将应用级资源字典合并到每个视图中,但仅限于设计时.在运行时,由于效率问题,我希望避免在每个视图中合并这些字典.

我尝试在视图的代码隐藏构造函数中合并每个视图上的字典,然后将该逻辑包装在检查DesignerProperties.GetIsInDesignMode()方法的if语句中; 但是,Visual Studio设计器不运行视图的构造函数 - 所以这种方法似乎是一个破产.

有没有人有更好的解决方案或解决方案?

Rob*_*sor 2

您能否将主(exe)程序集的 App.xaml 中的资源字典合并到引用的程序集中(无论是 App.xaml 还是您自己的资源字典)?