I'm developing Windows 10 universal app in C#/XAML.
I want to implement such a policy, that whenever user closes my app (and some other conditions are met, but its irrelevent here) an adverisment will display.
My question is how can I intercept and cancel/handle an event when application is being closed? This is easy when user decides to close the app by for example pressing a button that I'll define in XAML, but what if he presses Alt+F4? In Winforms this is easy as well:
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += Form1_FormClosing;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
But how can I achieve similiar effect in in Windows 10 universal app?
I'm afraid there is no option to cancel the closing of the app or even delay it. The Suspending event is the only indication your app will receive prior to termination (if it happens). Check out the Application.Suspending event: https://msdn.microsoft.com/en-us/library/windows.ui.xaml.application.suspending.aspx
No, you can not achieve the similar effect in Windows 10 Universal App (and Windows 8.1 Store App), because in the modern app, the user have the full control of the app and the app can not stop the user closing a app.
If you have sth need to handle when user closing the application, as Lukkha stated, you can handle them in Application.Suspending, but there is a time limitation by default, all of the things should be done within 5s. If you want to have more than 5s, you need to request a ExtendedExecutionSession.
Using Extended Execution in Windows 10 Universal Apps
Related
I'm developing a class library for windows 10 universal apps (mobile and desktop device families only). I need to invoke an event if the user has been idle(no touch, mouse move, key press etc) for x number of seconds. This method can be used to solves this problem on android. But I couldn't find a solution on windows UWP.
Is there an API available in UWP to achieve this?
You can detect global input with various events on the app's CoreWindow:
Touch and mouse input with CoreWindow.PointerPressed, PointerMoved, and PointerReleased.
Keyboard input: KeyUp and KeyDown (the soft keys) and CharacterReceived (for characters generated via chords & text suggestions)
Use these to detect the user is active and idle out if it goes too long without any of these events.
I know this is really old question, but I think you can now get to same result with RegisterBackgroundTask
Just set:
new TimeTrigger(15, false) //For time trigger
Link
new SystemCondition(SystemConditionType.UserNotPresent)) //And so you want to know so user is not present
Link
Example usage in App.xaml.cs:
var builder = new BackgroundTaskBuilder();
builder.Name = "Is user Idle";
builder.SetTrigger(new TimeTrigger(2, false)); //two mins
builder.AddCondition(new SystemCondition(SystemConditionType.UserNotPresent));
// Do not set builder.TaskEntryPoint for in-process background tasks
// Here we register the task and work will start based on the time trigger.
BackgroundTaskRegistration task = builder.Register();
task.Completed += (sender, args) =>
{
//Handle user not present (Idle) here.
};
Have a broblem with minimize application, when back button pressed:
protected override void OnBackKeyPress(CancelEventArgs e)
{
}
I need do not close application, just minimize him to background, have any idea for this?
You should close on back - otherwise you will fail certification for the store. If you need a background task running, instead use a Background Agent
More precise:
You can activate fast resume in the manifest:
http://msdn.microsoft.com/en-us/library/windows/apps/jj735579(v=vs.105).aspx
This will keep the instance of your app in the memory until the system cleans up the oldest apps to free memory when needed.
This is the recommended behavior and the default for all 8.1 Runtime Apps.
Hint:
Once put to the background, you app will still be suspended and no longer active.
It will be resumed once it gets back to the foreground.
I want to integrate some voice commands in my windows phone 8.1 app.
The first thing I want to do is to open my app by a voice command and navigate to a certain page.
According to MSDN article Quickstart: Voice commands (XAML) I can use the override of protected virtual void OnActivated(IActivatedEventArgs args) method in App.xaml.cs to meet my requirements. But it does'nt work the way I though it would!
I have the method with the following structure:
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.VoiceCommand)
{
var commandArgs = args as VoiceCommandActivatedEventArgs;
if (commandArgs != null)
{
// ... some logic here
}
}
}
The problem is when I'm activating my app by saying "Open 'name of my app' [optional words]" the app opens but the Activated event never fires! The app opens and OnLaunched event fires. So I can't even enter the OnActivated method.
Does anyone know the problem? Why can't I enter OnActivated method using voice commands?
P.S. I tried it with a simulator as well as with a real device.
you can see this article,
http://t.co/Q5hRxRPvwR
is in spanish, but you will understand.
After you install the app and run it, the xml should be installed, like said in documentation.
After ask to cortana "What can I say?" it will show all you can said, and the apps that supports cortana. Choose you app and you will see what you can say for your app, like
If you say what your app can listen, your app will be activated.
I have a below requirement in Windows Phone 8 to run voice listener in background agent whenever an incoming call comes or for an outgoing call. The voice listener should stop when there was NO call.
Eg: When I lift an incoming call and while speaking. I would like to say a voice command say "SPEAKER", then the speaker should ON
I saw obscured events can be used for detecting calls. But am unable to start the voice listener from background agent. Kindly please assist.
Here is the method I am using in the ScheduledAgent.cs file, but no luck
private async void SpeakTest()
{
SpeechRecognizerUI speechRecognition = new SpeechRecognizerUI();
SpeechRecognitionUIResult recoResult = await speechRecognition.RecognizeWithUIAsync();
if (recoResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded)
{
lblMessage.Text = recoResult.RecognitionResult.Text;
}
}
First, You can not run a background task whenever you want. OS will decide when to run your task agent. It'll not run continuously. Rather it'll run in interval
Second, In windows phone 8 ( not elsewhere) the microphone is muted to external app.
Third, You shouldn't try to run SpeechRecognizerUI from background agent.
As windows phone 8 provides us with this method for programmatically terminate an app, will there be any issue while app submission if we use this in app for terminating a page while there is no backentry in the navigation history?
There won't be any issue in certification when using this call, but make sure you have saved all data in your app when calling this, because this call effectively kills your app immediately - ApplicationClosing even handler won't be raised after it!
Why would you call Application.Terminate when navigating back with an empty back stack? Just let the app close itself. Seems a bit pointless to me to overuse Application.Terminate().
I can't say much about the new Terminate method, but I do have an app (NOTE: Not a game) that does the following at certain points
private void Kill()
{
new Microsoft.Xna.Framework.Game().Exit();
}
This app passed certification without any problems. This was an app for both WP7 and WP8 so I did not have the ability to use Terminate().