以编程方式访问 ResourceDictionary 项目 [英] Access ResourceDictionary items programmatically

查看:20
本文介绍了以编程方式访问 ResourceDictionary 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为MySilverlightControls"的 Silverlight 控件程序集.在该程序集中的几个文件夹中,我有一个类从第三方供应商那里扩展了网格列,我们称之为MyImageColumn.cs".

I have a Silverlight controls assembly, called "MySilverlightControls". Several folders down into that assembly I have a class which extends a grid column from a third party vendor, let's call it "MyImageColumn.cs".

我还创建了一个名为 Generic.xaml 的资源字典,它位于程序集的 Themes 文件夹中.在该资源字典中,我定义了一个名为 MyImageColumnTemplate:

I have also created a resource dictionary called Generic.xaml, this is situated in the Themes folder of the assembly. In that resource dictionary i have defined a ControlTemplate called MyImageColumnTemplate:

<ControlTemplate x:Name="MyImageColumnTemplate" >
    <Grid Margin="8,8,4,4" MaxHeight="32" MaxWidth="32">
        <Grid.Resources>
            <localGrid:StatusColumnImageConverter x:Key="ImageContentConverter"/>
        </Grid.Resources>
        <Border Margin="5,5,0,0" Background="Black" Opacity="0.15" CornerRadius="5" />
        <Border Background="#FF6E6E6E" CornerRadius="4,4,4,4" Padding="4" Margin="0,0,5,5">
            <Border Background="White" CornerRadius="2,2,2,2" Padding="3">
                <Image Source="{Binding EditValue, Converter={StaticResource ImageContentConverter}}" Stretch="Uniform"/>
            </Border>
        </Border>
    </Grid>
</ControlTemplate>

我的问题是:从 MyImageColumn,我如何以编程方式引用/加载此控件模板,以便将其分配给列上的属性?我希望使用与此类似的语法:

My question is: from MyImageColumn, how can I programmatically reference/load this control template so I can assign it to a property on the column? I would expect to be using a syntax similar to this:

ControlTemplate ct = (ControlTemplate)Application.Current.Resources["MyImageColumnTemplate"];

但这总是返回空值.当我在 Reflector 中加载程序集时,我看到 Generic.xaml 文件在那里,资源的名称是 MySilverlightControls.g.resources,以及其中的路径即 themes/generic.xaml.

but this always returns null. When I load the assembly up in Reflector, I see that the Generic.xaml file is there, the name of the resource is MySilverlightControls.g.resources, and the path within that is themes/generic.xaml.

我如何才能准确地访问此资源字典中的各个项目?

How exactly can I get to the individual items in this resource dictionary?

推荐答案

解决了.

我需要:

  • 加载我的资源字典
  • 将其与应用程序的资源合并
  • 从应用程序资源加载我的控件模板

作为加载资源字典的一部分,我还必须注册pack URI 方案.然后,由于我的 xaml 的轻微错误,我不得不处理一些基于 COM 的疯狂异常.我还必须将我的 xaml 移动到一个单独的资源字典文件中,尝试通过 generic.xaml 执行此操作时不断抛出错误(即使 xaml 是完美无缺的并且可以使用新创建的资源字典文件正常加载).所以,简化一下,这是代码:

As part of loading the resource dictionary, i also had to register the pack URI scheme. I then had to deal with some crazy COM based exceptions due to slight errors with my xaml. I also had to move my xaml into a separate resource dictionary file, trying to do it through generic.xaml kept throwing errors (even though the xaml was faultless and could be loaded fine using the newly created resource dictionary file). So, simplifying it down, this was the code:

if (!UriParser.IsKnownScheme("pack"))
    UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1);

ResourceDictionary dict = new ResourceDictionary();
Uri uri = new Uri("/MySilverlightControls;component/themes/Dictionary1.xaml", UriKind.Relative);
dict.Source = uri;
Application.Current.Resources.MergedDictionaries.Add(dict);
ControlTemplate ct = (ControlTemplate)Application.Current.Resources["MyImageColumnTemplate"];

我已在此博客中发布了此解决方案的完整详细信息发布.

这篇关于以编程方式访问 ResourceDictionary 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