微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight – 以编程方式访问ResourceDictionary项目

我有一个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" Background="Black" Opacity="0.15" CornerRadius="5" />
        <Border Background="#FF6E6E6E" CornerRadius="4,4" Padding="4" Margin="0,5">
            <Border Background="White" CornerRadius="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。

我如何获得这个资源字典中的各个项目?

解决方法

得到解决

我需要:

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

作为加载资源字典的一部分,我还必须注册包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"];

我已经在this blog post发布了该解决方案的完整详细信息。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