C# VS2012《资源》;X";无法解决
我的项目中有一个Resources.xaml文件,其中包含一个资源字典,如下所示:C# VS2012《资源》;X";无法解决,c#,xaml,microsoft-metro,windows-phone,resourcedictionary,C#,Xaml,Microsoft Metro,Windows Phone,Resourcedictionary,我的项目中有一个Resources.xaml文件,其中包含一个资源字典,如下所示: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key="GPHeaderFontSize"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="GPHeaderFontSize" TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="24" />
<Setter Property="Text" Value="BLAHHHHH"/>
</Style>
</ResourceDictionary>
我在App.xaml中包含了这本词典,如下所示:
<Application x:Class="GoldenPlains.App"
xmlns="schemas.microsoft.com/winfx/2006/xaml/presentation";
xmlns:x="schemas.microsoft.com/winfx/2006/xaml";
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:GoldenPlains" x:Key="LocalizedStrings"/>
<ResourceDictionary x:Key="GPResources">
<ResourceDictionary.MergedDictionaries>
<!-- Sometimes VS2012 complaining about path with blue line, please ignore it as path is correct -->
<ResourceDictionary Source="Styles/GPResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="GPRootOverlayBarStyle" TargetType="Image">
<Setter Property="Source" Value="Assets/Images/root_brown_horizontal_bar.png"/>
<Setter Property="Width" Value="729"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Stretch" Value="Uniform"/>
</style>
</Application.Resources>
...
...
</Application>
...
...
但是,当我尝试从另一个Page.xaml文件引用资源字典中的元素时,它似乎无法解析资源。。。。
如:
我尝试过使用这样的绑定:
<TextBlock Style="{Binding Path=LocalizedResources.MyTextBlockStyle, Source= {StaticResource GPResources}}"/>
它并不表示有问题,但UI上没有显示任何内容
如果方向正确,那就太好了,干杯。App.xaml中的资源字典定义应该与以下示例类似:
<Application.Resources>
<ResourceDictionary>
<local:LocalizedStrings xmlns:local="clr-namespace:GoldenPlains" x:Key="LocalizedStrings"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/GPResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Other resources if you have -->
</ResourceDictionary>
</Application.Resources>
然后,当您需要将Resources.xaml中定义的样式应用于UI控件时,只需参考样式的键/名称:
<TextBlock Style="{StaticResource GPHeaderFontSize}" />
注意:所有资源都需要位于
ResourceDictionary
标记内,包括LocalizedStrings
嗨,我曾尝试在应用程序资源节点中添加资源字典,就像几天前在这里显示的那样,但VS2012迫使我为顶级资源字典节点指定一个键,我注意到你的例子省略了。其他我已经在做的事情。CheesMind发布你的App.xaml?我在VS2012 Windows Phone 8项目中也尝试了这些代码。一切正常,就这样?我知道这一部分,因为你张贴了它的问题。我的意思是,如果你在App.xaml中有更多内容,请将其包含在问题中。若你们并没有你们已经发布的内容以外的内容,那个么请尝试改变它们,就像我在回答中所展示的那个样。我相信它应该会起作用。这里只有标准的本地化字符串名称空间声明和我定义的一些自定义样式,我现在没有代码,我会尝试一下,然后再给你回复。干杯