我有一个Silverlight控制组件,称为"MySilverlightControls".几个文件夹下进入大会上,我有一个类延伸的一个网格列从第三方供应商,让我们叫它"MyImageColumn.cs"。

我们还创建了一个资源的字典所谓 Generic.xaml, 这是酒店的 Themes 文件夹的大会。在这一资源的字典,我已经定义了一个称为控件 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,我怎么可以编程参考载这种控制模板所以我可以把它分配给财产上的柱?我希望被使用的语法类似于这样的:

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

但是,这总是返回空。当我加载的大会在反,我看到的 Generic.xaml 文件是存在的,资源的名称是 MySilverlightControls.g.resources, 和内路径, themes/generic.xaml.

究竟如何,我可以得到的个别项目,在这一资源的字典?

有帮助吗?

解决方案

把它解决。

我需要:

  • 载我的资源的字典
  • 合并的应用程序的资源
  • 载我的控制模板应用资源

作为一部分载入的资源的字典,我也必须登记的 pack URI方案。然后我不得不处理一些疯狂的COM基于例外情况,因为轻微的错,我的样式我还有我的署的一个单独的资源的字典的文件,试图这样做,通过通用的。xaml保持投掷的错误(即使xaml是完美无缺的,并可能装载的现使用新创建的资源的字典文件)。因此,简化了下来,这是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"];

我已经发布的全部细节,对于这种解决方案 这篇文章.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top