简体   繁体   English

如何在 xamarin 中更改标签文本

[英]How to change label text in xamarin

I am relatively new to Xamarin forms.我对 Xamarin 表单比较陌生。 I have found out I am unable to change label text from the code behind.我发现我无法从后面的代码中更改标签文本。 Normally I would do myLabel.text = variable .通常我会做myLabel.text = variable Does this work in Xamarin?这在 Xamarin 中有效吗? If it does why does this code not change the text?如果确实如此,为什么此代码不更改文本?

Label_ControlSW.Text = controlSW_Out;
            Label_BLESW.Text = bleSW_Out;
            Label_Mode.Text = mode_Out;

Xaml file Xaml 文件

<Label x:Name="Label_ControlSW" Grid.Row="1" Grid.Column="1" HorizontalOptions="Center"  VerticalOptions="Center" FontSize="17" TextColor="White"/>
                <Label x:Name="Label_BLESW" Grid.Row="2" Grid.Column="1" HorizontalOptions="Center"  VerticalOptions="Center" FontSize="17" TextColor="#525252"/>
                <Label x:Name="Label_Mode"  Grid.Row="4" Grid.Column="1" HorizontalOptions="Center"  VerticalOptions="Center" FontSize="17" TextColor="White"/>

Does this work in Xamarin?这在 Xamarin 中有效吗?

Yes, it does.是的,它确实。

If it does why does this code not change the text?如果确实如此,为什么此代码不更改文本?

Because the Label component is not bounded to the variable, it just gets its value when you did Label_ControlSW.Text = controlSW_Out;因为Label组件没有绑定到变量,所以当你执行Label_ControlSW.Text = controlSW_Out;时,它只是获取它的值Label_ControlSW.Text = controlSW_Out; and no furthermore.没有了。

To make it works you have basically two choices:要使其正常工作,您基本上有两种选择:

1. Set the value to the label on every change; 1.在每次更改时将值设置为标签;

There's no magic here.这里没有魔法。 Just set the values or variables like Ali Heikal's answer suggests, but you must do that every time manually.只需像Ali Heikal 的回答所建议的那样设置值或变量,但您每次都必须手动执行此操作。

2. Bind the page (View) to an Observable object (Model) , then the view will listen to every change on your model and react to this (changing it's own Text value, for example). 2. 将页面 (View) 绑定到一个 Observable 对象 (Model) ,然后该视图将侦听模型上的每个更改并对此做出反应(例如,更改它自己的Text值)。

I guess what you're intending to do is the second one.我猜你打算做的是第二个。 So you can create a public string property on your page's code-behind and bind the instance of your page to itself.因此,您可以在页面的代码隐藏上创建公共字符串属性,并将页面实例绑定到自身。 Like this:像这样:

XAML XAML

<Label Text="{Binding MyStringProperty}"
       .../>

Code behind背后的代码

public partial class MyTestPage : ContentPage
{
    private string myStringProperty;
    public string MyStringProperty
    {
        get { return myStringProperty; }
        set 
        {
            myStringProperty = value;
            OnPropertyChanged(nameof(MyStringProperty)); // Notify that there was a change on this property
        }
    }
    
    public MyTestPage()
    {
        InitializeComponents();
        BindingContext = this;

        MyStringProperty = "New label text"; // It will be shown at your label
    }
}

You should take a look at official docs about data bindings and MVVM pattern on XF and if you're starting with Xamarin.Forms, I highly recommend you to follow the official getting started guide that addresses each topic clear and deep enough to learn everything you need.您应该查看有关 XF 上的数据绑定和 MVVM 模式的官方文档,如果您从 Xamarin.Forms 开始,我强烈建议您遵循官方入门指南,该指南清楚而深入地解决了每个主题,以了解您的所有知识需要。

I hope it helps.我希望它有帮助。

Try initializing the Text value in XAML like the following:尝试在 XAML 中初始化Text值,如下所示:

<Label x:Name="YourLableName" Text="Initial Label"/>

Then access it in the code behind like the following:然后在后面的代码中访问它,如下所示:

YourLableName.Text = "Desired Name";

or或者

YourLableName.Text = variable;

In order to update the UI, you have to be on the UI thread.为了更新 UI,您必须在 UI 线程上。 You would want to do something like:你会想要做这样的事情:

 Device.BeginInvokeOnMainThread(() =>
 {
     Label_ControlSW.Text = controlSW_Out;
     Label_BLESW.Text     = bleSW_Out;
     Label_Mode.Text      = mode_Out;
 });

This will solve your problem, but as the others have stated in their answers, the Xamarin way to do this would be using data binding to update the view.这将解决您的问题,但正如其他人在他们的答案中所述,Xamarin 的方法是使用数据绑定来更新视图。 The data binding will handle the UI update for you.数据绑定将为您处理 UI 更新。

I faced the same issue.我遇到了同样的问题。 Try to clean and build your project after u make x:Name reference in your view folder.在您的视图文件夹中创建 x:Name 引用后,尝试清理和构建您的项目。 Or you can do Data binding .或者你可以做数据绑定 By using this code通过使用此代码

 <Label Text="{Binding MyProperty}" />

in Xaml .Xml 中 And in ViewModel classViewModel类中

public class MyViewModel
{
    public string MyProperty { get; set; } = "My Label Text";
}

And in code behind just mention the ViewModel your using在后面的代码中只提到您使用的ViewModel

this.BindingContext = new MyViewModel();

This is the correct approach.这是正确的做法。 So try to use MVVM approach in your future of coding because this is the right approach.所以尝试在你未来的编码中使用MVVM方法,因为这是正确的方法。

There were a few reasons why this didn't work.有几个原因导致这不起作用。 I changed to the MVVM approach.我改用了 MVVM 方法。 This still had no effect but it was a better approach as was suggested.这仍然没有效果,但正如所建议的那样,这是一种更好的方法。 When debugging I noticed this line: I/Choreographer(16942): Skipped 31 frames!调试时我注意到这一行:I/Choreographer(16942): Skipped 31 frames! The application may be doing too much work on its main thread.应用程序可能在其主线程上做了太多工作。 This was because I was doing too much on the main thread (ble connections) and it was just skipping the UI change.这是因为我在主线程(ble 连接)上做了太多事情,而它只是跳过了 UI 更改。 I had to restructure my code to perform more on different threads.我不得不重构我的代码以在不同的线程上执行更多操作。 Thanks for the help everyone.谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测 xamarin UWP 应用程序中标签元素的文本值更改? - how to detect text value change for label element in xamarin UWP application? Xamarin 如果包含减号,我如何更改 label 文本颜色 - Xamarin How can i change label text color if it contains minus Xamarin Forms 更改框架中的标签文本 - Xamarin Forms Change label text in Frame 如何使用 Xamarin 更改带有随机短语列表的标签 - How to change a label with a random list of phrases with Xamarin 如何动态更改标签的颜色? Xamarin - How to dynamically change the colour of label? Xamarin 如何在xamarin表单中的标签文本上画一条线 - how to draw a line on label text in xamarin forms 如何在Xamarin Forms中同时更改多个标签的文本颜色? - How to change multiple label's text color at the same time in Xamarin Forms? 如何从另一个在Xamarin IOS中添加UITabBarController的控制器更改标签的文本值 - How to change the text value of label from another controller which added UITabBarController in Xamarin IOS 如何在 Xamarin.Forms 中使用触发器在 Entry 控件 IsFocused 变为 true 时更改标签文本? - how to change the label text when Entry control IsFocused becomes true using trigger in Xamarin.Forms? Xamarin形成标签动画后如何保留标签文本 - Xamarin forms how to keep label text after label animation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM