c# – Caliburn Micro,如何使用ViewModel优先使用ContentControl(或显示’sub’ViewModel)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – Caliburn Micro,如何使用ViewModel优先使用ContentControl(或显示’sub’ViewModel),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3348字,纯文字阅读大概需要5分钟。
内容图文
![c# – Caliburn Micro,如何使用ViewModel优先使用ContentControl(或显示’sub’ViewModel)](/upload/InfoBanner/zyjiaocheng/796/0d9f546cc2294ee8b8017a85c44225ac.jpg)
我首先使用ViewModel在我的应用程序中使用MVVM框架Caliburn Micro(或者我认为).但是,当我在使用TryClose(true)的对话框出现问题时,无法关闭它的父窗口并偶然发现这个完全概述我的问题的问题,我也得到了“TryClose需要父IConductor或带有Close方法的视图”或IsOpen属性.“:
Caliburn.Micro – ShowDialog() how to close the dialog?
但是,我不确定如何实施该解决方案.答案说明:
Remove the cal:Bind.Model and cal:View.Model bindings…
原来使用这些绑定是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来说,它只是一个包含okay和取消按钮的包装器(谁的视图由caliburn解析,因此我认为我首先在做ViewModel).如果我删除此cal:View.Model绑定我确实恢复了关闭我的对话框的能力,但我放弃了所有实际内容.我正在使用ContentControl来显示我的应用程序(在ItemsControls,对话框,弹出窗口等).
我的问题是,我应该如何在ViewModel首次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);
}
}
解决方法:
回答主要问题
how should I be displaying a ViewModel in a ViewModel first Caliburn?
我假设TimeSpanViewModel是ViewModel上的一个属性,它具有[Import](并且ViewModel正在导出自己)?我认为你应该改变cal:View.Model =“{Binding TimeSpanViewModel}”到x:Name =“TimeSpanViewModel”.即使这可能无法解决问题,但这是正确的做法,Caliburn将确保它正确绑定.
我试图重现你的问题,但即使用你的方式它也适合我.那么为什么它不能按照你目前的方式工作,这是一个很好的(第二个)问题.
最大的问题可能是你的AppWindowManager,如果你在那里创建的窗口没有通过正确的Caliburn代码,它将无法正确绑定.由于缺少很多代码,我甚至不确定AppViewModel Content {get;组;在那里做,大多数我可以推测.您是否尝试使用默认的WindowManager实现,只是为了查看它是否可以使用它?
内容总结
以上是互联网集市为您收集整理的c# – Caliburn Micro,如何使用ViewModel优先使用ContentControl(或显示’sub’ViewModel)全部内容,希望文章能够帮你解决c# – Caliburn Micro,如何使用ViewModel优先使用ContentControl(或显示’sub’ViewModel)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。