找不到名称/键为的资源 [英] Cannot find a Resource with the Name/Key

查看:13
本文介绍了找不到名称/键为的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Silverlight 4工具包对用户界面进行单元测试。

当我尝试实例化UserControl时,它引发异常,因为在UserControl的XAML中,它使用定义的App.xaml样式。

有没有办法在实例化UserControl之前以某种方式加载资源?我是不是走错了路?

以下是单元测试代码:

        [TestMethod]
    public void ExerciseTimePeriodUserInterface()
    {
        CustomUserControls.TimePeriodFilter timePeriodFilter = new CustomUserControls.TimePeriodFilter();
    }

下面是对UserControl中的样式的引用:

<Border Style="{StaticResource FilterBorderWrapper}">

最后,下面是App.xaml:

中定义的样式
    <Style TargetType="Border" x:Key="FilterBorderWrapper">
        <Setter Property="Background" Value="#F1F5FB" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="BorderBrush" Value="#CBD9E9" />
        <Setter Property="CornerRadius" Value="2" />
        <Setter Property="Margin" Value="2" />
    </Style>

推荐答案

如果您的所有资源都放入资源词典中。您可以简单地创建应用程序实例并将该字典添加到资源中。请看样品:

Application _app = new Application();
ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Source = new Uri("pack://application:,,,/Gui.Mvvm;component/Themes/YourResourceDictionary.xaml");
_app.Resources.MergedDictionaries.Add(dictionary);

对于我的WPF应用程序,这工作得很好。在编写了这段代码之后,我能够测试我的模板选择器、数据模板选择器等等。在代码隐藏调用中使用的所有代码

Application.Current.FindResource()

效果很好。

这篇关于找不到名称/键为的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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