首页 文章

Xamarin Xaml样式作为ContentPage中的背景

提问于
浏览
0

最近在Xamarin工作,似乎如果我试图为_2836134设置一个样式,没有任何反应

App.xaml的ResourceDictionary中的Xaml:

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

我知道正在阅读app.xaml样式 . 我有一个全局应用的按钮样式 . 但我似乎无法影响 ContentPage 的任何变化 . 没有报告错误 .

我如何全局设置backgroundcolor?

我读过这可能是一个错误,但那是一年前的事 . 如果它仍然是一个bug是有解决方法吗?

1 回答

  • 1

    在@Tomasz Kowalczyk的提示下使用示例xaml from this rather incomplete post

    在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"
    

相关问题