我需要在视图首次打开时设置默认大小,但视图必须允许用户扩展它。(由于其他原因,我不能在我的WindowManager中使用SizeToContent属性。)
这应该是一个常见的问题,设置默认窗口大小的推荐方法是什么?
我需要在视图首次打开时设置默认大小,但视图必须允许用户扩展它。(由于其他原因,我不能在我的WindowManager中使用SizeToContent属性。)
这应该是一个常见的问题,设置默认窗口大小的推荐方法是什么?
这是一件让我困扰已久的事情。当我找到答案后,我很生气没有早点找到答案。
在使用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方法还有两个其他字段。第三个参数允许您动态设置窗口对象上的属性。
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);
}
}
我希望文档中有更多关于使用这个的信息,但就是这样。
SizeToContent = SizeToContent.Manual
。 - Govert不确定这篇文章发表时是否适用,但是对于现在关注此问题的任何人来说,以下是您如何在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);
}
WindowState = Maximized
,请设置 MaxHeight
以确保窗口不会超过任务栏。 - CularBytes不确定是否为推荐的方法,但是可以不必引导/显示UserControl
,而是引导/显示Window
,然后设置高度、宽度等。
Dictionary<string, object> window_settings = new Dictionary<string, object>();
window_settings.Add("WindowState", System.Windows.WindowState.Maximized);
DisplayRootViewFor<IShellViewModel>(window_settings);
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);
}
}