【问题标题】:Declare static and non-static variable声明静态和非静态变量
【发布时间】:2018-12-15 16:16:41
【问题描述】:

我声明了一个公共静态字符串,并将变量放入函数中以了解我的同步状态。我的问题是变量不会覆盖。我知道这是一个静态变量。我不知道如何覆盖变量或在 public static async 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

【问题讨论】:

  • 您多久执行一次最后一条语句?它只会分配一次当前值。您可能需要设置依赖项

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


【解决方案1】:

这是因为您的代码中 status(string) 的默认值为 nulllblStatus 会将其视为 String .空.

您必须先调用 SynUserSyncHost,然后才能尝试在视图 (xaml) 中显示 status

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多