How to change StartupUri of WPF Application? - wpf

I am trying to modify App.cs and load the WPF XAML files from code behind but its not working as it should.
No matter whatever I try to set as StartupUri it doesnt start, the program quits after this.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
LoginDialog dlg = new LoginDialog();
if (dlg.ShowDialog() != true)
return;
switch (dlg.ChoiceApp) {
case ChoiceApp.CustomerEntry:
StartupUri = new Uri("/MyApp;component/Forms/CustomerEntry.xaml",
UriKind.Relative);
break;
case ChoiceApp.VendorEntry:
StartupUri = new Uri("/MyApp;component/Forms/VendorEntry.xaml",
UriKind.Relative);
break;
}
}
}
Now I even did trace and found out that LoginDialog is working correctly and is returning values correctly but setting "StartupUri" does not work.
I checked in reverse assembly that DoStartup method of App gets called after OnStartup, so technically my StartupUri must load, but it doesnt, in App.xaml startup uri is not at all defined.
Note: Bug Confirmed
I noticed that ShowDialog sets Application.MainWindow and when dialog ends, it sets it back to null, and because of this setting StartupUri does not work after calling Modal Dialog in OnStartup or Startup event.
There is no error or exception about invalid uri or anything like that.
This method works without DialogBox being called in Startup event or OnStartup, i think calling showdialog on this method causes something like its mainwindow being set to expired window and it shuts down after this.

Akash, I ran into this exactly issue trying to implement a LoginDialog just like yours. The dialog does not have a bug, but rather the behavior is by design.
Not a bug. The default ShutdownMode of
Application is OnLastWindowClosed, so
as soon as the first window is closed
your application will start shutting
down! Change to OnExplicitShutdown and
it will work, but you'll have to
manage the shutdown.
See this previous StackOverflow question: WPF ShowDialog returns null immediately on second call

instead of overriding the OnStartup() method, hook into the event instead.
in the XAML
<Application x:Class="SOTestWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
in the code behind:
private void Application_Startup(object sender, StartupEventArgs e)
{
var rnd = new Random();
if (rnd.NextDouble() > 0.5)
StartupUri = new Uri("/SOTestWPF;component/Window1.xaml", UriKind.Relative);
else
StartupUri = new Uri("/SOTestWPF;component/Window2.xaml", UriKind.Relative);
}
This is only my test case and I have verified that it performs correctly (randomly :D)

Just try in OnStartup() :
StartupUri = new Uri("Forms/CustomerEntry.xaml", UriKind.Relative);

Do you still have a StartupUri specified in the XAML? If so, remove it and see if that helps.MSDN Source
If not, you may need to approach this differently: have your Dialog as your startup, then from that point open another Window based on the selected value.

Related

Application closes if any window is shown before the main window

I have a very puzzling case. During the initialization of my application, before any windows are shown, this function deserializes application settings from XML file, but if deserialization function throws any errors, it displays an error message to the user (which is a custom-made WPF dialog), and once that message dialog is closed, it creates new instance of the settings, and continues the initialization:
Public Function LoadSettings()
Try
Return DeserializeFromXML(settingsPath)
Catch ex As Exception
Msg.ShowMessage(Msg.corruptedSettingsFile)
Return new AppSettings
End Try
End Function
ShowMessage is defined as:
Public Function ShowMessage(message As Message) As Boolean
Dim messageDialog As New MessageDialog(message.Title, message.Content, message.Buttons)
Return messageDialog.ShowDialog()
End Function
Now, the weird thing is, after that exception is caught, and error message is closed by the user, initialization continues, but when it arrives at mainWindow.Show(), nothing happens. Main window is not shown. And once initialization code finishes, the application closes immediately.
In my Application Properties, the Shutdown mode is set to "On main window close". StartupUri is not set, because I want to show that main window exactly when I want, not automatically. However, for testing, I tried to remove mainWindow.Show() altogether, and set StartupUri to MainWindow.XML, but it doesn't solve the issue either.
I have tested that if I do not display that error message to the user, the application loads correctly, either with mainWindow.Show() or with StartupUri.
Why is this happening?
EDIT: I forgot to mention, which is probably important, that before the mainWindow.Show(), I also call Application.Current.MainWindow = mainWindow. So in case WPF makes that message dialog main window, this call should override it... But it still doesn't work.
EDIT 2: I discovered that if I call Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown before showing the message, and restore it with Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose and also call Application.Current.MainWindow = mainWindow, then the application does not close, and all works correctly. However, this is a horrible solution. It is obvious now that the message dialog hijacks the Application.Current.MainWindow. How do I stop this behavior on the application level, so that the Application.Current.MainWindow would only get set when I explicitly set it?
you must set MainWindow before calling ShowDialog methed of any other windows. The reason of this is: a window that was created in application thread, will set itself as main window if MainWindow was null; and after ShowDialog was called, you would have any chance to fix this because of modal mode.
// this will work.
private void Application_Startup(object sender, StartupEventArgs e)
{
var a = new MainWindow();
var b = new MessageDialog();
b.ShowDialog();
a.Show();
}
// this will not.
private void Application_Startup(object sender, StartupEventArgs e)
{
var b = new MessageDialog();
b.ShowDialog(); //app shutdown at this point
var a = new MainWindow();
a.Show();
}

How to customize startup of WPF application?

