首页 > 解决方案 > 如何在 C# 后端的模板中更改标签的颜色?

问题描述

我有这个针对问题进行了简化的模板:

<?xml version="1.0" encoding="UTF-8"?>
<Frame xmlns="http://xamarin.com/schemas/2014/forms" 
                      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
   xmlns:t="clr-namespace:Japanese.Templates" 
   xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
   x:Class="Japanese.Templates.RoundButtonText" x:Name="this">
   <Label Text="ABC" />
</Frame>

还有这个 C#

using Xamarin.Forms;

namespace Japanese.Templates
{
    public partial class RoundButtonText : BaseFrameButtonTemplate
    {
        public RoundButtonText()
        {
            InitializeComponent();
            ?? = Color.Red;
        }

    }
}

有人可以告诉我如何在后端 C# 的构造函数中更改 XAML 中标签的 TextColor 来帮助我。请注意,还有更多内容,但我正在简化问题所需的内容,就好像我知道这一点一样,然后我可以做剩下的事情。

标签: xamarinxamarin.forms

解决方案


在您要访问x:Name="MyLabel"标签上指定。

然后,您可以像这样在后端 C# 文件中访问该标签:

public RoundButtonText()
{
    InitializeComponent();
    MyLabel.TextColor = Color.Red; //OR
    MyLabel.BackgroundColor = Color.Red;
}

这使您能够访问标签上的任何公共属性


推荐阅读