声明静态和非静态变量

时间:2018-12-15 06:06:34

标签: c# xamarin xamarin.forms xamarin.android

我声明了一个公共静态字符串,并将变量放入函数中以了解我的同步状态。我的问题是变量不会覆盖。我知道这是一个静态变量。我不知道如何在公共静态异步void 内覆盖该变量或声明一个非静态变量,这样我才能知道我的同步状态是什么。我将在我的xaml中显示状态。

public class SyncFunction
{
    public static string status; //This will display status of every function below

    public static async void SyncUser(string host, string database, string contact, string ipaddress, byte[] pingipaddress){
      ... // Function here
      status = "Syncing user";
    }

    public static async void SyncHost(string host, string database, string contact, string ipaddress, byte[] pingipaddress){
      ... // Function here
      status = "Syncing host";
    }

}

在我的XAML中,CS将是:

lblStatus.text = SyncFunction.status; //will display the status of my sync

1 个答案:

答案 0 :(得分:1)

之所以会发生这种情况,是因为代码中 status (字符串)的默认值为 null lblStatus 将其视为 String 。空

您必须先呼叫 SynUser SyncHost ,然后再尝试在视图(xaml)中显示状态

希望这会有所帮助。