我有ViewModel1
和View1相关联。我使用ViewModel2
对象从IWindowManager
(另一个视图模型)启动对话框窗口。来自ViewModel2
class的代码:
windowManager.ShowDialog(new ViewModel());
所以,我有一个带View1用户控件的Dialog Window。
我的答案是下一个 - 我可以使用红色关闭按钮关闭该对话框窗口,但是如何使用我的特定按钮(包含在View1
用户控件中)关闭它,类似于“取消”按钮和关闭命令( Command={Binding CancelCommand}
),CancelCommand
当然包含在ViewModel1
类中。
答案 0 :(得分:38)
如果您的视图模型扩展Caliburn.Micro.Screen
,那就更容易了:
TryClose();
答案 1 :(得分:10)
您可以通过在ViewModel上实现IViewAware
界面来获取当前视图(在您的情况下是对话框窗口)。然后,您可以在执行命令时在视图(作为对话框创建的Close
)上调用Window
。
最简单的原因是派生自ViewAware
:
public class DialogViewModel : ViewAware
{
public void ExecuteCancelCommand()
{
(GetView() as Window).Close();
}
}
如果您不被允许派生,您可以自己实施:
public class DialogViewModel : IViewAware
{
public void ExecuteCancelCommand()
{
dialogWindow.Close();
}
private Window dialogWindow;
public void AttachView(object view, object context = null)
{
dialogWindow = view as Window;
if (ViewAttached != null)
ViewAttached(this,
new ViewAttachedEventArgs(){Context = context, View = view});
}
public object GetView(object context = null)
{
return dialogWindow;
}
public event EventHandler<ViewAttachedEventArgs> ViewAttached;
}
注意:我已将Caliburn.Micro 1.3.1用于我的样本。
答案 2 :(得分:5)
我使用的一种更清洁的方式(个人品味的主题)是使用IResult模式,这样就抽象了Window实现
视图模型
public IEnumerable<IResult> CloseMe()
{
yield return new CloseResult();
}
结果代码
public class CloseResult : Result
{
public override void Execute(ActionExecutionContext context)
{
var window = Window.GetWindow(context.View);
window.Close();
base.Execute(context);
}
}
public abstract class Result : IResult
{
public virtual void Execute(ActionExecutionContext context)
{
OnCompleted(this, new ResultCompletionEventArgs());
}
protected virtual void OnCompleted(object sender, ResultCompletionEventArgs e)
{
if (Completed != null)
Completed(sender, e);
}
public event EventHandler<ResultCompletionEventArgs> Completed;
}
编辑(仅适用于IoC):如果您想更进一步,请为所有屏幕执行基类
public abstract class ShellPresentationModel : Screen
{
public ShellPresentationModel(IResultFactory resultFactory)
{
Result = resultFactory;
}
public IResultFactory Result { get; private set; }
}
通过这种方式,您可以更轻松地为IoC注入依赖项,然后您的VIewmodel close方法将如下所示
public IEnumerable<IResult> CloseMe()
{
yield return Result.Close();
}
使用依赖关系的IResult的示例可以是
public class ShowDialogResult<TModel> : Result
{
private readonly IWindowManager windowManager;
private readonly TModel model;
private Action<TModel> configure;
public ShowDialogResult(IWindowManager windowManager, TModel model)
{
this.windowManager = windowManager;
this.model = model;
}
public IResult Configure(Action<TModel> configure)
{
this.configure = configure;
return this;
}
public override void Execute(ActionExecutionContext context)
{
if(configure != null)
configure(model);
windowManager.ShowDialog(model);
base.Execute(context);
}
}
编辑注意到我忘记添加上述IoC exmaple的示例, 使用子IoC容器模式,它看起来像这样
public IEnumerable<IResult> ShowDialog()
{
yield return Result.ShowDialog<MyViewModel>();
}
如果没有子容器模式,您需要手动将父依赖同步注入子项
yield return Result.ShowDialog<MyViewModel>().Configure(m => m.SomeData = this.SomeData);