【发布时间】:2022-08-03 00:29:11
【问题描述】:
当我执行更新屏幕()函数,当在 TextLabel 字符串中设置新值时会引发异常。此异常在代码后面的图中演示。
当我通过调用屏幕更新时发生此错误INotifyPropertyChanged接口或通过方法可观察对象类,创建一个新线程后。
我的代码:
public class PageInicialViewModel : ObservableObject
{
private int cont = 0;
private string _textLabel = 0.ToString();
public string TextLabel
{
get => _textLabel;
set => SetProperty(ref _textLabel, value);
}
public void updateScreen()
{
Task.Factory.StartNew(updateTextLabel);
}
public void updateTextLabel()
{
while (true)
{
cont++;
TextLabel = cont.ToString();
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}
}
错误:System.Runtime.InteropServices.COMException:\'应用程序调用了一个为不同线程编组的接口。 (0x8001010E (RPC_E_WRONG_THREAD))\'
标签: c#