Windows phone C# 将文本更新到 GUI mainpage.xaml.cs?
Windows phone C# updating text to GUI mainpage.xaml.cs?
我正在尝试将文本从线程更新到主 GUI 线程,但抛出了异常,因为它在不同的线程中。我尝试使用 Dispatcher.CheckAccess()、BeginInvoke() 但此项目不是 Silverlight 或 WPF。我尝试了旧式的 InvokeRequired、Invoke(),但这些都不可用。
StartTest.cs:
public StartTest(MainPage mainPage)
{
mainPageObj = mainPage;
}
public async Task RunTest(string testCase)
{
await Task.Run(() => RunTestCase(testCase));
}
public bool RunTestCase(string testcase)
{
switch (testcase)
{
case "testcase1":
int i = 0;
while (i < 10000)
{
i++;
}
mainPageObj.UpdatePassFail(true);//update the main GUI
break;
case "testcase2":
mainPageObj.UpdatePassFail(false);//update the main GUI
break;
}
return false;
}
Mainpage.xaml.cs:
public void UpdatePassFail(bool pass)
{
try
{
if (pass == true)
{
passCount++;
passFailCount = passCount + failCount;
textBlock_passTotal.Text = passCount.ToString();
textBlock_passFailTotal.Text = passFailCount.ToString();
}
else if (pass == false)
{
failCount++;
passFailCount = passCount + failCount;
textBlock_failTotal.Text = failCount.ToString();
textBlock_passFailTotal.Text = passFailCount.ToString();
}
}
catch(Exception error)
{
textBlock_tapInfo.Text = error.Message;
}
}
更新:
将以下语句放入第一个 if 语句后,更新后的数据显示在主 GUI 中。
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
textBlock_passTotal.Text = passCount.ToString();
textBlock_passFailTotal.Text = passFailCount.ToString();
});
您应该在 WP 8.1
中使用 CoreDispatcher
CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
//UI code goes here
}
);
参考MSDNlink关于CoreDispatcher
我正在尝试将文本从线程更新到主 GUI 线程,但抛出了异常,因为它在不同的线程中。我尝试使用 Dispatcher.CheckAccess()、BeginInvoke() 但此项目不是 Silverlight 或 WPF。我尝试了旧式的 InvokeRequired、Invoke(),但这些都不可用。
StartTest.cs:
public StartTest(MainPage mainPage)
{
mainPageObj = mainPage;
}
public async Task RunTest(string testCase)
{
await Task.Run(() => RunTestCase(testCase));
}
public bool RunTestCase(string testcase)
{
switch (testcase)
{
case "testcase1":
int i = 0;
while (i < 10000)
{
i++;
}
mainPageObj.UpdatePassFail(true);//update the main GUI
break;
case "testcase2":
mainPageObj.UpdatePassFail(false);//update the main GUI
break;
}
return false;
}
Mainpage.xaml.cs:
public void UpdatePassFail(bool pass)
{
try
{
if (pass == true)
{
passCount++;
passFailCount = passCount + failCount;
textBlock_passTotal.Text = passCount.ToString();
textBlock_passFailTotal.Text = passFailCount.ToString();
}
else if (pass == false)
{
failCount++;
passFailCount = passCount + failCount;
textBlock_failTotal.Text = failCount.ToString();
textBlock_passFailTotal.Text = passFailCount.ToString();
}
}
catch(Exception error)
{
textBlock_tapInfo.Text = error.Message;
}
}
更新:
将以下语句放入第一个 if 语句后,更新后的数据显示在主 GUI 中。
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
textBlock_passTotal.Text = passCount.ToString();
textBlock_passFailTotal.Text = passFailCount.ToString();
});
您应该在 WP 8.1
中使用CoreDispatcher
CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
//UI code goes here
}
);
参考MSDNlink关于CoreDispatcher