Capture Activated event for an App - c#

I am referring to http://msdn.microsoft.com/en-us/library/windows/apps/hh464925.aspx#app_activation
// App is an Application
public App()
{
this.InitializeComponent();
// Doesn't compile
//this.Activated += OnActivated;
this.Suspending += OnSuspending;
}
protected override void OnActivated(IActivatedEventArgs args)
{
System.Diagnostics.Debug.WriteLine("OnActivated");
}
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
deferral.Complete();
}
Note, OnActivated will never be triggered. OnSuspending will be triggered, after I quit the app around 30 seconds.
How can I capture Activated event? It is weird that I do not find Activated event in App, although the documentation says so.

The activation is a little confusing. Take a look at the documentation here:
http://msdn.microsoft.com/en-us/library/windows/apps/br242324.aspx
What you'll find is that when the user taps the tile, only the OnLaunched event is fired (see documentation here: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.onactivated.aspx )
The OnActivated is only for special cases outside of the ordinary launch. That would be any one of the following:
OnFileActivated
OnSearchActivated
OnShareTargetActivated
OnFileOpenPickerActivated
OnFileSavePickerActivated
OnCachedFileUpdaterActivated
So if you truly want something that is called anytime the app is activated regardless of how, I'd suggest making your own private method, then calling it from both OnLaunched and OnActivated. That should hit all cases for activation.

Maybe the problem is that you launch the app normally for example, by tapping the app tile(without facts, I'm just guessing). In this case, only the OnLaunched method is called.
msdn

Related

WPF: Unable to raise Window.Closing event from app module

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.

UWP c# app with two kinds of onActivated listeners available - Activation by notification

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

c# WPF how to delay autostart of an application?

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

How do I use the event VisibilityChanged?

I need to re-run the code contained within dela method OnNavigatedTo () when the app resumes from background.
To do this I need the event VisibilityChanged:
Link MSDN
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
//My code
}
With this event, each time the app is opened again from the background, the code contained within dell'OnNavigatedTo runs again. How can I use that event? I can not.
Visibility changed is only relevant if the page isn't in the background. Every time the app opens it goes to OnNavigatedTo(), you can make a bool or counter to check if it is the first time you entered the page and then decide what to do based on that inside OnNavigatedTo. for Example:
private override void OnNavigatedTo(NavigationEventArgs e)
{
if(hasBeenHere) Repeat_Visit(args);
else First_Visit(args);
}
The Application_Activated Event gets fired when the application is resumed from the background.
From the App.xaml.cs of the WP8.1 Template:
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
// your code
}
Edit : to call methods on MainPage from app.xaml.cs has already been answered here : How to use a method in MainPage from app.xaml.cs with variable appbar pivot

OnNavigatedFrom is not called when the app is suspended

Today I've encountered strange problem - seems that on my device (v. 8.10.14203.306) OnNavigatedFrom event is not getting called when the app is being suspended. As far as I remember some time ago it was working ok, and exacly as documentation stays:
Note On Windows Phone, OnNavigatedFrom() is called when the app is suspended. OnNavigatedTo() is not called when the app is resumed.
I've tried a simple example (available at GitHub):
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedFrom(e);
Debug.WriteLine("Navigation");
this.Background = new SolidColorBrush(Colors.Red);
}
when there is normal navigation (e.g. by back button) the event is fired, but no more when the app is suspended - both in debug and release mode. I've also checked if the Suspending event is fired and it turnes out that it is:
// uncomment this to check if app is being suspended
App.Current.Suspending += (sender, e) => this.Background = new SolidColorBrush(Colors.Blue);
Am I missing something?
The only thing I can suggest is to make sure that you're calling Frame.GetNavigationState from within your Application.Suspending handler (this is usually done by SuspensionManager.SaveAsync). According to the docs:
Calling this method will call Page.OnNavigatedFrom for the current page using NavigationMode.Forward. GetNavigationState is usually called when the application is being suspended, so the current page is navigated away from.

Categories