如何在其他页面中使用App ResourceDictionary和MergedDictionaries - Windows Phone 8.1

时间:2015-01-23 09:13:42

标签: c# xaml windows-phone-8.1

我有2个资源字典Dictionary1.xamlDictionary2.xaml,其格式为TextBoxTextBlock。我在App.xaml中添加了两个字典,如下所示:

<Application
    x:Class="XAMLResources.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XAMLResources">

    <Application.Resources>
        <SolidColorBrush x:Key="MyBrush" Color="Yellow" />

        <ResourceDictionary x:Key="dict" Source="Dictionary1.xaml"/>

        <ResourceDictionary x:Key="mergeDictionary">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
                <ResourceDictionary Source="Dictionary2.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </Application.Resources>

</Application>

现在请帮我在页面中使用这些资源:

<Page
    x:Class="XAMLResources.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    >
    <StackPanel Margin="20,30,0,0">
        <TextBox Margin="0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Stretch"/>
    </StackPanel>
</Page>

在此页面中,请建议我如何在TextBox

中使用这些字典并合并字典以控制MainPage.xaml

修改 Dictionary1.xaml看起来像:

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

    <Style x:Key="eText" TargetType="TextBox">
        <Setter Property="FontSize" Value="18"/>
        <Setter Property="FontFamily" Value="Segoe WP SemiLight"/>
        <Setter Property="TextWrapping" Value="Wrap"/>
        <Setter Property="Background" Value="#FAFAFA"/>
        <Setter Property="BorderBrush" Value="#D2D2D2"/>

    </Style>
</ResourceDictionary>

我可以在UserControl或任何页面中使用此eText吗?

1 个答案:

答案 0 :(得分:2)

例如,您可以在外部文件中以下列方式定义外部资源。

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <SolidColorBrush x:Key="TitleBrush" Color="DarkViolet" />
    <SolidColorBrush x:Key="ContentBrush" Color="Black" />
</ResourceDictionary>

修改

根据你我在App.xaml中定义了它们

<Application
    x:Class="App2.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2">

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

现在我在我的普通xaml页面中使用它。 TextBlock中使用的 TitleBrush 资源是相同的资源,其定义实际存在于Dictionary1.xaml中。

<Page
    x:Class="App2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ColumnDefinitions >
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
        </Grid.RowDefinitions>

        <StackPanel>
            <TextBlock FontSize="20" Foreground="{StaticResource TitleBrush}" Text="Sample text" />
        </StackPanel>
    </Grid>
</Page>

如需进一步的帮助,请参阅this link.