【问题标题】:How can I access a dynamic resource from a different assembly inside a ViewBase如何从 ViewBase 中的不同程序集访问动态资源
【发布时间】:2012-12-13 06:53:36
【问题描述】:

我正在使用一个 Xaml 文件,该文件是从 ViewBase 派生的自定义视图,并且我想访问不同程序集中的 DynamicResource。我已经看到可以使用类似的东西:

<Application.Resources>
    <ResourceDictionary
    Source="/mylib;Resources/MyStyleDictionary.xaml" />
</Application.Resources>

但是我正在处理一个看起来像这样的 xaml 文件:

<myLib:ViewBase> 
    <Grid>
        <Button>
            Style="{DynamicResource MyButtonStyle}" // I want this style to come from a different assembly
        </Button>
    </Grid>
 </myLib:ViewBase>

我该怎么做?

【问题讨论】:

    标签: wpf xaml resources .net-assembly dynamicresource


    【解决方案1】:

    了解动态资源和静态资源之间的区别很重要。 What's the difference between StaticResource and DynamicResource in WPF?

    但是要回答这个问题:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/SomeOtherAssembly;Resources/SomeOtherDictionaryWithMyButtonStyleKey.xaml" />
                <ResourceDictionary Source="/mylib;Resources/MyStyleDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    资源被动态引用,因此在合并 MyStyleDictionary.xaml 之前合并 SomeOtherDictionaryWithMyButtonStyleKey.xaml 应该可以工作。

    【讨论】:

    • 谢谢,但是这个 Application.Resources 应该放在哪里呢?我正在使用 ViewBase xaml,我在这个项目中没有 App.xaml
    • 在任何资源级别都有意义。 Resources 是 FrameworkElement 上的一个属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多