简体   繁体   English

如何恢复最小化/隐藏的 window?

[英]How to restore minimized/hidden window?

I have MaterialWindows from MaterialDesignInXaml which can be minimized in the notification bar.我有来自 MaterialDesignInXaml 的 MaterialWindows,它可以在通知栏中最小化。

I hide the windows by setting the Visibility to Collapsed .我通过将Visibility设置为Collapsed来隐藏 windows 。

The double click on the icon launches a command to restore the windows:双击图标启动命令以恢复 windows:

public RelayCommand OpenWindowsCommand => new RelayCommand(ExecuteOpenWindowsCommand);
private void ExecuteOpenWindowsCommand(object o)
{
    WindowState = WindowState.Normal;
    //Activated = true;
    //IsInForeground = true;
    //IsInForeground = false;
    //IsFocus = true;
}

My property WindowState changed the visibility in it and notify the change with OnPropertyChanded .我的属性WindowState更改了其中的可见性并使用OnPropertyChanded通知更改。

The windows appear in the task bar but doesn't come on the foreground of my screen. windows 出现在任务栏中,但没有出现在我屏幕的前台。

As you can see in comment, I tried different ways to get it without success like:正如您在评论中看到的那样,我尝试了不同的方法来获得它但没有成功,例如:

  • Focusable property Focusable属性
  • TopMost property TopMost属性
  • Top property Top物业
  • Even a behaviour to activate the windows甚至是激活 windows 的行为

Is there something special to put a windows in the foreground of the screen?将windows放在屏幕的前景有什么特别的吗?

Edit to add a sample repo:编辑以添加示例存储库:

MainWindow.xaml主窗口.xaml

<controls:MaterialWindow
    x:Class="MinimiedWindows.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Behaviors="clr-namespace:MinimiedWindows.Behavior"
    xmlns:controls="clr-namespace:MaterialDesignExtensions.Controls;assembly=MaterialDesignExtensions"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:MinimiedWindows"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:tb="http://www.hardcodet.net/taskbar"
    Title="MainWindow"
    Width="800"
    Height="450"
    Focusable="{Binding IsFocus}"
    Icon="app.ico"
    Topmost="{Binding IsFocus}"
    Visibility="{Binding WindowsVisibility, Mode=TwoWay}"
    WindowState="{Binding WindowState}"
    mc:Ignorable="d">

    <i:Interaction.Behaviors>
        <Behaviors:ActivateBehavior Activated="{Binding Activated, Mode=TwoWay}" />
    </i:Interaction.Behaviors>

    <Grid>
        <tb:TaskbarIcon
            x:Name="myNotifyIcon"
            DoubleClickCommand="{Binding OpenWindowsCommand}"
            IconSource="{Binding NotifyIcon}"
            MenuActivation="LeftOrRightClick"
            PopupActivation="DoubleClick"
            ToolTip="aa"
            Visibility="{Binding NotifyIconVisibility}" />
    </Grid>
</controls:MaterialWindow>

MainWindows constructor set the datacontext to a new MainViewModel. MainWindows 构造函数将数据上下文设置为新的 MainViewModel。

MainViewModel.cs: (ViewModelBase is the classic one with the property OnPropertyChanged) MainViewModel.cs:(ViewModelBase 是具有属性 OnPropertyChanged 的经典版本)

public class MainViewModel : ViewModelBase
{
   public MainViewModel()
   {
   }

   #region Windows properties

   private WindowState _WindowState = WindowState.Normal;

   public WindowState WindowState
   {
      get { return _WindowState; }
      set
      {
         _WindowState = value;
         OnPropertyChanged();
         OnPropertyChanged("WindowsVisibility");
         OnPropertyChanged("NotifyIconVisibility");
      }
   }

   private Visibility _WindowsVisibility = Visibility.Visible;

   public Visibility WindowsVisibility
   {
      get
      {
         if (WindowState == WindowState.Minimized)
         {
            return Visibility.Collapsed;
         }

         return Visibility.Visible;
      }
      set { _WindowsVisibility = value; }
   }

   private bool _Activated;

   public bool Activated
   {
      get { return _Activated; }
      set
      {
         _Activated = value;
         OnPropertyChanged();
      }
   }

   private bool _IsInForeground;

