我想从另一个线程登录到uwp中的列表框。
我从窗口窗体中知道我应该使用委托。但是没有调用方法。这是我用于日志记录的界面。这是我尝试使用的第一件事。
public interface ILoggingService
{
void LogInformation(LogEntryType logEntryType,string logEntryMessage);
}
delegate void LoggerDelegate(LogEntryType logEntryType,string message);
LoggerDelegate _loggerDelegate = new LoggerDelegate(LogInformation);
public void LogInformation(LogEntryType logEntryType,string logEntryMessage)
{
if (lbxInformation.InvokeRequired)
{
lbxRequestInformation.Invoke(logRequestInformationDelegate,message);
}
else
{
lbxInformation.Items.Add(message);
}
}
使用 Dispatcher.RunAsync 从另一个线程更新 UI。以下是文档:
https://learn.microsoft.com/en-us/windows/uwp/threading-async/using-windows-runtime-objects-in-a-multithreaded-environment