【问题标题】:How to create global style for Xamarin forms application如何为 Xamarin 表单应用程序创建全局样式
【发布时间】:2016-09-08 04:02:06
【问题描述】:

我正在使用带有 Xamarin Studio 的 Mac 机器上的 PCL 创建我的第一个 xamarin 表单应用程序。 我阅读了很多文章来为应用程序创建全局样式,但我无法访问在 App.xaml.cs 文件中声明的应用程序内的任何文件的样式。我在下面提到了我使用的一些代码。或者,如果有人有其他选择可以让它变得容易或无论如何可能,请建议我。提前致谢。

App.xaml.cs-

<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DinePerfect.App">
     <Application.Resources>
        <ResourceDictionary>
            <Style  x:Key="btnStyle"  TargetType="Button">
                <Setter Property="HorizontalOptions" Value="Center" />
                <Setter Property="VerticalOptions" Value="CenterAndExpand" />
                <Setter Property="BorderColor" Value="Lime" />
                <Setter Property="BorderRadius" Value="5" />
                <Setter Property="BorderWidth" Value="5" />
                <Setter Property="WidthRequest" Value="200" />
                <Setter Property="TextColor" Value="Teal" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

在其他文件中调用样式-

<Button Text="Login" Style="{StaticResource btnStyle} TextColor="Black" BackgroundColor="#9C661F"  Clicked="btnLoginClicked" />

【问题讨论】:

    标签: xaml styles xamarin.forms


    【解决方案1】:

    在将您的 App 类转换为基于 XAML 的过程中,您似乎错过了构造函数中的 InitializeComponent(); 调用。

    【讨论】:

      【解决方案2】:

      我想你忘记添加 InitializeComponent();在您的 App.cs 中。当您有 xaml 代码时,您必须调用 InitializeComponent();在构造函数中。

      【讨论】:

        【解决方案3】:

        您实际上还需要在 Style 中设置 BackgroundColor 以使 BorderColor 起作用。

        From the Button.BorderColor Documentation

        如果 Button.BorderWidth 设置为 0,则此属性无效。在 Android 上,除非 VisualElement.BackgroundColor 设置为非默认颜色,否则此属性将无效。

        风格:

        <ResourceDictionary>
          <Style  x:Key="btnStyle"  TargetType="Button">
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="CenterAndExpand" />
            <Setter Property="BackgroundColor" Value="Black" />
            <Setter Property="BorderColor" Value="Lime" />
            <Setter Property="BorderRadius" Value="5" />
            <Setter Property="BorderWidth" Value="5" />
            <Setter Property="WidthRequest" Value="200" />
            <Setter Property="TextColor" Value="Teal" />
          </Style>
        </ResourceDictionary>
        

        在 Xaml 中的用法:

          <Button Text="Login" Style="{StaticResource btnStyle}" />
        

        这对我有用。

        编辑: Example Project

        【讨论】:

        • 嗨@Marius - 我不知道,但两天以来我一直在努力解决这个问题,并且我收到了找不到 StaticResource btnStyle 的错误。如果我在每个单独的文件中声明上述样式,我就会明白。你能告诉我你遵循了什么程序吗?
        • 嗨,我添加了一个指向我复制您的问题的项目的链接。您可以在引用样式的地方发布完整的 xaml 页面吗?我不确定为什么这对你不起作用。
        猜你喜欢
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 2019-06-07
        • 1970-01-01
        • 2011-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多