我正在创建一个Windows Phone 8应用程序。在这个应用程序中,我在XAML文件中定义了一个ResourceDictionary,如下所示:
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="YellowGreen" />
<Setter Property="FontSize" Value="35" />
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
<Style TargetType="TextBox" x:Key="CommonSetters">
<Setter Property="Width" Value="450"/>
<Setter Property="FontSize" Value="35" />
<Setter Property="Foreground" Value="YellowGreen" />
<Setter Property="Height" Value="100"/>
<Setter Property="Background" Value="Red">
<!--<Setter.Value>
<ImageBrush ImageSource="logo.png" Opacity="0.1"/>
</Setter.Value>-->
</Setter>
</Style>
</ResourceDictionary>此ResourceDictionary在App.xaml中被引用为:
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"> <!--Application Resources-->
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:Work_Force" x:Key="LocalizedStrings"/>
<ResourceDictionary x:Key="myDict">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<Style TargetType="TextBlock" x:Key="NN">
<Setter Property="Width" Value="450"/>
<Setter Property="FontSize" Value="35" />
<Setter Property="Foreground" Value="YellowGreen" />
<Setter Property="Height" Value="100"/>
</Style>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>然后是简单的部分:
静态资源NN运行良好,但commonSetters没有工作,它在resourse.xaml中声明。
发布于 2014-01-16 08:25:41
按照您在App.xaml中定义资源的方式,您的“myDict”资源字典是Application.Resources默认字典中的嵌套字典。我不知道在XAML中如何在嵌套字典中引用资源(因为我还不需要这个;我认为FindResource(.)从代码后面可以实现这一点)。解决问题的一种方法是,直接修改Application.Resources‘默认字典,以便与'Resources’字典合并。您可以通过修改Application.Resources部分来做到这一点:
<Application.Resources>
<ResourceDictionary>
<local:LocalizedStrings xmlns:local="clr-namespace:PhoneApp1" x:Key="LocalizedStrings"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>请注意,现在您已经没有一个名为“ReosurceDictionary”的myDict了。您的“参考资料”字典将直接合并到没有键的默认字典中。然后资源将被正确地解析。
发布于 2014-01-16 09:20:08
最后,我得到了类似于aspx或html页面的解决方案,其中我们在必需的页面中添加了样式的引用。
在任何地方,它都需要在您需要的页面中添加该页的引用。
<phone:PhoneApplicationPage.Resources>
<ResourceDictionary Source="yourstylepage.xaml"/>
</phone:PhoneApplicationPage.Resources>将上面的代码放在<phone:PhoneApplicationPage>之后
https://stackoverflow.com/questions/21155746
复制相似问题