我有2个资源字典Dictionary1.xaml
和Dictionary2.xaml
,其格式为TextBox
和TextBlock
。我在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
吗?
答案 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.