2017-01-26 4 views
-2

私はC#を初めて使いました。別のハードウェアとのシリアル通信を行うコードを書こうとしています。 私は仕事をするためにasync/awaitを使用しています。私のメソッドがclass1という別のクラスにあり、ソリューションに添付されているclass1.csファイルに存在することを考慮して、待っているメソッドからプログレスバーを更新したいと思います。 XAMLコードには、1つのテキストブロック、1つの進行状況バー、1つの開始ボタンがあります。ユニバーサルウィンドウアプリケーションの別のクラスの待っていたメソッドからUIを更新しました

"class1"クラスの "DummyWork"メソッドから "progressbar1"をどのように更新できますか?

この問題で誰かが私を助けることができたら、私は感謝します。 私のテンプレートコードは以下の通りです。

MainPage.xaml.cs内容:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using System.Threading.Tasks; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
using System.Threading; 
using uwptestapp1classes; 

namespace uwptestapp1 
{ 
    public sealed partial class MainPage : Page 
    {  
     public MainPage() 
     { 
      this.InitializeComponent(); 
      textBlock.Text = string.Empty; 
     } 

     private async void startBtn_Click(object sender, RoutedEventArgs e) 
     { 
      int asyncResult = 0; 
      progressbar1.Value = 0; 
      startBtn.IsEnabled = false; 
      textBlock.Text += "button press log: " + startBtn.Content + " button is pressed" + Environment.NewLine; 
      DateTime previousTime = DateTime.Now; 
      asyncResult = await Task.Run(() => Class1.DummyWork(previousTime)); 
      textBlock.Text += "Task have been done" + Environment.NewLine; 
      startBtn.IsEnabled = true; 
     } 
    } 
} 

Class1.csの内容:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Windows.UI.Xaml.Controls; 
using System.Threading; 

namespace uwptestapp1classes 
{ 
    public class Class1 
    { 
       public static int DummyWork(DateTime previoustime) 
     { 
      for (int i = 0; i <= 100;) 
      { 
       if ((DateTime.Now - previoustime).Milliseconds >= 500) 
       { 
        /* UI update code 
        * update "progressbar1.value" 
        */ 
        previoustime = DateTime.Now; 
       } 

       //delay without using task.delay 
       for (int myCounter = 0; myCounter < 50000000;) 
       { 
        myCounter++; 
       } 
      } 
      return 1; 
     } 
    } 
} 
+2

ここで質問は表示されません。 [なぜ誰かが私を助けることができますか?実際の質問ではありませんか?](http://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – BradleyDotNET

+0

@BradleyDotNETご意見ありがとうございます。私は正確な質問で更新しました。 – Arash

+1

[これについて多くの研究を行っていないようです](https://www.google.com/?gws_rd=ssl#q=uwp+progress+bar+from+task+c%23)。あなたは? – Romasz

答えて

1

コードの一部から進捗状況を報告するために、あなたはIProgress<T>を使用する必要があります。

public class Class1 
{ 
    public static int DummyWork(DateTime previoustime, IProgress<DateTime> progress = null) 
    { 
    for (int i = 0; i <= 100;) 
    { 
     if ((DateTime.Now - previoustime).Milliseconds >= 500) 
     { 
     progress?.Report(DateTime.Now); 
     } 

     //delay without using task.delay 
     for (int myCounter = 0; myCounter < 50000000;) 
     { 
     myCounter++; 
     } 
    } 
    return 1; 
    } 
} 

進捗状況を受け取るには、Progress<T>

public sealed partial class MainPage : Page 
{  
    private async void startBtn_Click(object sender, RoutedEventArgs e) 
    { 
    int asyncResult = 0; 
    progressbar1.Value = 0; 
    startBtn.IsEnabled = false; 
    textBlock.Text += "button press log: " + startBtn.Content + " button is pressed" + Environment.NewLine; 
    DateTime previousTime = DateTime.Now; 
    var progress = new Progress<DateTime>(time => 
    { 
     progressbar1.value = ...; 
    }); 
    asyncResult = await Task.Run(() => Class1.DummyWork(previousTime, progress)); 
    textBlock.Text += "Task have been done" + Environment.NewLine; 
    startBtn.IsEnabled = true; 
    } 
} 
+0

ありがとうございました。 "?" 'progress?.Report(DateTime.Now);'の意味ですか? – Arash

+0

'progress'が' null'でなければ、単に 'Report'を呼び出すことを意味します。 –

関連する問題