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

How to use App ResourceDictionary and MergedDictionaries in other page - 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>

在此页面中,请建议我如何在 MainPage.xaml

中使用那些字典和合并字典来控制 TextBox

编辑 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 吗?

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

<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.