2011-10-10 25 views
1

綁定視圖我建立需要主題支持的應用程序。所以我想提供視圖文件夾運行時間。caliburn.micro如何加載和運行時的ViewModel

public class AppBootstrapper : Bootstrapper<IShell> 
{ 
    CompositionContainer _container; 

    /// <summary> 
    /// By default, we are configure to use MEF 
    /// </summary> 
    protected override void Configure() 
    { 
     //view locator code get views from file and and binding it to viewmodel run time. 
    } 
} 
+0

所以,你想出貨幾個不同的DLL,每一個都有自己的你的意見的副本,並在運行時你會決定加載哪一個?如果沒有,你需要澄清你的意思是「供應視圖文件夾運行時間」。 –

+0

@JoeWhite我只想在xaml中提供視圖。我不會放入dll。所以當程序啓動時,它應該加載來自xaml文件的所有視圖。 –

+0

你沒有提供足夠的細節。動態啓動後是否需要切換主題?或者當你的應用程序重新啓動時?這將決定你需要採取哪條路線。 – jonathanpeppers

回答

3

在卡利,您可以創建自定義IConventionManager或tweek實施(DefaultConventionManager)來改變框架發現搜索文件夾在運行時的方式。

事實上觀點,並不必然是在瀏覽文件夾,因爲這僅僅是默認的公約,你可以修改這個默認行爲。實現這個接口的最好方法是檢查默認實現。

4

一個更好的調整是用這樣的方式(在卡利實現,但不是微之一)。 http://caliburnmicro.codeplex.com/discussions/265502

,首先您需要定義用於存儲用來發現視圖中的相關數據屬性:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)] 
public class ViewAttribute : Attribute 
{ 
    public object Context { get; set; } 

    public Type ViewType { get; private set; } 

    public ViewAttribute(Type viewType) 
    { 
     ViewType = viewType; 
    } 
} 

其附加到視圖模型。

[View(typeof(MyView))] 
public class MyViewModel : Screen 

然後,你需要在你的引導程序來改變LocateTypeForModelType到這樣的事情:

void Initialize() 
{ 
    var baseLocate = ViewLocator.LocateTypeForModelType; 

    ViewLocator.LocateTypeForModelType = (modelType, displayLocation, context) => 
    { 
     var attribute = modelType.GetCustomAttributes(typeof(ViewAttribute), false).OfType<ViewAttribute>().Where(x => x.Context == context).FirstOrDefault(); 
     return attribute != null ? attribute.ViewType : baseLocate(modelType, displayLocation, context); 
    }; 
} 
相關問題