當然 - 這裏有兩種方法供您考慮。
選項1
棱鏡的documentation對 「製作命令全局可用」 使用他們的CompositeCommand類的部分。要做到這一點,創建一個公共圖書館公共靜態類,這兩個組件可以參考:
public static class GlobalCommands
{
public static CompositeCommand ShowFinanceFormCommand = new CompositeCommand();
}
在財務表格視圖模型,你就註冊您的實際命令(其中包含所有邏輯顯示形式)與:
public FinanceViewModel()
{
GlobalCommands.ShowFinanceFormCommand.RegisterCommand(this.ShowFinancePopupCommand);
}
在員工視圖模型,綁定你的按鈕的ICommand
到ShowFinanceFormCommand
複合命令。
public EmployeeViewModel()
{
this.EmployeeShowFinanceFormCommand = GlobalCommands.ShowFinanceFormCommand;
}
選項2
如果您需要在廣播鬆散耦合的方式跨模塊的事件,使用棱鏡的EventAggregator類。要實現這一點,請在通用組件中創建一個ShowFinanceFormEvent
,這兩個組件都可以引用。在員工視圖模型中,按下按鈕時發佈事件。在財務視圖模型中,訂閱該事件並作出相應的反應。
// This sits in a separate module that both can reference
public class ShowFinanceFormEvent : PubSubEvent<object>
{
}
public class EmployeeViewModel
{
private readonly IEventAggregator eventAggregator;
public EmployeeViewModel(IEventAggregator eventAggregator)
{
this.ShowFormCommand = new DelegateCommand(RaiseShowFormEvent);
this.eventAggregator = eventAggregator;
}
public ICommand ShowFormCommand { get; }
private void RaiseShowFormEvent()
{
// Notify any listeners that the button has been pressed
this.eventAggregator.GetEvent<ShowFinanceFormEvent>().Publish(null);
}
}
public class FinanceViewModel
{
public FinanceViewModel(IEventAggregator eventAggregator)
{
// subscribe to button click events
eventAggregator.GetEvent<ShowFinanceFormEvent>().Subscribe(this.ShowForm);
this.ShowFinanceFormRequest = new InteractionRequest<INotification>();
}
public InteractionRequest<INotification> ShowFinanceFormRequest { get; }
private void ShowForm(object src)
{
// Logic goes here to show the form... e.g.
this.ShowFinanceFormRequest.Raise(
new Notification { Content = "Wow it worked", Title = "Finance form title" });
}
}
感謝大AL的詳細解答和文檔鏈接。目前我已經實施了選項2(事件彙總),閱讀了「解決方案指揮」,並將實施最終解決方案的策略(按照建議並適合我的策略和需求)。 – Alice