async await进度条与MVVM

时间:2015-09-03 12:41:49

标签: wpf vb.net asynchronous mvvm

如何使用MVVM实现进度条或进度状态。基本上下面是我的ViewModel的样子。 我基本上通过在继承的CrudVMBase类中实现Relaycommand来触发getdata函数。当它完成它是作业时,它将提升ProductModel的属性,并在我的xaml中绑定到datagrid,如下所示。这有时需要长达一个小时。我想向xaml实现一些状态/进度信息。不确定我是否可以使用

实现这一目标

Messenger。[默认] .Send(Of UserMessage)(msg)

MvvmLight的功能。我正在使用它已经返回异常,如下面的try catch部分所示。

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:

 <Grid>
            <cst:CustomDataGrid x:Name="grdProduct"                                                             
                                  ItemsSource="{Binding ProductModels, IsAsync=True}"                
                 Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"    
                 Style="{StaticResource myDataGrid2}"
                  >
    ....              
 </Grid>

1 个答案:

答案 0 :(得分:0)

你不能只是添加一个&#34; Progress&#34; CheckProductsViewModel的属性? 让它实现RaisePropertyChanged事件,并在你的xaml中将该值绑定到ProgressBar的value属性。

除非我没有清楚地理解这个问题,否则MVVMLight的使者绝对没有必要