When a new WPF Application project is created, MainWindow.xaml, App.xaml and their corresponding code behind classes are automatically generated. In the App.xaml there is an attribute that defines which window is going to be run initially and by the default it's StartupUri="MainWindow.xaml"
I have created a new Dispatcher class in the same project. At startup, I want the instance of that class Dispatcher to be constructed and then one of its method to run. That method would actually create and show the MainWindow window. So how do I modify the App.xaml or App.xaml.cs in order to make it happen? Or, if it cannot be done by App, how should I implement it? Thanks.
You can remove the StartupUri attribute from the App.xaml.
Then, by creating an override for OnStartup() in the App.xaml.cs, you can create your new instance of your Dispatcher class.
Here's what my quick app.xaml.cs implementation looks like:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
new MyClassIWantToInstantiate();
}
}
}
Update
I recently discovered this workaround for a bug if you use this method to customize app startup and suddenly none of the Application-level resources can be found.
Try to use the Startup event (class Application) - MSDN.
You can show MainWindow in this event handler - after you create a Dispatcher instance.
1.In App.xaml, To replace the StartupUri with a subscription to the Startup event.
Use the event in App.xaml.cs .
For instance,
Startup="Application_Startup" in .xaml.
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
// Create the startup window
MainWindow wnd = new MainWindow();
// Do stuff here, e.g. to the window
wnd.Title = "Something else";
// Show the window
wnd.Show();
}
}

Window is not recognizing an application resource

To reproduce the error:
Create a new MVVM-Light WPF application.
Copy MainWindow.xaml to MainWindow2.xaml Rename MainWindow2's class name to MainWindow2 (and the constructor)
Rename MainWindow2 window class attribute to "x:Class="MvvmLight2.MainWindow2"
Remove StartupUri from App.xaml
Add the following to App:
protected override void OnStartup(StartupEventArgs e)
{
new MainWindow().Show();
new MainWindow2().Show();
}
Run the application and get error:
Cannot find resource named '{Locator}'. Resource names are case sensitive. Error at object 'System.Windows.Data.Binding' in markup file 'MvvmLight2;component/mainwindow.xaml' Line 10 Position 9.
To resolve the error:
Remove DataContext="{Binding Main, Source={StaticResource Locator}}" from both windows.
Add the following line to both windows' constructors:
DataContext = new ViewModelLocator().Main;
The application now runs.
The question is why doesn't it recognize the Locator even though it's defined as an application resource?
Update:
I just noticed that I can add the same resource on both xaml and code without any visible side effects. The question now becomes, is there a problem with this? Does it create a duplicate resource or it doesn't because they have the same key?
More than just hacking it, I'm trying to understand what's going on.
Managed to fix this by adding InitializeComponent() inside Application.Startup event handler:
App.xaml
<Application x:Class="SomeNamespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup"> <!-- Important to use Startup -->
App.xaml.cs
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
InitializeComponent(); // <-- Important to set this!
var window = new MainWindow();
window.Show();
}
}

App xaml assumes the first window instantiated is the main window (showdialog is ignored), I need to show multiple windows

I have the following code in my App.xaml.cs
private void App_Start(object sender, StartupEventArgs e)
{
if ( CompletedInstall())
{
//using show to allow for pacifier if loading is slow
var manager = new WINServiceConfig();
MainWindow = manager;
manager.ShowDialog();
}
}
private bool CompletedInstall()
{
var window = new Initialize();
window.ShowDialog();
return window.DoLaunchManager;
}
and the following in the App.xaml
<Application x:Class="Manager.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_Start">
When i comment out the line that checks CompletedInstall() the manager.ShowDialog() works fine, and my configuration window shows.
When CompletedInstall() is called the call to manager.ShowDialog() returns right away without displaying the window. I added the main window on the assumption that somewhere along the line someone decided an app should only show one window.
I found a workaround by setting the main window before calling CompletedInstall
private void App_Start(object sender, StartupEventArgs e)
{
var manager = new WINServiceConfig();
MainWindow = manager;
if (CompletedInstall())
{
manager.ShowDialog();
}
but this forces me to develop WINServiceConfig (specifically the constructor) based on its use, because it cannot count on the prerequisites being completed. This is bad form. What else can i do to get around this problem?
Dummy window? That can't be the best answer. Can it??
You should set the ShutdownMode to OnExplicitShutdown (at least while showing the initial dialog).

Application.LoadComponent makes application freeze on shutdown

If I use Application.LoadComponent() to load a UserControl, Page or Window, my application freezes when I try to close it.
The app apparently closes, but the process keeps running. Easy to notice when debugging.
I've tested it under Windows 7 64bit and Vista 32bit. In both cases I have used VS2008 and .NET 3.5.
A repro can be built by creating a wpf application as follows:
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}
public void LoadCopy() {
var uri = new Uri("/WpfApplication1;component/window1.xaml", UriKind.Relative);
var copy = (Window)Application.LoadComponent(uri);
MessageBox.Show(copy.Title);
}
private void Button_Click(object sender, EventArgs e) {
LoadCopy();
}
}
Does anyone know what might be happening? And how to solve it?
Try assigning the owner to the created assembly i.e.
copy.Owner = this;
I was able to close your example after doing this.
I think it is because you are calling LoadComponent() on what is also your Main Window ( http://msdn.microsoft.com/en-us/library/system.windows.application.mainwindow.aspx ), i.e. the startup uri, in your case Window1. The program is probably entering some loop when you close it because closing a Main Window by default shuts down the Application and your two instances of Window1 are waiting on each other (A.K.A. a deadlock)! Albeit seemingly only after making the Application invisible (so it seems to have closed).
If you still must use use LoadComponent() on Window1 I think you would need to not make it your startup uri by changing the StartupUri of your Application:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml"> <!-- change this -->
</Application>
Or change Application.ShutdownMode ( http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx ) to OnLastWindowClose:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml"
ShutdownMode="OnLastWindowClose">
</Application>
I have build you application on Windows 7 32bit under .Net 4.0 and 3.5.
I works fine for me. I think you problem is configuration specific.
Which configuration do you have? Do you reference any assemblies except default WPF project references?

Resources