2011-10-10 61 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); 
    }; 
} 
相关问题