picoe / Eto

Cross platform GUI framework for desktop and mobile applications in .NET
Other
3.57k stars 325 forks source link

[WPF] Can't stop closing of hidden main window #1130

Open MartinRothschink opened 6 years ago

MartinRothschink commented 6 years ago

I'm still trying to create a tray application with a hidden main window. Now I noticed that I can't prevent the closing of a hidden main window.

Expected Behavior

OnClosing: e.Cancel = true should stop closing a window, regardless of its visibility.

Actual Behavior

If window is invisible, window is closed

Steps to Reproduce the Problem

  1. Set e.Cancel = true in OnClosing
  2. Set main window invisible
  3. Close window from tray context menu
  4. Window is closed, application exits

Code that Demonstrates the Problem

using System;
using Eto.Forms;
using Eto.Drawing;

namespace EtoTrayApp
{
   using System.ComponentModel;

   public partial class MainForm : Form
   {
      private bool _startup = true;
      private TrayIndicator _tray;
      public MainForm()
      {
         Title = "My Eto Form";
         ClientSize = new Size(200, 200);
         _tray = new TrayIndicator
         {
            Image = Eto.Drawing.Icon.FromResource("EtoTrayApp.error.ico"),
            Menu = new ContextMenu
            {
               Items =
               {
                   new ButtonMenuItem {Text = "Quit", Command = new Command((s, e) => Application.Instance.Quit())}
               }
            }
         };

         ShowInTaskbar = false;
         _tray.Visible = true;
      }

      protected override void OnClosing(CancelEventArgs e)
      {
         base.OnClosing(e);
     e.Cancel = true;
      }

      protected override void OnClosed(EventArgs e)
      {
         base.OnClosed(e);
         _tray.Dispose();
      }

      protected override void OnShown(EventArgs e)
      {
         base.OnShown(e);
         if (_startup)
         {
            // change this to false to show the issue
            Visible = true;
            _startup = false;
         }
      }
   }
}

Specifications

MartinRothschink commented 6 years ago

This works on MAC and GTK as expected.