在Caliburn.micro中设置初始窗口大小

12

我需要在视图首次打开时设置默认大小,但视图必须允许用户扩展它。(由于其他原因,我不能在我的WindowManager中使用SizeToContent属性。)

这应该是一个常见的问题,设置默认窗口大小的推荐方法是什么?


我能想到的唯一可行的方法是在视图中设置特定大小,然后在视图加载后通过将大小设置为自动来释放视图,以确保窗口大小适合内容。这可能是您在视图上设置的一种行为。 - Charleh
5个回答

13

这是一件让我困扰已久的事情。当我找到答案后,我很生气没有早点找到答案。

在使用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);
    }
}

我希望文档中有更多关于使用这个的信息,但就是这样。


2
你可能需要添加另一个设置:SizeToContent = SizeToContent.Manual - Govert

6

不确定这篇文章发表时是否适用,但是对于现在关注此问题的任何人来说,以下是您如何在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);
    }

1
如果您使用 WindowState = Maximized,请设置 MaxHeight 以确保窗口不会超过任务栏。 - CularBytes

3

不确定是否为推荐的方法,但是可以不必引导/显示UserControl,而是引导/显示Window,然后设置高度、宽度等。


1
这是基于Hugh的答案的答案:

        Dictionary<string, object> window_settings = new Dictionary<string, object>();
        window_settings.Add("WindowState", System.Windows.WindowState.Maximized);

        DisplayRootViewFor<IShellViewModel>(window_settings);

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);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接