我需要在首次打开时设置视图的默认大小,但视图必须允许用户展开它。 (由于其他原因,我无法在WindowManager中使用SizeToContent属性。)
这一定是常见的,设置默认窗口大小的推荐方法是什么?
答案 0 :(得分:9)
这实际上已经让我误解了一段时间。一旦我弄清楚了,我就惹恼了我,我没有早点弄明白。
在caliburn中显示窗口时,可以在调用时设置有关Window对象的属性。
因此,假设您要将窗口的高度和宽度设置为600 x 300:
首先,你会从这样的事情开始:
public class ShellViewModel : PropertyChangedBase, IShell
{
private readonly IWindowManager windowManager;
public ShellViewModel()
{
this.windowManager = new WindowManager();
this.windowManager.ShowWindow(new LameViewModel());
}
}
ShowWindow方法还有另外两个字段。第三个参数允许您在Window对象上动态设置属性。
public class ShellViewModel : PropertyChangedBase, IShell
{
private readonly IWindowManager windowManager;
public ShellViewModel()
{
this.windowManager = new WindowManager();
dynamic settings = new ExpandoObject();
settings.Height = 600;
settings.Width = 300;
settings.SizeToContent = SizeToContent.Manual;
this.windowManager.ShowWindow(new LameViewModel(), null, settings);
}
}
我希望有更多关于在文档上使用此信息的信息,但是你有它。
答案 1 :(得分:5)
在发布此帖子时不确定是否适用,但对于现在关注的任何人,您可以在app bootstrapper中轻松设置CaliburnMicro中的窗口大小。我的代码设计为在启动时保留与之前关闭时相同的窗口尺寸。我将屏幕高度/宽度保存为关闭时的设置属性,然后在启动时将其恢复回来(检查以确保不大于当前屏幕大小)。
<强> AppBootstrapper.cs 强>
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e) {
double width = Settings.Default.screen_width; //Previous window width
double height = Settings.Default.screen_height; //Previous window height
double screen_width = System.Windows.SystemParameters.PrimaryScreenWidth;
double screen_height = System.Windows.SystemParameters.PrimaryScreenHeight;
if (width > screen_width) width = (screen_width - 10);
if (height > screen_height) height = (screen_height-10);
Dictionary<string, object> window_settings = new Dictionary<string, object>();
window_settings.Add("Width", width);
window_settings.Add("Height", height);
DisplayRootViewFor<IShell>(window_settings);
}
答案 2 :(得分:3)
不确定这是否是推荐的方法,但不是引导/显示UserControl
,而是可以引导/显示Window
,然后设置高度,宽度等。
答案 3 :(得分:2)
以下是基于休的回答的答案:
Dictionary<string, object> window_settings = new Dictionary<string, object>();
window_settings.Add("WindowState", System.Windows.WindowState.Maximized);
DisplayRootViewFor<IShellViewModel>(window_settings);
答案 4 :(得分:0)
您还可以在引导程序中获取IWindowManager的实例,并在其中设置RootView的初始大小。这是基于GrantByrne的答案。
public class CustomBootstrapper : BootstrapperBase
{
public CustomBootstrapper()
{
Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
var windowManager = IoC.Get<IWindowManager>();
MainViewModel mainViewModel = IoC.Get<MainViewModel>();
dynamic settings = new ExpandoObject();
settings.Height = 500;
settings.Width = 800;
settings.SizeToContent = SizeToContent.Manual;
windowManager.ShowWindow(mainViewModel, null, settings);
}
}