简体   繁体   中英

How can I change the color of a Label in a template in the C# back end?

I have this template that's simplified for the question:

<?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>

and this C#

using Xamarin.Forms;

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

    }
}

Can someone help me by telling me how I can change the TextColor of the label in the XAML inside the constructor of the back-end C#. Note that there's much more to this but I'm simplifying what I need for the question as if I know this then I can do the rest.

Specify x:Name="MyLabel" on the Label you want to access.

Then you can access that Label in your back-end C# file like so:

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

That allows you to be able access any public property on the Label

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM