WPF主窗口按下按钮,关闭当前窗口,切换到另一窗口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF主窗口按下按钮,关闭当前窗口,切换到另一窗口相关的知识,希望对你有一定的参考价值。

第一张是跳新窗口的代码,但是我已经做好了很多窗体用来跳转,如第二张图,假设我现在想从主窗口按下按钮后跳转到P1窗口,并且关掉主窗口,该怎么写代码呢

参考技术A <script type="text/javascript">
Leaf leaf = new Leaf("Leaf D");
root.Add(leaf);
root.Remove(leaf);
root.Display(1);
Console.Read();
追问

你这写的是啥。。。

WPF 多屏时子窗口的屏幕位置问题

问题:

在多个显示屏运行的情况下,如果将主窗口从当前显示屏移动到另一显示屏。

设置子窗口单例模式,在当前显示屏时弹出后,在主窗口移动到另一显示屏后,再弹出子窗口时,你会发现子窗口跑到原来显示屏去了

----这是WPF的锅

因为已经设置了WindowStartupLocation="CenterOwner",也加了Owner的情况下,窗口每次弹出,理论上就该和主窗口保持在同一屏幕的。

 

解决:

通过窗口的Activated添加委托,每次窗口唤醒,都重新设置窗口的Location

    subWindow.Left = screen.Bounds.Left + (screen.Bounds.Width - subWindow.Width) / 2;
    subWindow.Top = screen.Bounds.Top + (screen.Bounds.Height - subWindow.Height) / 2;

获取当前主窗口所在的屏幕。

var screen = Screen.FromHandle(new WindowInteropHelper(mainWindow).Handle);

 

详细:

通过给Window添加一个附加属性即可

helper:WindowScreenHelper.IsWindowShowInCurrentScreen="True" 

 

public class WindowScreenHelper
{
    public static readonly DependencyProperty IsWindowShowInCurrentScreenProperty = DependencyProperty.RegisterAttached(
        "IsWindowShowInCurrentScreen", typeof(bool), typeof(WindowScreenHelper), new PropertyMetadata(default(bool), ShowWindowInCurrentScreen));

    private static void ShowWindowInCurrentScreen(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var window = d as Window;
        if (window != null)
        {
            window.Activated += ShowInCurrentScreenWindow_Activated;
        }
    }

    private static void ShowInCurrentScreenWindow_Activated(object sender, EventArgs e)
    {
        var subWindow = sender as Window;
        if (subWindow == null) return;
        if (GetIsWindowShowInCurrentScreen(subWindow))
        {
            var mainWindow = App.Current.MainWindow;
            if (mainWindow == null) return;

            var screen = Screen.FromHandle(new WindowInteropHelper(mainWindow).Handle);
                
            if (subWindow.Tag?.ToString()==WindowState.Maximized.ToString())
            {
                subWindow.Left = screen.Bounds.Left;
                subWindow.Top = screen.Bounds.Top;
                subWindow.Width = screen.Bounds.Width;
                subWindow.Height = screen.Bounds.Height;
            }
            else
            {
                subWindow.Left = screen.Bounds.Left + (screen.Bounds.Width - subWindow.Width) / 2;
                subWindow.Top = screen.Bounds.Top + (screen.Bounds.Height - subWindow.Height) / 2;
            }
        }
    }

    public static void SetIsWindowShowInCurrentScreen(DependencyObject element, bool value)
    {
        element.SetValue(IsWindowShowInCurrentScreenProperty, value);
    }

    public static bool GetIsWindowShowInCurrentScreen(DependencyObject element)
    {
        return (bool)element.GetValue(IsWindowShowInCurrentScreenProperty);
    }
}

 

 

PS:

当窗口状态为最大化时,即WindowState=“Maximized”。是设置不了Location的。那么就需要去掉Xmal中的WindowState,通过后台去设置全屏

以上是关于WPF主窗口按下按钮,关闭当前窗口,切换到另一窗口的主要内容,如果未能解决你的问题,请参考以下文章

如何在 WPF 中从一个窗口移动到另一个窗口?

WPF中,怎样设置点击按钮关闭当前窗口

wpf关闭主窗口询问“退出”的问题

C#窗口切换问题:如何切换回主窗体

关于wpf中按钮关闭原窗口,开启新窗口

按下 Enter 键时如何关闭 WPF 窗口(对话框)?