XAML 如何在winUI包中使用默认样式

bt1cpqcv  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(156)

有一个ResourceDictionary:

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

    <Style TargetType="Button">

        <Setter Property="VerticalAlignment"
                Value="Stretch" />
        <Setter Property="Background"
                Value="RoyalBlue" />
    </Style>
</ResourceDictionary>

我想用它作为defualt按钮控件。
App.Xaml包含dictionarydictionary:

<Application x:Class="EroMangaManager.UWP.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:muxc="using:Microsoft.UI.Xaml.Controls">
    <Application.Resources>
        <muxc:XamlControlsResources>
            <muxc:XamlControlsResources.MergedDictionaries>
                <ResourceDictionary Source="ResourceDictionarys/DefaultControlStyle.xaml" />
            </muxc:XamlControlsResources.MergedDictionaries>
        </muxc:XamlControlsResources>
    </Application.Resources>
</Application>

旧的UI是原始的UWP UI(不是WinUI包),默认的按钮控件可以正常工作。
但是如果我将UI更改为WinUI 2.8.2包,默认的按钮控件就不能工作了。
我尝试在App.xaml中定义样式,而不是在外部的xaml文件中:

<Application x:Class="EroMangaManager.UWP.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:muxc="using:Microsoft.UI.Xaml.Controls">
    <Application.Resources>
        <Style TargetType="Button">
            <Setter Property="Background"
                    Value="RoyalBlue" />
        </Style>
        <muxc:XamlControlsResources x:Key="de">
            <muxc:XamlControlsResources.MergedDictionaries>
                <ResourceDictionary Source="ResourceDictionarys/DefaultControlStyle.xaml" />
            </muxc:XamlControlsResources.MergedDictionaries>
        </muxc:XamlControlsResources>
    </Application.Resources>
</Application>

编译是可以的,但是当运行到Page.Construct函数时,代码this.InitializeComponent();将抛出异常。
如何正确使用WinUI包的默认值?

fcipmucu

fcipmucu1#

我测试了代码,这是一个意外的行为。目前,您可以尝试以下两种解决方案。
1.使用ResourceDictionary(DefaultControlStyle.xaml)中的新ResourceKey。

<SolidColorBrush x:Key="NewButtonBackgroundBrush" Color="HotPink" />

<!-- Resources for Windows.UI.Xaml.Controls.Button -->
<!-- I copy this form generic.xaml  "C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.22621.0\Generic\generic.xaml" -->
<StaticResource x:Key="ButtonBackground" ResourceKey="NewButtonBackgroundBrush" />
<StaticResource x:Key="ButtonBackgroundPointerOver" ResourceKey="SystemControlBackgroundBaseLowBrush" />
<StaticResource x:Key="ButtonBackgroundPressed" ResourceKey="SystemControlBackgroundBaseMediumLowBrush" />
<StaticResource x:Key="ButtonBackgroundDisabled" ResourceKey="SystemControlBackgroundBaseLowBrush" />
<StaticResource x:Key="ButtonForeground" ResourceKey="SystemControlForegroundBaseHighBrush" />
<StaticResource x:Key="ButtonForegroundPointerOver" ResourceKey="SystemControlHighlightBaseHighBrush" />
<StaticResource x:Key="ButtonForegroundPressed" ResourceKey="SystemControlHighlightBaseHighBrush" />
<StaticResource x:Key="ButtonForegroundDisabled" ResourceKey="SystemControlDisabledBaseMediumLowBrush" />
<StaticResource x:Key="ButtonBorderBrush" ResourceKey="SystemControlForegroundTransparentBrush" />
<StaticResource x:Key="ButtonBorderBrushPointerOver" ResourceKey="SystemControlHighlightBaseMediumLowBrush" />
<StaticResource x:Key="ButtonBorderBrushPressed" ResourceKey="SystemControlHighlightTransparentBrush" />
<StaticResource x:Key="ButtonBorderBrushDisabled" ResourceKey="SystemControlDisabledTransparentBrush" />
<StaticResource x:Key="NewButtonBorderThicknessValue" ResourceKey="NewButtonBorderThickness" />

<Style TargetType="Button" x:Key="NewBtnStyle">
    <Setter Property="VerticalAlignment"
            Value="Stretch" />
    <Setter Property="Background" Value="{StaticResource ButtonBackground}"/>
</Style>

2.定义每个页面资源中按钮的样式。

<Page.Resources>
    <ResourceDictionary Source="ms-appx:///DefaultControlStyle.xaml"/>
</Page.Resources>

相关问题