【问题标题】:C# Xamarin How to update a static string in a labelC# Xamarin 如何更新标签中的静态字符串
【发布时间】:2019-08-16 13:06:46
【问题描述】:

我想用静态字符串更新标签。 那是我的实际代码

public partial class MainPage : INotifyPropertyChanged
{
    private static String _uploadstring;
    /// <summary>
    /// A static property which you'd like to bind to
    /// </summary>
    public static String Uploadstring
    {
        get
        {
            return _uploadstring;
        }

        set
        {
            _uploadstring = value;

        }
    }

我想用

来称呼它
        Device.BeginInvokeOnMainThread(async () =>
        {
            Uploadstring = "TEEEEEEST";
        });

这是我的 XAML 绑定

       <Label Text="{Binding Source={x:Staticlocal:MainPage.UploadString}}" x:Name="ttts"  TextColor="Red" TranslationY="50" HeightRequest="30" FontSize="26" HorizontalOptions="Center"/>

如果我设置断点,我会看到 Uploadstring 在我的 XAML 中更新。但他没有显示它,因为他不更新文本。我该如何解决我的问题?

【问题讨论】:

  • 您使用密钥设置 OnPropertyChanged 事件的位置

标签: c# xamarin binding static cross-platform


【解决方案1】:

在Setter上插入一行怎么样?

public static String Uploadstring
{
    get
    {
        return _uploadstring;
    }
    set
    {
        _uploadstring = value;
        Form.ttts.Text = _uploadstring;  // This line (Modified)
    }
}

如果不喜欢这样使用,需要绑定Twoway Mode

已添加

添加这个以使用静态方法

    public static MainPage Form { get; set; } // This Line

    public MainPage()
    {
        InitializeComponent();
        Form = this; // And This Line
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多