Wpf 带MVVM的异步等待进度条
如何使用MVVM实现progressbar或进度状态。下面是我的ViewModel的基本外观。 我基本上是通过在继承的CrudVMBase类中实现Relaycommand来启动getdata函数。当它完成任务时,它将引发ProductModels的属性changed,并在我的xaml中绑定到datagrid,如下所示。这有时需要一个小时。我想为xaml实现一些状态/进度信息。不确定我是否可以使用 Messenger.[Default].SendOf UserMessagemsg MvvmLight的功能。我使用它已经返回了异常,如下面的try-catch部分所示Wpf 带MVVM的异步等待进度条,wpf,vb.net,asynchronous,mvvm,Wpf,Vb.net,Asynchronous,Mvvm,如何使用MVVM实现progressbar或进度状态。下面是我的ViewModel的基本外观。 我基本上是通过在继承的CrudVMBase类中实现Relaycommand来启动getdata函数。当它完成任务时,它将引发ProductModels的属性changed,并在我的xaml中绑定到datagrid,如下所示。这有时需要一个小时。我想为xaml实现一些状态/进度信息。不确定我是否可以使用 Messenger.[Default].SendOf UserMessagemsg MvvmLigh
Namespace ViewModels
Public Class CheckProductsViewModel
Inherits CrudVMBase
Private m_ProductModels As ObservableCollection(Of ProductVM)
Public Property ProductModels() As ObservableCollection(Of ProductVM)
Get
Return m_ProductModels
End Get
Set(value As ObservableCollection(Of ProductVM))
m_ProductModels = value
End Set
End Property
Public Sub New()
MyBase.New()
End Sub
Private _totalTime As String
Public Property totalTime() As String
Get
Return _totalTime
End Get
Set(ByVal value As String)
_totalTime = value
End Set
End Property
Protected Overrides Async Sub GetData()
Dim watch As Stopwatch = Stopwatch.StartNew()
ThrobberVisible = Visibility.Visible
Try
Dim myProducts = Await (myContext.Products.Where(Function(w) w.Name <> "").ToListAsync())
Dim myProductComponent As New ProductComponent
Dim _ProductModels As ObservableCollection(Of ProductVM)
Dim counter = 0
Dim tasks = myProducts.Select(Function(x) myProductComponent.testUrl_async(x))
Dim results = Await Task.WhenAll(tasks)
Dim r = (From item In results Where item IsNot Nothing Select New ProductVM With { _
.IsNew = False, _
.TheProductModel = item _
}).ToList()
_ProductModels = New ObservableCollection(Of ProductVM)(r)
ProductModels = _ProductModels
watch.Stop()
totalTime = "total time:" & Math.Round(watch.Elapsed.TotalSeconds).ToString
RaisePropertyChanged("totalTime")
RaisePropertyChanged("ProductModels")
Catch ex As Exception
Dim msg As New UserMessage()
msg.isException = True
msg.Message = ex.ToString
Messenger.[Default].Send(Of UserMessage)(msg)
End Try
ThrobberVisible = Visibility.Collapsed
End Sub
End Class
End Namespace
XAML:
您不能只向CheckProductsViewModel添加一个进度属性吗? 使其实现RaisePropertyChanged事件,并在xaml中将该值绑定到ProgressBar的value属性 除非我不清楚这个问题,否则绝对没有必要在这里使用MVVMLight的Messenger
<Grid>
<cst:CustomDataGrid x:Name="grdProduct"
ItemsSource="{Binding ProductModels, IsAsync=True}"
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource myDataGrid2}"
>
....
</Grid>