wpf 在 Caliburn.micro 中设置初始窗口大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16404538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the initial window size in Caliburn.micro
提问by Kelly
I need to set the default size of a view when it first opens, but the view must allow for the user to expand it. (For other reasons I can't use the SizeToContent property in my WindowManager.)
我需要在第一次打开时设置视图的默认大小,但视图必须允许用户展开它。(由于其他原因,我无法在 WindowManager 中使用 SizeToContent 属性。)
This must be a common thing, what is the recommended approach to setting the default window size?
这一定是很常见的事情,设置默认窗口大小的推荐方法是什么?
采纳答案by GrantByrne
This is something that has actually bugged me for a while. Once I figured it out, it annoyed me that I didn't figure it out sooner.
这确实困扰了我一段时间。一旦我弄清楚了,我很生气,因为我没有早点弄清楚。
When displaying a window in caliburn, you can set attributes about the Window object when calling it.
在caliburn中显示窗口时,可以在调用时设置Window对象的属性。
So, lets say you want to set the height and width on the window to 600 x 300:
因此,假设您要将窗口的高度和宽度设置为 600 x 300:
First, you would start with something like this:
首先,你会从这样的事情开始:
public class ShellViewModel : PropertyChangedBase, IShell
{
private readonly IWindowManager windowManager;
public ShellViewModel()
{
this.windowManager = new WindowManager();
this.windowManager.ShowWindow(new LameViewModel());
}
}
There are two other fields on the ShowWindow method. The third parameter lets you dynamically set the attributes on the Window object.
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);
}
}
I wish there was more information about working with this on the documentation, but there you have it.
我希望在文档中有更多关于使用这个的信息,但你已经知道了。
回答by Hugh
Not sure if this applies at the time this post was made, but for anyone following now this is how you can easily set the window size in CaliburnMicro in the app bootstrapper. My code is designed to retain the same window dimensions on startup as at previous closedown. I save the screen height/width as setting properties on closedown and then retrieve them back on startup here (with a check to ensure not greater than current screen size).
不确定这在发布这篇文章时是否适用,但对于现在关注的任何人来说,这是您可以在应用程序引导程序中轻松设置 CaliburnMicro 窗口大小的方法。我的代码旨在在启动时保留与上次关闭时相同的窗口尺寸。我将屏幕高度/宽度保存为关闭时的设置属性,然后在此处启动时检索它们(检查以确保不大于当前屏幕尺寸)。
AppBootstrapper.cs
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);
}
回答by Hack
Not sure if it's the recommended approach but instead of bootstrapping/showing a UserControlyou can bootstrap/show a Windowinstead and then set Height, Width, etc.
不确定这是否是推荐的方法,但不是引导/显示 aUserControl您可以引导/显示 aWindow然后设置高度、宽度等。
回答by FindOutIslamNow
Here is the answer based on Hugh's answer:
以下是基于休回答的答案:
Dictionary<string, object> window_settings = new Dictionary<string, object>();
window_settings.Add("WindowState", System.Windows.WindowState.Maximized);
DisplayRootViewFor<IShellViewModel>(window_settings);

