2016-04-19 11 views
1

Ich versuche Text von Thread zu Haupt GUI-Thread zu aktualisieren, aber Ausnahme ausgelöst, weil es in anderen Thread ist. Ich versuche, Dispatcher.CheckAccess(), BeginInvoke() zu verwenden, aber dieses Projekt ist nicht Silverlight oder WPF. Ich versuche den alten Stil InvokeRequired, Invoke(), aber diese sind nicht verfügbar.Windows Phone C# Aktualisierung von Text auf GUI mainpage.xaml.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:

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:

Nach unten Anweisungen innerhalb der ersten if-Anweisung setzen, ist die aktualisierte Daten in der Haupt-GUI angezeigt.

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

Antwort

0

sollten Sie verwenden CoreDispatcher in WP 8,1

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

Siehe MSDN Link zu CoreDispatcher

+0

Ihnen danken. Es funktioniert jetzt, nachdem Sie Ihrem Vorschlag gefolgt haben. –

Verwandte Themen