繁体   English   中英

ContentPage中用于背景的Xamarin Xaml样式

[英]Xamarin Xaml Styles for background in ContentPage

Xamarin中的新功能,似乎如果我尝试为ContentPage设置样式,则什么也没有发生

App.xaml的ResourceDictionary中的Xaml:

<Style TargetType="ContentPage">
    <Setter Property="BackgroundColor" Value="#f8f8f8"/>
</Style>

我知道正在读取app.xaml样式。 我有一个已在全球范围内应用的按钮样式。 但是我似乎无法影响ContentPage上的任何更改。 没有错误报告。

如何全局设置背景色?

我已经读到这可能是一个错误,但是那是一年前的事。 如果仍然是错误,是否有解决方法?

通过@Tomasz Kowalczyk的技巧,使用这篇不太完整的帖子中的示例xaml:

在app.xaml ResourceDictionary中输入以下样式:

<Style x:Key="defaultPageStyle" TargetType="ContentPage">
   <Setter Property="BackgroundColor" Value="#f8f8f8"/>
</Style>

该基类称为BaseContentPage

public class BaseContentPage : ContentPage
{
    public BaseContentPage()
    {
        var style = (Style)Application.Current.Resources["defaultPageStyle"];
        Style = style;
    }
}

然后在每个xaml.cs类中将它们捆绑在一起:

namespace MyNamespace
{
    public partial class MyXamlPage: BaseContentPage

和.xaml文件

<local:BaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:MyNamespace;assembly=MyNamespace"
         x:Class="MyNamespace.MyXamlPage"

App.xaml以下代码对我App.xaml

<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
   <Setter Property="BackgroundColor" Value="Lime" />
</Style>

注意:

  • 您不应在Style使用属性x:Key
  • 您应该添加ApplyToDerivedTypes="True"

我在https://putridparrot.com/blog/styles-in-xamarin-forms/找到了解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM