【发布时间】:2016-06-01 08:32:50
【问题描述】:
我首先在我的应用程序中使用 MVVM 框架 Caliburn Micro,并使用 ViewModel(或者我是这么认为的)。但是,当我在使用TryClose(true) 无法关闭其父窗口的对话框中遇到问题并偶然发现这个完美概述了我的问题的问题时,我也得到了“TryClose 需要父 IConductor 或带有 Close 方法的视图或IsOpen 属性。”:
Caliburn.Micro - ShowDialog() how to close the dialog?
但是,我不确定如何实施该解决方案。答案是:
移除 cal:Bind.Model 和 cal:View.Model 绑定...
原来使用这些绑定是一种 View-First 方法,我不知道我正在这样做。这是我的冒犯对话的示例:
<UserControl ... Height="206" Width="415">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="AUTO" />
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="Okay" Content="Okay" Width="100" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Button x:Name="Cancel" Content="Cancel" Width="100" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<ContentControl cal:View.Model="{Binding TimeSpanViewModel}"/>
</Grid>
</UserControl>
它只是一个包装器,带有一个用于已经存在的 ViewModel 的 OK 和 cancel 按钮(谁的视图由 caliburn 解决,因此我认为我首先在做 ViewModel)。如果我删除这个cal:View.Model 绑定,我确实恢复了关闭对话框的能力,但我失去了所有实际内容。我正在使用 ContentControl 来显示整个应用程序中的内容(在 ItemsControls、对话框、弹出窗口等中)。
我的问题是,应该如何首先在 Caliburn 中显示 ViewModel?
编辑:我正在使用 WindowManager 显示 DialogViewModel(继承屏幕),如下所示:
[Export(typeof(IWindowManager))]
public class AppWindowManager : MetroWindowManager, IDialogManager
{
AppViewModel Content { get; set; }
public AppWindowManager()
{
}
public override MetroWindow CreateCustomWindow(object view, bool windowIsView)
{
if (windowIsView)
{
return view as MainWindowContainer;
}
MainWindowContainer window = new MainWindowContainer();
//{
window.Content = view;
//};
return window;
}
public override bool? ShowDialog(object rootModel, object context = null, IDictionary<string, object> settings = null)
{
Window window = CreateWindow(rootModel, true, context, settings);
return window.ShowDialog();
}
public object ShowCustomDialog(object rootModel, string title, bool showWindowsOptions = true)
{
dynamic settings = new ExpandoObject();
settings.Title = title;
settings.ShowCloseButton = showWindowsOptions;
settings.ShowMaxRestoreButton = showWindowsOptions;
settings.ShowMinButton = showWindowsOptions;
settings.SizeToContent = SizeToContent.WidthAndHeight;
return ShowDialog(rootModel, null, settings);
}
public ILoadingDialogViewModel CreateLoadingDialogManager()
{
return new LoadingDialogViewModel(this);
}
}
【问题讨论】:
-
或许您应该添加更多详细信息:您是否使用
conductor来显示您的对话框?TryClose有什么问题?您是否看到另一个问题的相同信息? -
这是视图模型,因为您没有使用用户控件来指定视图,而是基于视图模型将视图加载到内容控件中。如果您提供要绑定/创建视图模型到/用于的用户控件,则只有视图优先。您的子视图模型是否有 candeactivate 挂钩?
-
子 (TimeSpanViewModel) 继承 PropertyChangedBase 并且托管它的对话框 ViewModel(查看上面的代码)实现 Screen。我没有接触过 caliburn 中的停用钩子。
-
@IlVic 我从 caliburn 收到相同的日志消息:“TryClose 需要父 IConductor 或具有 Close 方法或 IsOpen 属性的视图。”我没有显示带有导体的 ViewModel,而是在使用 WindowManager 的窗口中显示(如果有任何帮助,我已将该代码添加到问题中)
标签: c# wpf mvvm caliburn.micro