I am starting a new WPF project and I need lighting on the launch of the application.
I watched 2 different WPF projects to see how to begin.
The first one starts with the void App_Startup(object sender, StartupEventArgs e) method in the App.xaml.cs file, called directly from the App.xaml file (Startup="App_Startup").
The other one starts with the protected override void OnStartup(StartupEventArgs e) method in the App.xaml.cs file.
In addition of that, when I started my WPF project, the default code template starts in a third way with StartupUri="MainWindow.xaml" in the App.xaml file.
What are the differences between this three manners?
Startup is an event that is raised by the OnStartup method of the Application base class as you can see in the reference source. This is how it's implemented:
protected virtual void OnStartup(StartupEventArgs e)
{
VerifyAccess();
StartupEventHandler handler = (StartupEventHandler)Events[EVENT_STARTUP];
if (handler != null)
{
handler(this, e);
}
}
So if you override OnStartup in your App class and call base.OnStartup(e), the event will be raised and any attached event handler will be invoked. Whether you implement your logic in the event handler or directly in the overridden OnStartup is a matter of personal or application specific preference. There are no recommendations on why one should be better than the other.
When it comes to the StartupUri, it only makes sense to set this when you simply want to display a default window immediately on startup and you don't have any custom initialization logic.
In enterprise apps, it's common to have some kind of bootstrapper that sets up the application and its dependencies and create and show the main window programmatically rather than using the StartupUri property.
Startup="App_Startup" is just event handler for your application, which is started in App.xaml. It has event StartUp which you can subscribe to. If you want to have some "pre-loading" logic, this is perfect place.
If you want to have just your window shown, it is enough to specify URI (in StartUpUri) of that window, so application knows which file to load and what window to show :)
OnStartup raises event of application start (in base.OnStratUp), from the documentation:
OnStartup raises the Startup event.
A type that derives from Application may override OnStartup. The overridden method must call OnStartup in the base class if the Startup event needs to be raised.
There also can be put some pre-loading logic.
There is none recommended way, it is up to you and what you need.
Related
I take charge of a development of an older WPF modular application using Prism Library for WPF. In this case, entry point to the application for me is an overriden Initialize() method since I have no access to the Application.MainWindow's App class. This class along with some other helper classes is compiled to EXE file and DLL's.
Currently I'm facing to a problem that I have to catch the Window.Closing event which is not raised during closing the application. Normally this piece of code which is put into the constructor (in this specific case into Initialize() method) is working as expected
Application.Current.MainWindow.Closing += (s, e) =>
{
e.Cancel = true;
};
On the other hand, event Window.Closed is fired up without any issues.
In my opinion it's not possible to associate this event handler outside of Application.MainWindow's constructor, or do I something wrong? Please help me.
The issue was located. The OnClosing method which raises the Closing event is implemented in the shell and it looks like this
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
ViewModel.StartShutdownSequence();
}
The StartShutdownSequence() encapsulates some logic regarding handling individual modules and at the end it calls Application.Current.Shutdown() which definitely shutdown the application.
Currently in my apps i am registering an onActivated listener to be executed and checked for my sharing activity like so:
Window.Current.Activated += Current_Activated;
with the method as follows to check for the activation state:
private async void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
{
if (e.WindowActivationState == CoreWindowActivationState.Deactivated)
{
}
else
{} }
Now i would like to implement an activation from a notification from a background task. The click on the notification is currently opening the app or brings it to the foreground if already open. This is standard behaviour. Now I would like to execute some code on activation by the notification but my method never gets called and I am not completely understanding Microsofts online material as it seems relatively simple.
I am registering the listener as such:
(Application.Current as App).Activated = new EventHandler<IActivatedEventArgs>(App_Activated);
and the method that never gets called looks like this:
private void App_Activated(object sender, IActivatedEventArgs e)
{ }
Are those two conflicting each other? is there something I can do to implement the notification activation into my existing method?
I've read through this and many more pages: https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/07/08/quickstart-sending-a-local-toast-notification-and-handling-activations-from-it-windows-10/
Even from a test app i downloaded i don't really understand what needs to be done. Besides that i don't need to pass any info from the notification to the main activity. Simply just open it and execute one command of refreshing the page, but only on notification activation.
You should just override the OnActivated(IActivatedEventArgs args) method in your App.xaml.cs and check whether the args.Kind property is ActivationKind.ToastNotification.
You can find more info about handling an UWP app activation here.
As Marian said, you need to override the OnActivated method in your App.xaml.cs class. The OnActivated method is called when your toast is clicked, and it contains the toast arguments, so that you know what toast was clicked. Note that you must perform the same Frame initialization in your OnActivated as you do your OnLaunched, since if the user clicks on a toast while your app is closed, only the OnActivated method will be called - the OnLaunched won't be called.
This Quickstart explains exactly how to handle toast activation, and includes a full code sample: Quickstart: Sending a local Toast notification
My question is how can i delay the autostart on user logon in an WPF Project. Because its necessary on this application.
I tried:
void OnStartup(StartupEventArgs e)
{
Thread.Sleep(60000);
base.OnStartup(e);
}
but it seems like the OnStartup method isnt aviable. Its always an error. So there is something i dont get.
You can always just use
public App()
{
System.Threading.Thread.Sleep(10000)
}
on the App.xaml.cs
Of as already mentioned you need to subscribe to the OnStartup event on xaml.
This answer might have what you're looking for:
I think what you really want to do is to subscribe to the Startup
event. You can do this in your XAML file
Click here to get started with WPF
Is there a way to subscribe to this event or something similar so I can perform some clean up codes before objects are finalized?
The event is static so using it in a DLL doesn't cause any problem.
Do beware however that the event is only fired if your class library is actually used from a Winforms application. You can only be sure of that if you expose functionality that is only usable from a Winforms app. Like a custom control or UserControl.
Alternatives are the AppDomain events, DomainUnload and ProcessExit. Or just expecting the main app to let you know about the shutdown. Which is usually the better choice, you don't necessarily know why the app is exiting. You wouldn't want to save settings on a hard crash for example. Note how the ApplicationSettingsBase class follows that pattern as well, you have to explicitly call its Save() method.
You subscribe to this event in the same manner you would with any event.
The example on the ApplicationExit MSDN page is clear:
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
private void OnApplicationExit(object sender, EventArgs e) {
// do your cleanups
}
I am currently developing a Windows app with several forms. I use Form_Shown in one of those forms to execute some code to initialize (refresh) the form before showing it.
In Form.Shown Event on MSDN, it states that the event is raised only the first time the form is shown. However, I want to be able to execute code to initialize my form every time that I call Form.Show() in some of the forms. Here's an example.
From a form named Game. Contains an event handler Game_Shown and a button that when clicked shows a form named Menu:
private void btnMenu_Click(object sender, EventArgs e)
{
this.Hide();
Formulaires.formMenu.Show();
}
private void Game_Shown(object sender, EventArgs e)
{
Code here...
this.Refresh();
}
From the form named Menu. Contains a button that when clicked shows the form named Game:
private void lblGame_Click(object sender, EventArgs e)
{
this.Hide();
Formulaires.formGame.Show();
}
It is behaving by design.
From the docs:
The Shown event occurs whenever the form is first shown.
Also, you should not handle the Shown event in your class, rather you should override OnShown.
To achieve what you want, you might try overriding the OnVisibleChanged method. Inside the method, if the form is visible, then execute your code.
Like the Shown event, you should not handle it in your form class, instead override the appropriate method:
From the docs:
The OnVisibleChanged method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
What you want requires some detailed knowledge about which event happens when in the WinForm lifecycle. That may be documented somewhere, I don't know.
This is how I would find out:
create a small test project with 2 forms (Main and helper)
add the show and hide buttons and make sure it works.
Add Debug.Print("EventName") to all the candidate events of the helper form.
Look at the log in the output window and pick your event.
Candidate events would be FormClosing, FormClosed, (De)Activated, Enter, Leave, Load, ... go through the list.
When you find the right one, please post it here in an answer.