WP7,WP8如何设置几个ResourceDictionaries以使用自定义FontFamilies [英] WP7, WP8 How to set several ResourceDictionaries to use custom FontFamilies

本文介绍了WP7,WP8如何设置几个ResourceDictionaries以使用自定义FontFamilies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为某些控件设置自定义字体,因此,如果我在唯一的一个 ResourceDictionary中设置字体并以样式使用它,则所有效果都很好.但是这种方法对我来说不合适,因为我需要在单独的词典中包含字体声明,然后再使用它们的样式.

App.xaml中,我制作了多个资源字典

<Application ...> 
   <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/ResourceAgencyDictionary.xaml"/>
                <ResourceDictionary Source="Resources/ResourceCommonDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources> 
   ....
</Application>

ResourceAgencyDictionary.xaml中我有MyFontFamilyNormal声明

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <FontFamily x:Key="MyFontFamilyNormal">/GBAgencyCommon;component/Fonts/Fonts.zip#Gotham Book</FontFamily>
    .....    
</ResourceDictionary>

ResourceCommonDictionary.xaml中,我想使用该字体系列(MyFontFamilyNormal):

 <ResourceDictionary ...>
        <Style x:Key="MyTextBlockValueStyle" BasedOn="{StaticResource PhoneTextBlockBase}" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="{StaticResource MyFontFamilyNormal}"/>
            ....
        </Style>
 </ResourceDictionary>

项目已编译,但出现运行时错误

发生System.Windows.Markup.XamlParseException _HResult = -2146233087 _message = 无法找到名称/关键字为MyFontFamilyNormal的资源

有人知道我该如何解决吗?

解决方案

对不起,大家在Internet上搜索了很多解决方案,然后才找到它. 解决方案很简单(我尝试过,但是文件路径错误,并且没有注意到这是正确的方向)

App.xaml

<Application ...> 
   <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/ResourceCommonDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources> 
   ....
</Application>

ResourceAgencyDictionary.xaml相同

ResourceCommonDictionary.xaml

 <ResourceDictionary ...>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourceAgencyDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <Style x:Key="MyTextBlockValueStyle" BasedOn="{StaticResource PhoneTextBlockBase}" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="{StaticResource MyFontFamilyNormal}"/>
            ....
        </Style>
 </ResourceDictionary>

注意:Source="ResourceAgencyDictionary.xaml",因为此文件与ResourceCommonDictionary.xaml

位于同一文件夹中

I want to set custom fonts for some controls, so if I set a font inside the only one ResourceDictionary and use it in styles then everything works just fine. But this approach is not fine for me, because I need to have font declarations in a separate dictionary then styles that are using them.

In App.xaml I have made several Resource Dictionaries

<Application ...> 
   <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/ResourceAgencyDictionary.xaml"/>
                <ResourceDictionary Source="Resources/ResourceCommonDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources> 
   ....
</Application>

In ResourceAgencyDictionary.xaml I have MyFontFamilyNormal declaration

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <FontFamily x:Key="MyFontFamilyNormal">/GBAgencyCommon;component/Fonts/Fonts.zip#Gotham Book</FontFamily>
    .....    
</ResourceDictionary>

In ResourceCommonDictionary.xaml I want to use that font family (MyFontFamilyNormal):

 <ResourceDictionary ...>
        <Style x:Key="MyTextBlockValueStyle" BasedOn="{StaticResource PhoneTextBlockBase}" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="{StaticResource MyFontFamilyNormal}"/>
            ....
        </Style>
 </ResourceDictionary>

The project compiles but I get a runtime error

System.Windows.Markup.XamlParseException occurred _HResult=-2146233087 _message=Cannot find a Resource with the Name/Key MyFontFamilyNormal

Does anyone know how can I fix that?

解决方案

Sorry guys, searched the solution a lot in Internet and just found it. The solution was simple (I've tried it but with wrong path to file and didn't notice that it was the right direction)

so in App.xaml

<Application ...> 
   <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/ResourceCommonDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources> 
   ....
</Application>

the ResourceAgencyDictionary.xaml is the same

in ResourceCommonDictionary.xaml

 <ResourceDictionary ...>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourceAgencyDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <Style x:Key="MyTextBlockValueStyle" BasedOn="{StaticResource PhoneTextBlockBase}" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="{StaticResource MyFontFamilyNormal}"/>
            ....
        </Style>
 </ResourceDictionary>

NOTE: Source="ResourceAgencyDictionary.xaml" because this file is in the same folder as ResourceCommonDictionary.xaml

这篇关于WP7,WP8如何设置几个ResourceDictionaries以使用自定义FontFamilies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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