I tried Cortana voice command to start my app,
but it works only when app is suspended. When app is closed, it shows Splashscreen and then app fails. I'm not able to to catch any exception.
A have exactly the same code as in https://msdn.microsoft.com/en-us/library/dn630430.aspx and Microphone Capability.
When a Voice Command activates the app, this method is going to be called and OnLaunched is not. Because of that we need similar code to the code we have in OnLaunched
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
rootFrame.CacheSize = 1;
Window.Current.Content = rootFrame;
rootFrame.Navigate(typeof(MainPage));
}
When an app is opened using Voice Commands, you need to handle it on app.OnActivated method:
protected override void OnActivated(IActivatedEventArgs e)
{
// Handle when app is launched by Cortana
if (e.Kind == ActivationKind.VoiceCommand)
{
VoiceCommandActivatedEventArgs commandArgs = e as VoiceCommandActivatedEventArgs;
SpeechRecognitionResult speechRecognitionResult = commandArgs.Result;
string voiceCommandName = speechRecognitionResult.RulePath[0];
string textSpoken = speechRecognitionResult.Text;
IReadOnlyList<string> recognizedVoiceCommandPhrases;
System.Diagnostics.Debug.WriteLine("voiceCommandName: " + voiceCommandName);
System.Diagnostics.Debug.WriteLine("textSpoken: " + textSpoken);
switch (voiceCommandName)
...
If you need more information how to integrate it, please see this link
Related
I'm trying to launch my app when the user logs in to Windows. I have the appropriate Extension (StartupTask) set in Package.appxmanifest, and I can get the app to launch when I log in to Windows, as expected. However, the app crashes after showing the Splash screen for about a second or two.
In my App.xaml.cs file, I have overridden the OnLaunched (called when the user launches the app) and OnActivated (called when the system launches the app after Windows login). Both call the same function to initialize my app; however, the app crashes only when the app is initialized from the OnActivated function. When initialized from OnLaunched, it works as expected. Here is the relevant code, from App.xaml.cs:
// Called when the user launches the app
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
// Calling InitializeApp here, the app launches without problem
InitializeApp(e);
}
// Called when the system launches the app after Windows login
protected override void OnActivated(IActivatedEventArgs e)
{
base.OnActivated(e);
// Calling InitializeApp here will cause the app to crash
InitializeApp((LaunchActivatedEventArgs)e);
}
// initialize the app
async void InitializeApp(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
var assembliesToInclude = new List<Assembly>()
{
typeof(CachedImage).GetTypeInfo().Assembly,
typeof(CachedImageRenderer).GetTypeInfo().Assembly
};
Xamarin.Forms.Forms.Init(e, assembliesToInclude);
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
if (session == null)
{
// prevent the app from stopping when minimized
session = new ExtendedExecutionSession();
session.Reason = ExtendedExecutionReason.Unspecified;
session.Revoked += (s, a) => { };
var result = await session.RequestExtensionAsync();
if (result == ExtendedExecutionResult.Denied)
System.Diagnostics.Debug.WriteLine("EXTENDED EXECUTION DENIED");
}
}
The problem is that you are trying to cast the IActivatedEventArgs to LaunchActivatedEventArgs, but when startup activation happens, the type is actually StartupTaskActivatedEventArgs.
Luckily, you actually need the e parameter only for Xamarin.Forms.Forms.Init which actually accepts IActivatedEventArgs, so you can just change the parameter of InitializeApp to be IActivatedEventArgs and remove the cast to LaunchActivatedEventArgs in OnActivated.
I can start an application using app-protocol feature in windows 10 and universal windows application.
To do that first i declare a protocol in Package.appxmanifest file in application B then from my main application which call application A, run this code to run application B :
var success = await Launcher.LaunchUriAsync(new Uri("MyApplicationProtocolName:"));
But i face a problem, when main application is startup, i cannot lunch application B, how can i do that ?
The problem is that declaring the protocol itself is not enough for the application to respond to it. You also need to implement the protocol activation in app B:
public partial class App
{
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
Window.Current.Activate();
}
}
}
The initialization you need to perform in OnActivated will probably be similar to OnLaunched in case the app is not yet launched. In case the app is already running, you don't need to do anything special, it will just come to the foreground. In case it is not running, you have to create the main Frame, initialize the Window.Current.Content and then do Window.Current.Activate() to activate the app.
Take a close look at the example code from Microsoft on this subject.
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/AssociationLaunching
See also:
https://learn.microsoft.com/en-us/windows/uwp/launch-resume/launch-app-with-uri
In my UWP app when i click on mobile back button app get close, so add this code in app.xaml.cs
private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame.CanGoBack && rootFrame != null)
{
rootFrame.GoBack();
}
else
{
var msg = new MessageDialog("Confirm Close! \nOr Press Refresh Button to Go Back");
var okBtn = new UICommand("OK");
var cancelBtn = new UICommand("Cancel");
msg.Commands.Add(okBtn);
msg.Commands.Add(cancelBtn);
IUICommand result = await msg.ShowAsync();
if (result != null && result.Label == "OK")
{
Application.Current.Exit();
}
}
}
and
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
/* Because of this line my app work on mobile great but when
when i debug on pc it through exception "show in image" */
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
After Done all this code when i debug app on phone, app successfully run -
Mobile Debuging:
But when debug in pc with same code, it show this error- PC Debuging:
when i remove HardwareButtons.BackPressed += HardwareButtons_BackPressed; then issue with pc debugging resolved but in mobile debuging back button again is not work.
The reason is that HardwareButtons API is not the universal solution for handling the back button. This API is available only in the Mobile extension SDK and trying to invoke it on other SKU will cause this exception, because the type is not available.
To enable the same functionality on all systems, you will need to use the new universal back button event:
SystemNavigationManager.GetForCurrentView().BackRequested += BackButtonHandler;
This will work the same on phone, PC, tablets,Xbox One, Surface Hub and HoloLens.
On PC this button is not shown by default, so you have to display it manually or create your own. To show the Back button in window's title bar, use:
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
AppViewBackButtonVisibility.Visible;
It is recommended that you hide this button once Frame.CanGoBack is false, because in that case the button is no longer useful. You should do this after each navigation of the frame. Best place to do this is when setting up the root frame in App.xaml.cs:
Frame rootFrame = Window.Current.Content as Frame;
rootFrame.Navigated += UpdateAppViewBackButton;
Now the handler could look like this:
private void UpdateAppViewBackButton( object sender, NavigationEventArgs e )
{
Frame frame = (Frame) sender;
var systemNavigationManager = SystemNavigationManager.GetForCurrentView();
systemNavigationManager.AppViewBackButtonVisibility =
frame.CanGoBack ? AppViewBackButtonVisibility.Visible :
AppViewBackButtonVisibility.Collapsed;
}
On Application close
I have also noticed that you are using Application.Current.Exit(); to exit the app. This is however not recommended. Once the user chooses OK in the dialog, you should rather set the e.Handled = false and let the system handle the app close manually. This will ensure the app suspension will run as expected and the app will stay in memory if the system has enough resources and will then launch faster again. Application.Current.Exit() kills the application and it is not recommended for UWP apps.
One thing to remember is that on Desktop, there is currently no way to catch the user clicking the close button in the app's title bar, so your confirmation dialog will not display in that case unfortunately.
I am struggling with it for a few hours and can't find working solution. My app is a target app for sharing and the problem is when it's running and user wants to share content.
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
await OnInitializeAsync();
if (await CheckToken(args) != true) return;
if (args.PreviousExecutionState != ApplicationExecutionState.Running)
{
if (await LoadData(args) != true) return;
}
var frame = new Frame();
var navigationService = new NavigationService(_dispatcherService) { RootFrame = frame, };
Window.Current.Content = frame;
Window.Current.Activate();
navigationService.Navigate<ShareViewModel>(args.ShareOperation);
}
The problem is that I can't use frame from running application because I get an exception "marshalling thread ...." so I create a new frame and I assign it to Window.Current.Content. This works fine but the problem is when user finishes sharing. What should I do? It seems that I should assign previous frame to Window.Current.Content which was "overriden" by sharing target right? While I try to do it I get again "marshalling thread" exception. If I don't do it then I can't interact with my application because I get an exception that app is being closed. What is the proper scenario for being a sharing target?
Edit: I guess it's important to mention that I call ReportStarted() when I send message in ShareViewModel and ReportCompleted() when I am done.
Exception thrown when I try to assign frame back:
{"The application called an interface that was marshalled for a different thread.\r\n\r\nFailed to initialize the application's root visual"}
I'm pasting the solution which solved the issue. I think the key here is to use
CoreWindow.GetForCurrentThread().Dispatcher
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
await OnInitializeAsync();
if (await CheckToken(args) != true) return;
if (args.PreviousExecutionState != ApplicationExecutionState.Running)
{
if (await LoadData(args) != true) return;
}
var frame = new Frame();
Window.Current.Content = frame;
var dispatchService = new DispatcherService() { Dispatcher = CoreWindow.GetForCurrentThread().Dispatcher };
var navigationService = new NavigationService(dispatchService) { RootFrame = frame };
navigationService.Navigate<ShareViewModel>(args.ShareOperation);
Window.Current.Activate();
}
I have a Windows Phone 8.1 Universal App that I am working on adding basic Cortana support to. A lot of the articles about this are for Silverlight etc. - I'm finding it hard to find really good information about this.
So far, I have activation working if the app is already running or suspended. However, if the app is completely exited, then upon activation it crashes immediately. I've tried using Hockey and a simple "LittleWatson" routine to catch the crash, but it seems to happen too soon to be caught. I've seen some references to doing a private beta and trying to get the crash dump, but I didn't have any luck with that so far.
Here's what my activation code looks like in app.xaml.cs:
protected override void OnActivated(IActivatedEventArgs args) {
base.OnActivated(args);
ReceivedSpeechRecognitionResult = null;
if (args.Kind == ActivationKind.VoiceCommand) {
var commandArgs = args as VoiceCommandActivatedEventArgs;
if (commandArgs != null) {
ReceivedSpeechRecognitionResult = commandArgs.Result;
var rootFrame = Window.Current.Content as Frame;
if (rootFrame != null) {
rootFrame.Navigate(typeof(CheckCredentials), null);
}
}
}
}
and here is my check for the command result:
private async Task CheckForVoiceCommands() {
await Task.Delay(1); // not sure why I need this
var speechRecognitionResult = ((App)Application.Current).ReceivedSpeechRecognitionResult;
if (speechRecognitionResult == null) {
return;
}
var voiceCommandName = speechRecognitionResult.RulePath[0];
switch (voiceCommandName) {
// omitted
}
((App)Application.Current).ReceivedSpeechRecognitionResult = null;
}
I'm pretty sure from inserting messages etc. that it fails long before it gets this far.
There's likely something easy I'm missing but I don't know what...
What is causing the crash so early?
EDIT One thing I tried is using the "debug without launch" configuration to try to catch the exception. When I do this, the app appears to hang forever connected in the debugger on the splash screen. However, that did let me force a break. It hangs in
global::Windows.UI.Xaml.Application.Start((p) => new App());
which as best I can tell, just tells me the app is hanging somewhere. That's the only line in the call stack.
Copy a segment of the OnLaunched code into OnActivated like in the example below. OnLaunched is not called when the App is Activated and it does some essential work like activating the window.
protected override void OnActivated(IActivatedEventArgs args)
{
// When a Voice Command activates the app, this method is going to
// be called and OnLaunched is not. Because of that we need similar
// code to the code we have in OnLaunched
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
rootFrame.CacheSize = 1;
Window.Current.Content = rootFrame;
rootFrame.Navigate(typeof(MainPage));
}
Window.Current.Activate();
// For VoiceCommand activations, the activation Kind is ActivationKind.VoiceCommand
if(args.Kind == ActivationKind.VoiceCommand)
{
// since we know this is the kind, a cast will work fine
VoiceCommandActivatedEventArgs vcArgs = (VoiceCommandActivatedEventArgs)args;
// The NavigationTarget retrieved here is the value of the Target attribute in the
// Voice Command Definition xml Navigate node
string target = vcArgs.Result.SemanticInterpretation.Properties["NavigationTarget"][0];