2012-02-21 99 views
0

任何人都可以使用mef和mvvm模式給我一個簡單的wpf應用程序。我在網上看了很多,但是我發現很少的例子,這些例子非常複雜。一些例子是wpf,mef但不遵循mvvm模式。如何在我的MVVM應用程序中利用MEF?

這是我想要做的。

會有一個主要的wpf應用程序。這將加載所有模塊(插件)並將其顯示給用戶。一個模塊將包含2個或3個頁面,並具有下一個後退按鈕(用於導航)。現在當我在主應用程序中選擇一個模塊,模塊打開或者你可以說它取代了當前窗口,並在按鈕導航上它改變了模塊的視圖。

主窗口 - >模塊1 - >第1頁

     Page 2 

         Page 3 

      Module 2 -> Page 1 

         Page 2 

         Page 3 

所以現在看起來很乾淨。模塊1是一個獨立的項目,模塊2是一個獨立的項目。主窗口從dll讀取模塊並顯示它們。點擊一個模塊將通過它的頁面。

+0

?它更好地問一個不那麼有問題的問題。 – blindmeis 2012-02-21 12:50:20

+0

@blindmeis我已經更新了這個問題,現在你可以看到它。我希望它很清楚。 – 2012-02-21 13:07:39

+0

現在明確:) – blindmeis 2012-02-21 13:54:42

回答

3

微軟有一些參考實現,可能會發現有用。他們是MVVM/MEF和WPF的一個很好的展示。 here's是第一個關於StockTrader RI實施的博客,下載鏈接指向here。 StockTrader RI和MVVM RI實現的Microsoft概述是專門用於StockTrader RI實現的herehere's。心連心。

+0

'該示例應用程序演示了在雲端運行的端到端面向服務的體系結構中使用ASP.NET和Windows Communication Foundation(WCF)技術。因此,該應用程序演示了Windows Azure平臺,SQL Azure,ASP.NET和WCF的許多最佳實踐編程實踐,包括使用n層,面向服務的設計。「錯誤的鏈接? – Bolu 2012-02-21 13:02:19

+0

它真的使用wpf,mvvm,mef。這是我認爲的一個雲應用程序。糾正我,如果我錯了。 – 2012-02-21 13:11:16

+0

我知道你正在談論的應用程序...我認爲它在安裝PRISM時會被安裝。這對MEF來說是一個很好的開始。如果你能找到鏈接,我會給你一個+1 :) – Rachel 2012-02-21 13:35:43

3

我做了2個項目,這些做你想做的。我所做的就是設置一個主要項目,它不過是通過MEF收集模塊並處理模塊的選擇。您還需要一個組件項目,您可以在其中設置接口和導出屬性。

這裏一些示例代碼

MainProject app.xaml.cs

public partial class App : Application 
{ 
    [ImportMany] 
    private IEnumerable<Lazy<IComponent, IComponentMetadata>> _components; 
    private CompositionContainer _mefcontainer; 

    protected override void OnStartup(StartupEventArgs e) 
    { 
     ShutdownMode = ShutdownMode.OnExplicitShutdown; 

     //i do login stuff here 

     //i use task.factory here and dynamic splashscreen here 

     this.MefContainer.ComposeParts(rahmen, this); 

     foreach (var component in _components) 
     { 
      //check metadata and fill modules collection 
     } 

     //add modules collection to mainwindowviewmodel 

     ShutdownMode = ShutdownMode.OnMainWindowClose; 

     this.MainWindow.Show(); 

    } 
} 

Component.dll

public interface IComponent//Marker interface 
{ 
    bool HasChanges { get; } 
} 

public interface IComponentMetadata 
{ 
    string Displayname { get; } 

    int SortIndex { get; } 

    string ImagePath { get; } 

    string IconPath { get; } 
} 

[MetadataAttribute] 
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] 
public class ComponentExportAttribute : ExportAttribute, IComponentMetadata 
{ 
    private const string DEFAULTICON = "pack://application:,,,/MyComponent;Component/Images/Default.png"; 


    public ComponentExportAttribute(string displayname, int sortindex): base(typeof(IComponent)) 
    { 
     this.Displayname = displayname; 
     this.SortIndex = sortindex; 

     this.ImagePath = DEFAULTICON; 
     this.IconPath = DEFAULTICON; 
    } 


    public ComponentExportAttribute(string displayname, int sortindex, string imagepath, string iconpath): base(typeof(IComponent)) 
    { 
     this.Displayname = displayname; 
     this.SortIndex = sortindex; 

     this.ImagePath = String.IsNullOrWhiteSpace(imagepath) ? DEFAULTICON : imagepath; 
     this.IconPath = String.IsNullOrWhiteSpace(iconpath) ? DEFAULTICON : iconpath; 
    } 

    #region Implementation of IComponentMetadata 

    public string Displayname { get; private set; } 

    public int SortIndex { get; private set; } 

    public string ImagePath { get; private set; } 

    public string IconPath { get; private set; } 

    #endregion 
} 

MODUL例如

[ComponentExport("Test1", 150 
    , "pack://application:,,,/TestProject;Component/Test/Logo1.png" 
    , "pack://application:,,,/TestProject;Component/Test/Icon1.png")] 
public partial class Test1MainWindow : UserControl, IComponent 
{ 
    [ImportingConstructor]//if you want to do DI 
    public Test1MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = this; 
    } 

    #region Implementation of IComponent 

    public bool HasChanges 
    { 
     get { return false; } 
    } 

    #endregion 
} 

或視圖模型導出 - >(如果你這樣做,你必須一個DataTemplate導出到mainapp,我可以證明這一點,如果你想)

[ComponentExport("Test2", 500 
    , "pack://application:,,,/TestProject;Component/Test/Logo2.png" 
    , "pack://application:,,,/TestProject;Component/Test/Icon2.png")] 
public class Test2: INPCBase, IComponent 
{ 

    [ImportingConstructor] 
    public Test2() 
    { 
    } 

    #region Implementation of IKabuComponent 

    public bool HasChanges 
    { 
     get { return false; } 
    } 

    #endregion 
} 
要與MEF,WPF和MVVM創建什麼樣的應用
+0

您可以在某處發佈解決方案嗎?像skydrive或mediafire。 – 2012-02-22 06:00:27

相關問題