我想通過檢查 toggleButton 開始一個程序,并在完成 proccess toggleButton 后取消選中。
這是我的代碼。
行程.xaml :
<ToggleButton Command="{Binding StartProccessCommand}" Content="Proccessing" IsChecked="{Binding isChecked,Mode=TwoWay}"></ToggleButton>
ProcessViewModel.cs :
public class ProccessViewModel: BindableBase
{
private bool _isChecked = false;
public bool isChecked
{
get { return _isChecked; }
set { SetProperty(ref _isChecked, value); }
}
public DelegateCommand StartProccessCommand{ get; set; }
public ProccessViewModel()
{
StartProccessCommand= new DelegateCommand(OnToggleButtonClicked);
}
public async void OnToggleButtonClicked()
{
await Task.Run(() => {
isChecked= true;
for (int i = 0; i < 50000; i )
{
Console.WriteLine(i);
}
}).ContinueWith((x) =>
{
for (int i = 50000; i < 100000; i )
{
Console.WriteLine(i);
}
isChecked= false;
}
}
但是當我在檢查后立即運行代碼 ToggleButton Unchecked 時。
結果 :
ToggleButton 選中
ToggleButton 未選中
1
2
.
.
49999
50000
50001
。
.
100000
uj5u.com熱心網友回復:
你為什么用ContinueWith
with await
?這是沒有意義的,因為OnToggleButtonClicked
一旦等待Task
完成,其余的將被執行。
設定屬性,等待第一個Task
然后等待另一個Task
并將屬性設定回false
:
public async void OnToggleButtonClicked()
{
isChecked = true;
await Task.Run(() => {
for (int i = 0; i < 50000; i )
{
Console.WriteLine(i);
}
});
await Task.Run(() =>
{
for (int i = 50000; i < 100000; i )
{
Console.WriteLine(i);
}
});
isChecked = false;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315354.html
上一篇:使用Helix工具的WPF,在嘗試更新ModelVisual3D視圖時,PropertyChangedEventHandler始終為null
下一篇:用滑鼠拖動彈出視窗