【问题标题】:Xamarin Xaml Styles for background in ContentPageContentPage 中背景的 Xamarin Xaml 样式
【发布时间】:2015-11-19 17:55:06
【问题描述】:

新在 Xamarin 中工作,如果我尝试为 ContentPage 设置样式,似乎没有任何反应

App.xaml 的 ResourceDictionary 中的 Xaml:

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

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

如何全局设置背景色?

我听说这可能是一个错误,但那是一年前的事了。如果它仍然是一个错误,是否有解决方法?

【问题讨论】:

  • 请阅读这篇文章,它可能很有用,有一个与此相关的错误和一些解决方法forums.xamarin.com/discussion/34798/…
  • 我已经尝试过了,但很难弄清楚我主页中的 xaml 是什么样子的
  • @TomaszKowalczyk,感谢您的帮助。你为我指明了正确的道路。
  • 超级所以也许投票支持我的评论? ;)
  • @TomaszKowalczyk,我会做得更好,因为评论点赞无济于事。

标签: c# xaml xamarin


【解决方案1】:

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/找到了解决方案

【讨论】:

    【解决方案2】:

    按照@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"
    

    【讨论】:

    • 在 C# 中会是什么样子?
    • @tmighty,我不明白。此解决方案在 c# 和 xaml 中都有。
    猜你喜欢
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2021-01-24
    • 2016-09-01
    • 2017-02-28
    相关资源
    最近更新 更多