如何使用MainWindow作为ShellViewModel View? [英] How to use MainWindow as ShellViewModel View?
问题描述
我了解默认情况下,CM会在Views文件夹中寻找ShellView用作ShellViewModel View,但我想改用MainWindow ...这可以做到吗?
I understand that by default CM will look for ShellView in Views folder to use as ShellViewModel View but I want to use the MainWindow instead... can this be done and how?
推荐答案
工作原理
CM使用一组 View / ViewModel命名约定 ,通常来说,如果您有一个名为<$ c $的ViewModel c> FooViewModel CM将尝试查找名称与 FooView
或 FooPage
。
How it Works
CM uses a set of View/ViewModel Naming Conventions, generally speaking, if you have a ViewModel named FooViewModel
CM will attempt to locate a type with a similar name of FooView
or FooPage
.
如果您只是想将现有的 MainWindow与现有的 root viewmodel一起使用,则可以考虑对 Bootstrapper< TRootModel>
进行子类化,并覆盖 OnStartUp
。这是一种规定的方法,但似乎令人生畏。
If you just wanted to use an existing "MainWindow" with an existing 'root viewmodel' then consider subclassing Bootstrapper<TRootModel>
and override OnStartUp
. This is a prescribed method, but can seem daunting.
(我尚未测试此代码。)
protected override void OnStartup(object sender, StartupEventArgs e)
{
var rootModel = IoC.Get<TRootModel>();
var rootView = new MainWindow();
ViewModelBinder.Bind(rootModel, rootView, this);
rootView.Show();
}
当然,上述方法仅适用于启动期间显示的根视图模型。不确定将来是否可以尝试显示 ShellViewModel
的视图,否则可能会导致错误。
The above method, of course, would only apply to the initial view for the root view model shown during start-up. Future attempts to display a view for ShellViewModel
may work, or they may result in errors, I am not certain.
有几种方法可以自定义约定本身。最灵活和直接的方法是拦截/挂钩 Caliburn.Micro.ViewLocator.LocateForModelType
,这使您可以修改在视图定位期间应用的行为/策略。
There are a few ways to customize the convention itself. The most flexible and direct method is to intercept/hook Caliburn.Micro.ViewLocator.LocateForModelType
, this allows you to modify the behavior/strategy applied during view location.
private static void CustomViewLocatorStrategy()
{
// store original implementation so we can fall back to it as necessary
var originalLocatorStrategy = Caliburn.Micro.ViewLocator.LocateForModelType;
// intercept ViewLocator.LocateForModelType requests and apply custom mappings
Caliburn.Micro.ViewLocator.LocateForModelType = (modelType, displayLocation, context) =>
{
// implement your custom logic
if (modelType == typeof(ShellViewModel))
{
return new MainWindow();
}
// fall back on original locator
return originalLocatorStrategy(modelType, displayLocation, context);
};
}
以上内容可以从 Bootstrapper < TRootModel> .Configure
覆盖:
protected override void Configure()
{
CustomViewLocatorStrategy();
base.Configure();
}
此方法更适合与CM配合使用(无论从哪种角度来看
This method is more likely to play well with CM (in terms of any view caching, namely.) However, it still breaks conventions, and it's still a fair amount of code.
我想指出的但没有机会玩的一件事是 ViewLocator.RegisterViewSuffix
实现。我相信,如果您执行 ViewLocator.RegisterViewSuffix(@ Window)
,则可以依靠CM将 MainViewModel
映射到 MainWindow
。
One thing I want to point out, but have not had a chance to play with, is ViewLocator.RegisterViewSuffix
implementation. I believe if you executed ViewLocator.RegisterViewSuffix(@"Window")
then you could rely on CM to map MainViewModel
to MainWindow
.
这将允许使用更具表现力的后缀(例如Window,Dialog,Form或其他您可以我个人不喜欢使用视图作为后缀,我认为它太通用了(毕竟它们都是视图。)
This would allow for more expressive suffixes (such as Window, Dialog, Form, or others you may want to use.) Personally I dislike the use of 'View' as a suffix, I believe it's too generic (after all, they are all Views.)
这篇关于如何使用MainWindow作为ShellViewModel View?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!