私は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;
}
}
}
ここで質問は表示されません。 [なぜ誰かが私を助けることができますか?実際の質問ではありませんか?](http://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – BradleyDotNET
@BradleyDotNETご意見ありがとうございます。私は正確な質問で更新しました。 – Arash
[これについて多くの研究を行っていないようです](https://www.google.com/?gws_rd=ssl#q=uwp+progress+bar+from+task+c%23)。あなたは? – Romasz