【问题标题】:Can you change the textcolor of all the tools in your project?您可以更改项目中所有工具的文本颜色吗?
【发布时间】:2022-01-22 16:16:34
【问题描述】:

您可以更改项目中所有文本的文本颜色吗?我的意思不是绑定或什么,只是通过设置默认颜色还是我真的需要更改每个标签/条目/按钮等的属性...?

【问题讨论】:

    标签: c# xamarin mvvm


    【解决方案1】:

    在 Xamarin 中,您可以创建全局样式。 来自documentation

    样式可以通过将样式添加到应用程序的资源字典中来全局使用。这有助于避免跨页面或控件的样式重复。

    【讨论】:

      【解决方案2】:

      一种方法是使用样式并定位到标签/条目/按钮等。

      <Style TargetType="Label">
      <Setter Property="TextColor" Value="Black" />
      </Style>
      

      更多详情请参考:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/

      另一种方法是将颜色设置为资源。

      设置资源:

       <Application.Resources>
          <!-- Colors -->
          <Color x:Key="NormalTextColor">Black</Color>
      </Application.Resources>
      

      用法:

         <Label Text="Hello"
                         TextColor="{StaticResource NormalTextColor}"
                         FontAttributes="Bold" />
      

      更多详情请参考:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/resource-dictionaries

      【讨论】:

        【解决方案3】:

        喜欢 TheTanic 的回答。 例如:

        App.xaml 一个Style 中,用于名称为BLabel 的标签。

        <Style x:Key="BLabel" TargetType="Label">
            <Setter Property="TextColor"  Value="#A7ADB1" />
            <Setter Property="HorizontalOptions" Value="Start" />
            <Setter Property="VerticalOptions" Value="Center" />
        </Style>
        

        你可以像这样在MainPage.xaml中使用它。

         <Label
           Grid.Row="4"
           Grid.Column="1"
           Style="{StaticResource BLabel}"
           Text="BB 3" />
        

        但您还可以添加更多内容,例如:

         <Setter Property="WidthRequest" Value="150" />
            <Setter Property="HeightRequest" Value="40" />
            <Setter Property="FontSize" Value="Small" />
            <Setter Property="BorderWidth" Value="1" />
            <Setter Property="BackgroundColor" Value="Red" />
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="TextTransform" Value="None" />
        

        还有更多......

        不仅适用于标签,还适用于按钮等。

        这是StaticResource 的示例,但您也可以使用DynamicResource 更改颜色等。

        https://www.youtube.com/watch?v=Se0yF5JXk70&ab_channel=JamesMontemagno

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-27
          • 1970-01-01
          • 2012-07-01
          • 2013-03-01
          相关资源
          最近更新 更多