簡體   English   中英

viewmodel和視圖之間的交互

[英]Interaction between viewmodel and view

我使用Windows Phone應用程序並使用MVVM,在我的viewmodel中,我在那里加載數據代碼,加載數據的屬性綁定到我的視圖(XAML),那么如何獲取有關加載和加載數據的信息? 因為我需要在我的xaml中添加ProgressIndicator所以我的用戶可以知道數據的加載...

如果我沒有使用mvvm並將我的所有代碼放入mainpage.xaml.cs那么我只需要將ProgressIndicator直接放入我的加載代碼中就像這樣

SystemTray.ProgressIndicator.IsIndeterminate = true;

SystemTray.ProgressIndicator.IsVisible = true;

(..我的邏輯代碼..)

SystemTray.ProgressIndicator.IsIndeterminate = false

SystemTray.ProgressIndicator.IsVisible = false;

如果它在mvvm然后我怎么做?

通常,MVM模式中的ViewModel和View之間的交互通過數據綁定發生,並且在某些特殊情況下通過使用Messanger模式的消息發生。 使用數據綁定可以在一般場景中適應這種情況。 在viewmodel中創建屬性以指示正在進行的工作,例如IsBusy 將ProgressIndicator IsIndeterminateIsVisible屬性綁定到IsBusy

<shell:SystemTray.ProgressIndicator>
    <shell:ProgressIndicator IsIndeterminate="{Binding IsBusy}" 
                             IsVisible="{Binding IsBusy}" 
                             Text="{Binding Message}" />
</shell:SystemTray.ProgressIndicator>

使用數據綁定,viewmodel中的代碼結構可以簡單如下:

IsBusy = true;

await MyFunction();

IsBusy = true;

....
private async void MyFunction()
{
    (..my logic code..)
}

請查看此SO答案以獲取進一步參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM