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

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

问题描述

我有一个Silverlight控件程序集,称为"MySilverlightControls".到该程序集中的几个文件夹中,我有一个类,它从第三方供应商处扩展了网格列,我们将其称为"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"];

,但这始终返回null.在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天全站免登陆