WPF application with modal MainWindow - c#

i am wondering if there are any drawbacks, in showing a modal mainwindow in a WPF application with .ShowDialog() instead of Show().
This is the usual way to manually show the mainwindow:
private void Application_Startup(object sender, StartupEventArgs e)
{
// the usual way to show the mainwindow
new MainWindow().Show();
}
This is what i want do to:
private void Application_Startup(object sender, StartupEventArgs e)
{
// what i want to do
new MainWindow().ShowDialog();
}
I want to start a WPF app from a 3. party application addon, that can call static methods from external .NET DLLs. So i build a DLL as an addon that starts my WPF app inside a new AppDomain. This works fine as long the user does not do anything in the 3. party app, otherwise it will crash.
I can prevent this, if i show the mainwindow of my app as a modal dialog, because this blocks the 3. party app window.

Well, a potential drawback is the fact that the ShowDialog() method doesn't return until the dialog window has been closed which means that you won't be able to interact with the "previous" window during the meantime.
But in your case, this seems to be exactly what you want. So since it works the way you want in your specific scenario, I would stick with using ShowDialog().

Related

Switching Between Windows WPF (MVC)

I have a test WPF application that has two windows (I'm also using MVC and not MVVM). Both which have one button that should direct the user to the other window.
Initially, I tried this code (I'm only showing the event handlers):
MainWindow.xaml.cs
private static void Button_Click(object sender, RoutedEventArgs e)
{
OtherWindow k = new OtherWindow();
k.Show();
this.Close();
}
OtherWindow.xaml.cs
private static void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow k = new MainWindow();
k.Show();
this.Close();
}
The code works, but I take a look at the memory usage and it increases every time I switch window. Is this normal or is there a way to avoid this and keep the simplicity?
It's very easy to run into memory leaks in WPF, especially when instantiating and disposing of windows multiple times.
I'd recommend taking a look at this page:
http://svetoslavsavov.blogspot.com/2010/05/memory-leaks-in-wpf-applications.html
which details the most common ways to run into a memory leak in WPF and how to fix it.
I'd suggest looking at the Event Handlers and Events from Static Objects sections first.
It seems that you aren't removing your event handlers for your window before closing it, which means that the Window will be kept in memory.
You should also take a look at this thread for good points on disposing of resources correctly. Check out the answer by rookie1024:
What is the correct way to dispose of a WPF window?

WPF DragDrop: Window.ShowModal() hangs Windows Explorer

I have written code to allow dropping a file from Windows Explorer into a WPF application. In my drop event handler I launch a window which displays information about the dropped file. If I create this window using Window.ShowModally() then Windows Explorer will hang/freeze until the window in my app is closed. However, if I create the window using Window.Show() this problem does not occur. Unfortunately, I require this window to be shown modally.
Presumably the Windows Explorer thread is locked because it detects that one of its files is being used. Is there a way to inform Windows Explorer that it does not need to wait for the window in my application to close? I have tried setting the DragDropEventArgs.Handled to true but this does not resolve the problem.
I no longer require the DragDrop once I have extracted the data from it so if there is a way to cancel or end the DragDrop in my Drop event handler then this would be an acceptable solution.
You cannot block in any of your drag+drop event handlers, that will hang the D+D plumbing and a dead source window is the expected outcome.
There's a simple workaround, just ask the dispatcher to run your code later, after the event completes. Elegantly done with its BeginInvoke() method. Roughly:
private void Window_Drop(object sender, DragEventArgs e) {
// Do something with dropped object
//...
this.Dispatcher.BeginInvoke(new Action(() => {
var dlg = new DialogWindow();
dlg.Owner = this;
var result = dlg.ShowDialog();
// etc..
}));
}

Is ShowDialog handled differently in WPF than Winforms?

I have another issue in converting my Winforms program to a WPF program. In my first program, I had a smaller window open to allow the user to adjust some data, and then when it closed, the other form was activated again with the new data.
I used form2.ShowDialog(); to open the form, which automatically makes the parent form deactivated in Winforms. This way when I closed form2, the parent form was activated, and I was able to use an event handler form1_Activated to reload and re-initialize some of the settings successfully.
However, now when I attempt to do the same thing with WPF, I am still able to open form2 using form2.ShowDialog();, but then when I close the form, it does not register the form1_Activated event handler. Instead, in order to reload the settings, I must click on another window, and then come back into my program to register the form1_Activated event handler.
Am I just doing something wrong, or is there another event handler that I should be using in WPF to achieve the same thing I was able to do in Winforms?
Calling ShowDialog() causes the dialog box top appear in modal mode so I don't understand why you would need an event handler to process the results after the dialog box is closed. Keep in mind that you can access public variables in the DialogBox, as well. If I understand your question, this should do what you are asking:
MainWindow:
My_DialogBox dlg = new My_DialogBox();
dlg.Owner = this;
dlg.MyPublicVariable = ''; //some value that you might need to pass to the dialog
dlg.ShowDialog(); //exection of MainWindow is suspended until dialog box is closed
if (dlg.DialogResult == true)
{
//dlg.MyPublicVariable is still accessible
//call whatever routines you need in order to refresh the main form's data
}
DialogBox:
private void OK_Button_Click(object sender, RoutedEventArgs e)
{
MyPublic variable = something; //accessible after the dialog has closed.
this.DialogResult = true;
}
private void Cancel_Button_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
The MSDN write-up on dialog boxes is pretty good. There may be some tips that might help you even more:
http://msdn.microsoft.com/en-us/library/aa969773.aspx
Good luck!

Different form will not load

I'm using c# to make a mobile 6 application. I created another windows form in the project. This is the form that I would like to load first. This is what have tried:
MainMenu gameMenu = new MainMenu();
private void MainForm_Load(object sender, EventArgs e)
{
this.Hide();
gameMenu.ShowDialog();
.....
}
When I run this the emulator comes up but it just stays as the default windows screen. And I don't get any of my forms.
GameMenu's parent is MainForm, which is now hidden, so the Dialog isn't going to be visible. You need to adjust your logic to do one of the following:
show the GameMenu first (i.e. Application.Run(new GameMenu))
Don't hide MainForm
Use gameMenu.Show() instead of ShowDialog()
You may need to get rid of this.Hide() or use gameMenu.Show() instead of gameMenu.ShowDialog() or you may need to do both.
If you have to use gameMenu.Show() instead of gameMenu.ShowDialog(), you may also want to do the following:
Subscribe to MainForm's GotFocus event and call gameMenu.Show() again whenever the other form gains focus unintentionally. Set MainForm's Enabled property to false while the gameMenu is shown if you want to prevent any accidental interaction with the MainForm while the gameMenu is supposed to be shown.

WinForms programming - Modal and Non-Modal forms problem

I have a problem with modality of the forms under C#.NET. Let's say I have main form #0 (see the image below). This form represents main application form, where user can perform various operations. However, from time to time, there is a need to open additional non-modal form to perform additional main application functionality supporting tasks. Let's say this is form #1 in the image. On this #1 form there might be opened few additional modal forms on top of each other (#2 form in the image), and at the end, there is a progress dialog showing a long operation progress and status, which might take from few minutes up to few hours. The problem is that the main form #0 is not responsive until you close all modal forms (#2 in the image). I need that the main form #0 would be operational in this situation. However, if you open a non-modal form in form #2, you can operate with both modal #2 form and newly created non modal form. I need the same behavior between the main form #0 and form #1 with all its child forms. Is it possible? Or am I doing something wrong? Maybe there is some kind of workaround, I really would not like to change all ShowDialog calls to Show...
Image http://img225.imageshack.us/img225/1075/modalnonmodalproblem.png
Modal forms do exactly what "modal" means, they disable all other windows in the app. That's rather important, your program is in a somewhat perilous state. You've got a chunk of code that is waiting for the dialog to close. Really Bad Things could happen if those other windows were not disabled. Like the user could start the modal dialog again, now your code is nested twice. Or she could close the owner window of the dialog, now it suddenly disappears.
These are the exact kind of problems you'd run into if you call Application.DoEvents() inside a loop. Which is one way to get a form to behave modal without disabling other windows. For example:
Form2 mDialog;
private void button1_Click(object sender, EventArgs e) {
mDialog = new Form2();
mDialog.FormClosed += (o, ea) => mDialog = null;
mDialog.Show(this);
while (mDialog != null) Application.DoEvents();
}
This is dangerous.
It is certainly best to use modal forms the way they were designed to stay out of trouble. If you don't want a modal form then simply don't make it modal, use the Show() method. Subscribe to its FormClosing event to know that it is about to close:
private void button1_Click(object sender, EventArgs e) {
var frm = new Form2();
frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
frm.Show();
}
void frm_FormClosing(object sender, FormClosingEventArgs e) {
var frm = sender as Form2;
// Do something with <frm>
//...
}
The first thing that comes to mind would be something like this. You could disable form 1 when you launch form 2 and then have form 1 handle the closed event of the second form to re-enable itself. You would NOT open modal 2 using show dialog.
Now keep in mind, from a user perspective this is going to be quite cumbersome, you might look at doing a MDI application to get all windows inside of a single container.
Your main form will not be responsive until any modal dialogs that are in the same process space are closed. There is not work around for that.
It looks to me like you could use an MDI application setting the Form #0 IsMdiContainer property to true.
Then, you could do something alike:
public partial class Form0 {
public Form0 {
InitializeComponent();
this.IsMdiContainer = true; // This will allow the Form #0 to be responsive while other forms are opened.
}
private void button1_Click(object sender, EventArgs e) {
Form1 newForm1 = new Form1();
newForm1.Parent = this;
newForm1.Show();
}
}
Using the ShowDialog() as you stated in your question will make all of the forms Modal = true.
By definition, a modal form is:
When a form is displayed modally, no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (usually in response to some user action) before input to another form can occur. Forms that are displayed modally are typically used as dialog boxes in an application.
You can use this property [(Modal)] to determine whether a form that you have obtained from a method or property has been displayed modally.
So, a modal form shall be used only when you require immediate assistance/interaction from the user. Using modal forms otherwise makes believe that you're perhaps running into a wrong direction.
If you do not want your main form to be an MDI container, then perhaps using multithreading is one solution through a simple BackgroundWorker class is the key to what you want to achieve. Thus, it looks to me like a design smell...
What is it you want to do, apart of making your main form responsive, etc.
What is it you have to do?
Explaining what you have to do, we might be able to guide you altogether into the right, or at least perhaps better, direction.
Actually the answer is very simple. Try
newForm.showDialog();
This will open a new form, while the parent one is inaccessible.

Categories

Resources