Select to view content in your preferred language

ShowWindow/HideWindow Toggle

1060
2
Jump to solution
10-30-2013 06:10 AM
BrianLeroux
Frequent Contributor
I have a bunch of tools that utilize ShowWindow to display the UI of the tool. Once the window is displayed, the button used to execute the tool is still enabled but does nothing. What I am hoping to do is allow the button to close the window that is already open. In order to do that I need to check to see if the window is open and if so run HideWindow indtead of ShowWindow. I looked through the Extensibility namespace and couldn't find much that will help me.

Any ideas?
0 Kudos
1 Solution

Accepted Solutions
BrianLeroux
Frequent Contributor
I think I figured this out. I needed to set up an event handler to set my Diaolog to null when hidewindow was fired. Here is my code in case anyone finds it useful or wants to show me there is a better way to handle.

[Export(typeof(ICommand))]     [DisplayName("Hail Analysis Tool")]     public class AnalysisTool : ICommand     {         private MyAnalysisDialog AnalysisDialog = null;         // Event Declaration         public event EventHandler hideDialog;                #region ICommand members         public void Execute(object parameter)         {             // Create a new Dialog if one has not already been created             //AnalysisDialog = AnalysisDialog ?? new MyAnalysisDialog();             hideDialog += new EventHandler(HideHandler);             if (AnalysisDialog != null)             {                 MapApplication.Current.HideWindow(AnalysisDialog);                                             }             else             {                 AnalysisDialog = new MyAnalysisDialog();                                 MapApplication.Current.ShowWindow("Hail Analysis", AnalysisDialog, false, null,HideHandler);             }                      }          public void HideHandler(object sender, EventArgs e)         {             AnalysisDialog = null;             hideDialog -= new EventHandler(HideHandler);         }          public bool CanExecute(object parameter)         {             // Return true so that the command can always be executed             return true;         }          public event EventHandler CanExecuteChanged;          #endregion     }

View solution in original post

0 Kudos
2 Replies
BrianLeroux
Frequent Contributor
I think I figured this out. I needed to set up an event handler to set my Diaolog to null when hidewindow was fired. Here is my code in case anyone finds it useful or wants to show me there is a better way to handle.

[Export(typeof(ICommand))]     [DisplayName("Hail Analysis Tool")]     public class AnalysisTool : ICommand     {         private MyAnalysisDialog AnalysisDialog = null;         // Event Declaration         public event EventHandler hideDialog;                #region ICommand members         public void Execute(object parameter)         {             // Create a new Dialog if one has not already been created             //AnalysisDialog = AnalysisDialog ?? new MyAnalysisDialog();             hideDialog += new EventHandler(HideHandler);             if (AnalysisDialog != null)             {                 MapApplication.Current.HideWindow(AnalysisDialog);                                             }             else             {                 AnalysisDialog = new MyAnalysisDialog();                                 MapApplication.Current.ShowWindow("Hail Analysis", AnalysisDialog, false, null,HideHandler);             }                      }          public void HideHandler(object sender, EventArgs e)         {             AnalysisDialog = null;             hideDialog -= new EventHandler(HideHandler);         }          public bool CanExecute(object parameter)         {             // Return true so that the command can always be executed             return true;         }          public event EventHandler CanExecuteChanged;          #endregion     }
0 Kudos
PietaSwanepoel2
Frequent Contributor
Brian, it is always a good idea to clean behind you when you leave an application. Kill all event handlers, and set variables to null

I like using toggle buttons in stead of just the button, eg.

public class MyTool : IToggleCommand
{
  private void toolViewOnUnloaded(object sender, RoutedEventArgs routedEventArgs)
  {
   if (toolView != null)
   {
    // Clear the results
    var toolViewModel = toolView.DataContext as ConfigurationViewModel;
    toolViewModel.ClearResults();
    toolViewModel = null;
    toolView = null;
   }
  }

  #region IToggleCommand Members

  private bool _isChecked = false;
  /// <summary>
  /// Gets whether the command is toggled on or off
  /// </summary>
  public bool IsChecked()
  {
   return _isChecked;
  }

  #endregion IToggleCommand Members

  #region ICommand members
  public void Execute(object parameter)
  {
   ToolExecuting = true;
   try
   {
    toolView = new ChartToolView()
    {
     Margin = new Thickness(10),
     MinWidth = 400,
     MinHeight = 400,
     HorizontalAlignment = HorizontalAlignment.Stretch
    };
    toolView.Unloaded += toolViewOnUnloaded;
....


}
0 Kudos