简体   繁体   English

Windows phone C#将文本更新到GUI mainpage.xaml.cs?

[英]Windows phone C# updating text to GUI mainpage.xaml.cs?

I'm trying to update text from thread to main GUI thread, but exception thrown because it's in different thread. 我正在尝试将文本从线程更新到主GUI线程,但抛出异常,因为它在不同的线程中。 I try to use Dispatcher.CheckAccess(), BeginInvoke() but this project is not Silverlight or WPF. 我尝试使用Dispatcher.CheckAccess(),BeginInvoke()但该项目不是Silverlight或WPF。 I try the old style InvokeRequired, Invoke(), but these are not available. 我尝试旧样式InvokeRequired,Invoke(),但这些都不可用。

StartTest.cs: 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: 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;
        }            
    }

Update: 更新:

After putting the below statements inside the first if-statement, the updated data is shown in main GUI. 将以下语句放在第一个if语句中后,更新的数据显示在主GUI中。

Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    textBlock_passTotal.Text = passCount.ToString();
                    textBlock_passFailTotal.Text = passFailCount.ToString();
                });

You should use CoreDispatcher in WP 8.1 您应该在WP 8.1中使用CoreDispatcher

CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => 
{ 
       //UI code goes here
}
);

Refer MSDN link about CoreDispatcher 请参阅有关CoreDispatcher MSDN链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows Phone中从MainPage.xaml.cs文件到MainPage.xaml文件获取字符串变量的值 - Get value of a string Variable from MainPage.xaml.cs file to MainPage.xaml file in Windows Phone 从Windows Phone 8中的App.Xaml.cs到达MainPage.Xaml.cs单击处理程序函数 - Reaching MainPage.Xaml.cs click handler functions from App.Xaml.cs in windows phone 8 类在MainPage.xaml.cs上实例化2次 - Class instantiate 2 times on MainPage.xaml.cs 无法初始化MainPage.xaml.cs中的AudioGraph并无法通过MainPage.xaml.cs上的滑块设置频率 - Unable to initialize an AudioGraph in MainPage.xaml.cs and set frequency with a slider on MainPage.xaml.cs 在MainPage.xaml.cs -xamarin-C#中看不到对象 - can't see objects in MainPage.xaml.cs -xamarin-C# 在 MainPage.xaml.cs 中使用 Android 特定代码 - Use Android specific code in MainPage.xaml.cs 从phonegap应用程序的MainPage.xaml.cs切换html页面 - Switching the html page from the MainPage.xaml.cs of a phonegap application 将 ToolbarItems 添加到 Xamarin.Forms 项目中的 MainPage.xaml.cs - Adding ToolbarItems to MainPage.xaml.cs in a Xamarin.Forms Project 如何从MainPage.xaml.cs调用LoadData() - How to call LoadData() from MainPage.xaml.cs 如何访问位于DataTemplate标签的文本框控件 <phone:PanoramaItem.HeaderTemplate> 从代码开始-在后面(MainPage.xaml.cs) - How to access textbox control located in DataTemplate tags of <phone:PanoramaItem.HeaderTemplate> from code - behind (MainPage.xaml.cs)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM