我正在尝试根据Oreilly的“开发Windows 8”一书创建一个城域应用程序。
我有以下代码,基于空白的地铁模板
Type rootType = typeof(DependencyObject);
TypeInfo rootTypeInfo = typeof(DependencyObject).GetTypeInfo();
List<Type> classes = new List<Type>();
Brush highlightBrush;
public MainPage()
{
this.InitializeComponent();
highlightBrush = this.Resources["ControlHighlightBrush"] as Brush;
最后一行抛出此异常。据我所知,它将在公共文件夹中搜索ControlHighlightBrush,但如果它不在StandardStyles.xaml文件中,它将不起作用?
我认为那会发生什么事?
mscorlib.dll中发生了'System.Runtime.InteropServices.COMException'类型的异常,但未在用户代码中处理
其他信息:错误HRESULT E_FAIL已从调用COM组件返回。
如果存在此异常的处理程序,则可以安全地继续该程序。
答案 0 :(得分:3)
默认情况下,我不认为它在资源的任何常见文件夹中查找。必须在MergedDictionary集合中的App.xaml中明确指定资源:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
<ResourceDictionary Source="Common/CustomStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Application-specific resources -->
<x:String x:Key="AppName">My App</x:String>
</ResourceDictionary>
</Application.Resources>
因此,您可以在CustomStyles.xaml等文件中添加自定义样式,并将其包含在上面。