[英]How to make communicate asynchronous calls between ViewModel and Model
[英]Communicate between ViewModel and Command
我正在創建一個小型收銀員應用程序,我的CashViewModel按日期過濾銷售額 。
現在我添加了一個歷史按鈕來顯示按日期分組的銷售( 在一個窗口中 ),然后當用戶選擇日期我的Date屬性更改時,所以我將該按鈕綁定到RelayCommand。
public RelayCommand HistoryCommand
{
get
{
return _historyCommand
?? (_historyCommand = new RelayCommand(
() =>
{
//?????????
}));
}
}
我的問題是在回調Action中, 我不想直接從這里調用窗口並出於測試原因。
我應該使用Messaging(如果是這樣我應該創建一個消息接收器,還是有其他選項???)
您可以創建一個WindowService(它直接調用一個窗口),並將其注入視圖模型。
例如:
public interface IWindowService
{
Result ShowWindow(InitArgs initArgs);
}
public sealed class WindowService : IWindowService
{
public Result ShowWindow(InitArgs initArgs);
{
//show window
//return result
}
}
public class CashViewModel
{
private IWindowService m_WindowService;
public CashViewModel(IWindowService windowService)
{
m_WindowService = windowService;
}
public RelayCommand HistoryCommand
{
get
{
return _historyCommand
?? (_historyCommand = new RelayCommand(
() =>
{
var result = m_WindowService.ShowWindow(args);
}));
}
}
}
你可以在那里給出功能名稱。
private ICommand _historyCommand;
public ICommand HistoryCommand
{
get { return _historyCommand?? (_historyCommand= new RelayCommand(MyFunction)); }
}
private void MyFunction()
{
// Function do something.
}
您可以使用EventAggregator
實現Prism框架 。 它使您能夠在不了解發送方和/或接收方的情況下發送和接收事件。
當您收到相關事件時,您可以只執行相關代碼來顯示視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.