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



我正在开发一个UI库项目。它包含将在两个不同的UWP应用程序项目中使用的一组页面。问题在于,在一个项目中,开发人员使用一个主题来修改基本控件的外观,例如文本框架或文本框的字体大小,contentControls等等等,在我的库中,我使用了漂亮的基本控件文本框,Combobox,日期选择器。它影响了我在外观上工作的UI库中的页面。我希望库中的页面看起来像是基本输入表单。我如何告诉UI库不遵循应用程序正在使用的任何主题?

如何告诉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'。

您会在主页上看到"导航"按钮,将在'application.resources'中使用样式,但是'brankpage1'上的按钮将使用其自己的'generic.xaml'文件中的样式。

最新更新