I have WPF application where I have changed the default entry point.
<Application x:Class="FrazerClient.App" Startup="AppStartup">
public void AppStartup(object sender, StartupEventArgs e)
{
// Does some minor work before an application window opens.
}
The minor work calls this a couple of times:
App.Current.Dispatcher.Invoke((Action)delegate
{
// Custom dialog window is opened
});
The second time this is called, App.Current becomes null. I am almost positive is has to do with the custom dialog window closing, but not really sure how to prevent the closing of the dialog window from nulling out App.Current when the last window closes.
This also prevents App.Current.Shutdown() from working.
Set ShutDownMode to OnExplicitShutdown so that closing the window does not automatically shutdown the application.
You usually do that in the Xaml of the application object
<Application x:Class="FrazerClient.App"
Startup="AppStartup"
ShutdownMode="OnExplicitShutdown">
Resolved with this code:
Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
Related
I've got another Window besides my MainWindow in my Application and I override the closing-method like this because I don't want the Window to be fully closed:
private void Window_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
this.Visibility = Visibility.Hidden;
}
then in the constructor:
public Inputwindow()
{
InitializeComponent();
this.Closing += Window_Closing;
}
But now if I want to close my MainWindow, it only hides the MainWindow.
Can't figure out, how to get it to work.
Every WPF application has a property called Application.ShutdownMode, which determines when the application actually ends. By default, it is set to OnLastWindowClose, which means that the application won't end until all windows are closed (even if the main window closes). It sounds like you want your application to end when the main window closes even if other windows are not closed, just hidden, which would correspond to the OnMainWindowClose mode. Here is the relevant MSDN documentation: https://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode(v=vs.110).aspx
To set the property in your xaml, open the App.xaml file and add the ShutdownMode property like this:
<Application x:Class="TestClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ShutdownMode="OnMainWindowClose">
</Application>
I switch ShutdownMode in my app to OnExplicitShutdown so when I close window, my app still working and I try to reopen window like this:
new MainWindow().Show;
But I've got following error: Set property '...ViewModelLocator.AutoWireViewModel' threw an exception. and InnerException is: 'AutoWireViewModel' property was already registered by 'ViewModelLocator'
Have you any idea how to fix this?
I use ViewModelLocator from Microsoft.Prism
Unfortunately, you simply can't re-open a Window after it has been closed. However, you can 'pretend' that you can and use a little deception... instead of closing the Window in the first place, you could simply hide it, as it can be hidden and re-displayed any number of times:
YourWindow.Visibility = Visibility.Collapsed;
You could improve this situation slightly perhaps, by attaching an event handler to the Window.Closing Event. This particular event provides a way to cancel the Close operation and so at this point, you could hide it instead:
private void Window_Closing(object sender, CancelEventArgs e)
{
// Cancel Window closing
e.Cancel = true;
// Hide Window instead
YourWindow.Visibility = Visibility.Collapsed;
}
You could alternatively put this functionality inside the Window code behind using the Window.OnClosing Method if you preferred.
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..
}));
}
I have a WPF project were I am using multiple WPF windows.
My WPF Windows are:
MainWindow
Window1
Login
I have to case scenarios, in the first one everything works fine but in the second I get a null reference exception.
First Scenario:
App.xaml is configured so as startup window to be MainWindow.
When user clicks on Button on MainWindow he is forwarded in Window1 were I have the following code:
MainWindow obj=(MainWindow)Application.Current.MainWindow;
private void button1_Click(object sender, RoutedEventArgs e)
{
obj.checkBox1.IsChecked = false;
}
2.Second Scenario:
App.xaml is configured so as startup window to be Login Window.
Code in Login:
private void button1_Click(object sender, RoutedEventArgs e)
{
var window=new MainWindow();
window.Show();
this.Close();
}
In this scenario when I click on button in Window1 a null reference exception is thrown for obj.
What is the difference in the initialization of MainWindow in these 2 cases that causes the exception in the 2nd case and how can I overcome it?
Well, the first Window being opened when you start the application, will become the window you get back when calling Application.Current.MainWindow.
In your case, that is Login, but in Window1 you expect it to be MainWindow, which is wrong. Since Login has been closed, you get null back, and the app crashes.
To fix this you have to make MainWindow the MainWindow :-)
You can do that in Login like so:
var window = new MainWindow();
Application.Current.MainWindow = window;
window.Show();
this.Close();
The this.Close() on your Login window in scenario 2 will close the application as this is the window being pointed to in your app.xaml file as the startup window. See the MainWindow property
MainWindow is automatically set with a reference to the first Window object to be instantiated in the AppDomain.
In the first scenario you don't close the MainWindow so the application continues. In the second one you close the Login window so the application exits.
In the first scenario you don't show where the user is forwarded to window1. It would be helpful to see that code as well.
My application uses ClickOnce technology for deployment. However I have problem when user starts using the application. The scenario for reproducing the problem is as follows:
User clicks on application's shortcut in order to run the application
ClickOnce's "Launching application" dialog box appears in order to check for updates
"Launching application" dialog box disappears
Splashscreen appears
Main window (login window) appears - however it's not active nor has a focus
Because main window is not active, user has to click on it before he/she can start typing username and password. How can I resolve this problem so the main window is active after it appears? I've tried the following code but it's not working:
protected override void OnInitialized(EventArgs e)
{
while (!this.IsFocused) { this.Focus(); WPFWaitForPriority.WaitForPriority(DispatcherPriority.Background); }
base.OnInitialized(e);
}
Most likely you're giving focus to the splash screen. So when it closes nothing has focus any longer. After closing the form call the Select Method on the control you want to have focus(the username textbox i'm guessing).
Select for Focus
try this code:
Pseudo code:
OnShown
this.focus
I mean.
Use diffrent event.
I have a WPF / ClickOnce application and don't have the same problem. I don't use the StartupUri of App.xaml, I manually show the login window instead as follows in App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
this.Exit += new ExitEventHandler(App_Exit);
base.OnStartup(e);
ShowLogin();
}
private MainWindow _mainWindow;
private void ShowLogin()
{
_mainWindow = new MainWindow(delegate()
{
//.......
});
_mainWindow.Closed += new EventHandler(_mainWindow_Closed);
this.MainWindow = _mainWindow;
this.MainWindow.Show();
}
As per my understanding you can programatically trigger click event once inside constructor .
How about LoginForm.Activate() after the splash screen is closed and the LoginForm is displayed?
Maybe try to get focus on TextBox on that form. Login name for example.
I think OnInitialized event may be too early. OnShown should be good.
Are you using Visual Studio? I know if you go to the form properties under the project tab, you can specify the start up object. Perhaps you can specify your main form and the splashscreen can load in front of it.
have you tried:
protected override void OnInitialized(EventArgs e)
{
while (this.CanFocus) { this.Focus(); WPFWaitForPriority.WaitForPriority(DispatcherPriority.Background); }
base.OnInitialized(e);
}