Maui/Xamarin.Forms:在所有框架和标签上应用属性

Maui/Xamarin.Forms: Apply properties on all Frames & Labels

所以我制作了以下标签和框架:

//Frames
        private readonly Frame _frame1 = new Frame
        {
            BorderColor = Colors.Black,
            Padding = new Thickness(1),
            BackgroundColor = Colors.Gray
        };
        private readonly Frame _frame2 = new Frame
        {
            BorderColor = Colors.Black,
            Padding = new Thickness(1),
            BackgroundColor = Colors.Gray
        };
        private readonly Frame _frame3 = new Frame
        {
            BorderColor = Colors.Black,
            Padding = new Thickness(1),
            BackgroundColor = Colors.Gray
        };
//Labels
        private readonly Label _label1 = new Label
        {
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
            TextColor = Colors.Black,
            Padding = 5
        };
        private readonly Label _label2 = new Label
        {
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
            TextColor = Colors.Black,
            Padding = 5
        };
        private readonly Label _label3 = new Label
        {
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
            TextColor = Colors.Black,
            Padding = 5
        };

如您所见,我在制作新标签时多次设置文本颜色、填充和背景颜色等内容。我认为这可以通过在“标准”label/frame 上定义所有这些属性来变得更漂亮(不确定正确的词是什么)。但基本上,当我使用框架或标签时,我总是希望应用这些属性。

有人知道怎么做吗?我希望你能理解我想要达到的目标哈哈,提前致谢!

隐式样式是一种方法。在您的 App.xaml 文件中:

<Application.Resources>
    <ResourceDictionary>
       <Style TargetType="Frame">
          <Setter Property="BorderColor" Value="Black" />
          <Setter Property="BackgroundColor" Value="Gray" />
          <Setter Property="Padding" Value="1" />
        </Style>
        <Style TargetType="Label">
           <Setter Property="FontSize" Value="Small" />
           <Setter Property="TextColor" Value="Black" />
           <Setter Property="Padding" Value="5" />
        </Style>
    </ResourceDictionary>
</Application.Resources>

设置它会自动将样式应用于应用程序中的所有框架和标签。

参考:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/implicit