【问题标题】:WPF MVVM Re-center application on screen after height and width has changed?高度和宽度改变后,WPF MVVM在屏幕上重新居中应用程序?
【发布时间】:2019-11-15 13:12:13
【问题描述】:

更改应用程序的高度和宽度后,如何使应用程序在屏幕上居中?

我的 ShellViewModel 像这样处理 DashboardRequestEvent:

public void Handle(DashboardRequestEvent message)
        {
            ActivateItem(IoC.Get<DashboardViewModel>());
            Application.Current.MainWindow.Height = 600;
            Application.Current.MainWindow.Width = 800;
            // How to center the application here?
        }

我已经尝试了来自此链接的答案:How do you center your main window in WPF?,但没有一个以我的应用程序为中心。

【问题讨论】:

  • 也许您应该在窗口中使用激活/加载事件并更改那里的位置,因为在您尝试做的时候窗口可能还没有准备好处理 .top / .left 更新它在这段代码中。我已经使用了链接中的代码并且它可以工作,所以我猜你正在尝试在窗口可以处理它之前进行更新。
  • @KevinCook 我尝试了你的建议,但它仍然对我不起作用。它设法改变宽度和高度,但它永远不会使应用程序重新居中。
  • 放置一个调试器并查看 System.Windows.SystemParameters.PrimaryScreenWidth 设置为什么,以及何时设置了 .left。我无法重现您的问题,因为我无法确定在什么上下文中调用了您的“句柄”函数。
  • @KevinCook System.Windows.SystemParameters.PrimaryScreenWidth = 1920Left = 560。 ShellViewModel 正在侦听用户单击按钮时发布的事件。这个想法是,它将改变视图并调整它的大小。手柄会处理这个问题。
  • 所以窗口已经打开了,或者这个调用发生时窗口隐藏/未初始化?

标签: wpf mvvm caliburn.micro


【解决方案1】:

我使用 RelayCommand(我使用 mvvmlight)制作了一个快速视图模型,该命令与一个按钮单击相关联,该按钮单击可展开/收缩窗口并将其居中。

public class MoreJunk : ViewModelBase
{
    private bool HeightSet = false;
    private double oldHeight;
    private double oldWidth;

    public RelayCommand ResizeAndCenterWindowCommand { get; private set; }
    private void ResizeAndCenterWindow()
    {
        if (!HeightSet)
        {
            Application.Current.MainWindow.Height = 300;
            Application.Current.MainWindow.Width = 500;
            Application.Current.MainWindow.Top = ((SystemParameters.PrimaryScreenHeight - 300) / 2);
            Application.Current.MainWindow.Left = ((SystemParameters.PrimaryScreenWidth - 500) / 2);
            HeightSet = true;
        }
        else
        {
            Application.Current.MainWindow.Height = oldHeight;
            Application.Current.MainWindow.Width = oldWidth;
            Application.Current.MainWindow.Top = ((SystemParameters.PrimaryScreenHeight - oldHeight) / 2);
            Application.Current.MainWindow.Left = ((SystemParameters.PrimaryScreenWidth - oldWidth) / 2);
            HeightSet = false;
        }
    }

    #region " Constructor "
    /// <summary>
    /// MoreJunk
    /// </summary>
    public MoreJunk()
    {
        this.ResizeAndCenterWindowCommand = new RelayCommand(ResizeAndCenterWindow);
        this.oldHeight = Application.Current.MainWindow.Height;
        this.oldWidth = Application.Current.MainWindow.Width;
    }
    #endregion
}

我可以拖动和移动窗口,单击按钮,窗口居中并自行调整大小没有问题。

我在页面上的简单网格:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition Height="10"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Button Grid.Row="2" Content="SizeMe" Command="{Binding ResizeAndCenterWindowCommand}"></Button>
</Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2015-09-10
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多