2015-12-19 47 views
0

我有一個帶區域的菜單區域和工作區域的WPF PRISM應用程序。當單擊菜單時,在工作區域內打開適當的視圖/表單。我有不同的模塊,如員工,組織,財務等......每個模塊都有多個視圖/表單。調用不同模塊的WPF PRISM對話窗口

我有一個要求,當點擊員工視圖/表單(模塊 - 員工)中的按鈕時,從財務模塊打開一個視圖/表單作爲對話窗口。

有沒有辦法在PRISM中實現相同?

[編輯1]:這兩個模塊是獨立的模塊。我不希望將Finance模塊的參考添加到Employee模塊。更進一步,我可能需要將Employee顯示爲來自一個或多個Finance視圖的對話窗口。

回答

1

當然 - 這裏有兩種方法供您考慮。

選項1

棱鏡的documentation對 「製作命令全局可用」 使用他們的CompositeCommand類的部分。要做到這一點,創建一個公共圖書館公共靜態類,這兩個組件可以參考:

public static class GlobalCommands 
{ 
    public static CompositeCommand ShowFinanceFormCommand = new CompositeCommand(); 
} 

在財務表格視圖模型,你就註冊您的實際命令(其中包含所有邏輯顯示形式)與:

public FinanceViewModel() 
{ 
    GlobalCommands.ShowFinanceFormCommand.RegisterCommand(this.ShowFinancePopupCommand); 
} 

在員工視圖模型,綁定你的按鈕的ICommandShowFinanceFormCommand複合命令。

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" }); 
    } 
} 
+0

感謝大AL的詳細解答和文檔鏈接。目前我已經實施了選項2(事件彙總),閱讀了「解決方案指揮」,並將實施最終解決方案的策略(按照建議並適合我的策略和需求)。 – Alice

1

在我看來,選項3和更好的選項只是使用Prism的導航框架。如果您需要顯示對話框,請使用對話服務,並將視圖鍵傳遞給服務以確定在對話框中顯示哪個視圖。由於您使用的是唯一鍵而不是引用,因此您不必知道或關心視圖的存在位置。

我使用類似的方法在我這裏PluralSight課程:

https://app.pluralsight.com/library/courses/prism-showing-multiple-shells/table-of-contents

退房對話框服務演示。

+0

我有使用Pluralsight的msdn訂閱,但您的課程不包含在允許的課程中:'( – Younes