   public bool IsInForeground
   {
      get { return _IsInForeground; }
      set
      {
         _IsInForeground = value;
         OnPropertyChanged();
      }
   }

   private bool _IsFocus;

   public bool IsFocus
   {
      get { return _IsFocus; }
      set
      {
         _IsFocus = value;
         OnPropertyChanged();
      }
   }

   #endregion

   #region NotifyBar

   public string NotifyIcon
   {
      get { return "app.ico"; }
   }

   public Visibility NotifyIconVisibility
   {
      get
      {
         if (WindowState == WindowState.Minimized)
         {
            return Visibility.Visible;
         }

         return Visibility.Collapsed;
      }
   }

   public RelayCommand OpenWindowsCommand => new RelayCommand(ExecuteOpenWindowsCommand);
   private void ExecuteOpenWindowsCommand(object o)
   {
      WindowState = WindowState.Normal;
      Activated = true;
   }
   #endregion
}

And finally my ActivateBehavior.cs最后是我的 ActivateBehavior.cs

public class ActivateBehavior : Behavior<MaterialWindow>
{
   Boolean isActivated;

   public static readonly DependencyProperty ActivatedProperty =
     DependencyProperty.Register(
       "Activated",
       typeof(Boolean),
       typeof(ActivateBehavior),
       new PropertyMetadata(OnActivatedChanged)
     );

   public Boolean Activated
   {
      get { return (Boolean)GetValue(ActivatedProperty); }
      set { SetValue(ActivatedProperty, value); }
   }

   static void OnActivatedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
   {
      var behavior = (ActivateBehavior)dependencyObject;
      if (!behavior.Activated || behavior.isActivated)
         return;
      // The Activated property is set to true but the Activated event (tracked by the
      // isActivated field) hasn't been fired. Go ahead and activate the window.
      if (behavior.AssociatedObject.WindowState == WindowState.Minimized)
         behavior.AssociatedObject.WindowState = WindowState.Normal;
      behavior.AssociatedObject.Activate();
   }

   protected override void OnAttached()
   {
      AssociatedObject.Activated += OnActivated;
      AssociatedObject.Deactivated += OnDeactivated;
   }

   protected override void OnDetaching()
   {
      AssociatedObject.Activated -= OnActivated;
      AssociatedObject.Deactivated -= OnDeactivated;
   }

   void OnActivated(Object sender, EventArgs eventArgs)
   {
      this.isActivated = true;
      Activated = true;
   }

   void OnDeactivated(Object sender, EventArgs eventArgs)
   {
      this.isActivated = false;
      Activated = false;
   }
}

For the packages, I have installed Hardcodet.NotifyIcon.Wpf, MaterialDesignExtensionss 3.1.0 (and the materialDesign dependencies) and System.Windows.Interactivity.WPF.对于这些包,我已经安装了 Hardcodet.NotifyIcon.Wpf、MaterialDesignExtensionss 3.1.0(和 materialDesign 依赖项)和 System.Windows.Interactivity.Z3055DD731D36724.

Change your ExecuteOpenWindowsCommand to only set the Activated property.将您的ExecuteOpenWindowsCommand更改为仅设置Activated属性。

private void ExecuteOpenWindowsCommand()
{
   Activated = true;
}

Adapt the OnActivatedChanged method in the ActivateBehavior like this.像这样调整ActivateBehavior中的OnActivatedChanged方法。

private static void OnActivatedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
   var behavior = (ActivateBehavior)dependencyObject;
   if (!behavior.Activated || behavior.isActivated)
      return;

   // The Activated property is set to true but the Activated event (tracked by the
   // isActivated field) hasn't been fired. Go ahead and activate the window.
   var window = behavior.AssociatedObject;
   if (window.WindowState == WindowState.Minimized)
   {
      window.WindowState = WindowState.Normal;
      SystemCommands.RestoreWindow(window);
   }

   window.Activate();
}

The essential part is restoring the window, which did not happen before, it was hidden.最重要的部分是恢复 window,这在以前没有发生过,它被隐藏了。 Then it needs to be activated to come to foreground.然后它需要被激活才能进入前台。 The window state asssignment is not needed, but makes the restore transition of the window more pleasant.不需要 window state 分配,但使 window 的恢复过渡更加愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM