Xamarin.Forms:如何从另一个文件加载ResourceDictionary?

P3P*_*PPP 12 c# xaml xamarin xamarin.forms

我写了下面的代码,但是XamlParseException抛出了bean.("找不到关键CustomColor的StaticResource")

MyPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XFApp11.MyPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="CustomResource.xaml" />
            </ResourceDictionary.MergedDictionaries> 
        </ResourceDictionary>
    </ContentPage.Resources>

    <ContentPage.Content>
        <BoxView Color="{StaticResource CustomColor}" />
    </ContentPage.Content>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

CustomResource.xaml(build action = EmbeddedResource)

<?xml version="1.0" encoding="UTF-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <Color x:Key="CustomColor">#004B86</Color>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

Ahm*_*ejo 14

从2.3.0可以正式合并xaml中的资源字典观察以下示例

BlueTheme.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    x:Class="UI.Themes.BlueTheme">
    <Style TargetType="Label">
        <Setter Property="TextColor" Value="Blue" />
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

App.xaml中

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:theme="clr-namespace:UI.Themes"
             x:Class="UI.App">
    <Application.Resources>
        <ResourceDictionary MergedWith="themes:BlueTheme" />
    </Application.Resources>
    <Label Text="Hello" />
</Application>
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记在UI.mes.BlueTheme类的构造函数中调用InitializeComponent().在这里找到了工作样本 - https://xamarinhelp.com/merged-dictionaries-xamarin-forms/ (3认同)
  • 这应该从Xaml中清楚:***ResourceDictionary***如果它有不同的基类我需要写类似***本地的东西:ResourceDictionary*** (2认同)

Ada*_*ley 9

Xamarin Forms XAML 2.1.0以下版本不支持Merged Dictionaries

你可以做到的唯一方法是将它放在另一个页面中,然后在后面的代码中加载它并通过DynamicResource而不是StaticResource引用它.

我在这里解释一下:http://www.xamarinhelp.com/styling-uiux-day-11/

但是从2.1.0-pre1(本周发布)开始,你现在可以进行模板化,这是另一种方法.杰森史密斯在博客上写了这篇文章:http://xfcomplete.net/general/2016/01/20/control-templates/

更新:从2.3.0开始,您可以使用名为MergedWith的属性执行合并字典.

创建一个新的XAML文件

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    x:Class="UIDemo.Style.NewStyle">
    <Style TargetType="Label">
        <Setter Property="TextColor" Value="Blue" />
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

现在在您的ContentPage中使用ResourceDictionary上的MergedWith属性.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:theme="clr-namespace:UIDemo.Style"
             x:Class="UIDemo.MainPage">
    <ContentPage.Resources>
        <ResourceDictionary MergedWith="theme:NewStyle" />
    </ContentPage.Resources>
    <Grid>
        <Label Text="Hello" />
    </Grid>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)