如何将自定义 Window 与 Caliburn.Micro 2 一起使用?
How to use a custom Window with Caliburn.Micro 2?
我想使用我自己的 class 扩展 Window 在使用 Caliburn.Micro 的 MVVM 环境中显示对话框。
我已经阅读了如何通过覆盖 中的 EnsureWindow 方法来自定义 CM 提供的 windowWindowManager,或者通过访问我的视图模型中的默认 WindowManager 实例并将设置字典传递给 "ShowDialog" 方法.尽管如此,我真正需要的是使用我自己的class,因为它包含其他元素,无法通过简单地设置一些属性来提供给默认window。
为清楚起见,我可以使用默认的 Window class 作为我的根视图。
这可能吗?如果我的问题不知何故没有意义,我会很乐意扩展我的理由...
提前感谢社区!
根据@mvermef 的建议,我做了一些研究并提出了解决方案。它包括覆盖 WindowManager class 的 EnsureWindow 方法。这是一个例子:
protected override Window EnsureWindow(object model, object view, bool isDialog)
{
var window = view as Window;
if (window == null)
{
window = new MyCustomWindowClass
{
SizeToContent = SizeToContent.WidthAndHeight
};
// I defined a ContentControl "WindowContent" in MyCustomWindow
// class to insert the window's contents
((MyCustomWindowClass)window).WindowContent.Content = view;
window.SetValue(View.IsGeneratedProperty, true);
var owner = InferOwnerOf(window);
if (owner != null)
{
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.Owner = owner;
}
else
{
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
}
else
{
var owner = InferOwnerOf(window);
if (owner != null && isDialog)
{
window.Owner = owner;
}
}
return window;
}
我想使用我自己的 class 扩展 Window 在使用 Caliburn.Micro 的 MVVM 环境中显示对话框。
我已经阅读了如何通过覆盖 中的 EnsureWindow 方法来自定义 CM 提供的 windowWindowManager,或者通过访问我的视图模型中的默认 WindowManager 实例并将设置字典传递给 "ShowDialog" 方法.尽管如此,我真正需要的是使用我自己的class,因为它包含其他元素,无法通过简单地设置一些属性来提供给默认window。
为清楚起见,我可以使用默认的 Window class 作为我的根视图。
这可能吗?如果我的问题不知何故没有意义,我会很乐意扩展我的理由...
提前感谢社区!
根据@mvermef 的建议,我做了一些研究并提出了解决方案。它包括覆盖 WindowManager class 的 EnsureWindow 方法。这是一个例子:
protected override Window EnsureWindow(object model, object view, bool isDialog)
{
var window = view as Window;
if (window == null)
{
window = new MyCustomWindowClass
{
SizeToContent = SizeToContent.WidthAndHeight
};
// I defined a ContentControl "WindowContent" in MyCustomWindow
// class to insert the window's contents
((MyCustomWindowClass)window).WindowContent.Content = view;
window.SetValue(View.IsGeneratedProperty, true);
var owner = InferOwnerOf(window);
if (owner != null)
{
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.Owner = owner;
}
else
{
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
}
else
{
var owner = InferOwnerOf(window);
if (owner != null && isDialog)
{
window.Owner = owner;
}
}
return window;
}