以编程方式访问 ResourceDictionary 项目

发布于 2024-08-17 10:19:26 字数 1433 浏览 3 评论 0原文

我有一个 Silverlight 控件程序集,名为“MySilverlightControls”。在该程序集中的几个文件夹中,我有一个类,它扩展了来自第三方供应商的网格列,我们将其称为“MyImageColumn.cs”。

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

<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,我如何以编程方式引用/加载此控件模板,以便将其分配给列上的属性?我希望使用与此类似的语法:

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

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

我究竟如何才能获取此资源字典中的各个项目?

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".

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>

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"];

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-08-24 10:19:26

已经解决了

我需要:

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

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

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"];

我已在 这篇博文

Got it solved.

I needed to:

  • load my resource dictionary
  • merge it with the application's resources
  • load my control template from the application resource

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"];

I have posted the full details for this solution in this blog post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文