当Application.Resource影响库控件的外观时,如何管理UI库的应用程序资源?

时间:2019-06-28 18:29:26

标签: windows xaml uwp resources

我正在开发一个UI库项目。它包含将在两个不同的UWP app项目中使用的页面集。问题在于,在一个项目中,开发人员使用的主题可以修改基本控件的外观,例如文本块或TextBox的字体大小,ContentControls的边距等。在我的库中,我使用了非常漂亮的基本控件文本框,组合框,日期选择器。它会影响我正在处理的UI库中页面的外观。我希望图书馆中的页面看起来像一个基本的输入表单。如何告诉UI库不要遵循应用程序正在使用的任何主题?

1 个答案:

答案 0 :(得分:1)

  

如何告诉UI库不要遵循应用程序正在使用的主题?

您可以在UI库项目中添加“资源字典”文件。在“ dictionary.xaml”文件中,您可以为UI库页面中使用的控件定义基本样式。这样,控件将不受主项目中资源样式的影响。

例如,我在UI库项目中添加了一个“ generic.xaml”文件,并为按钮定义了一种样式,如下所示:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ClassLibraryUI">
    <Style TargetType="Button" BasedOn="{StaticResource ButtonRevealStyle}">
    </Style>
</ResourceDictionary>

我在UI库中有一个“ BlankPage1.xaml”。

<Page
x:Class="ClassLibraryUI.BlankPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ClassLibraryUI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="generic.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>
<Grid>
    <Button Content="Hello UWP"></Button>
</Grid>
</Page>

在主项目中,我为“ App.xaml”中的按钮定义了一种样式:

<Application
x:Class="AppUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppUI">
<Application.Resources>
    <Style TargetType="Button">
        <Setter Property="Foreground" Value="Red"></Setter>
        <Setter Property="Background" Value="LightBlue"></Setter>
        <Setter Property="Width" Value="200"></Setter>
        <Setter Property="Height" Value="50"></Setter>
    </Style>
</Application.Resources>
</Application>

在“ MainPage.xaml”上:

<StackPanel>
    <Button Content="Navigate" Click="Button_Click"></Button>
    <Frame x:Name="frame"></Frame>
</StackPanel>

在“ MainPage.xaml.cs”中,我使用Frame控件导航到UI库中的“ BalnkPage1”。

您会看到MainPage上的“导航”按钮将使用“ Application.Resources”中的样式,但是“ BlankPage1”上的按钮将在其自己的“ generic.xaml”文件中使用该样式。