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];
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
I would like to open my app after another app choose my app as the target for sharing a web link.
So first I use OnShareTargetActivated to read the weblink and then use LaunchUriAsync to launch my app through a custom scheme I created for it.
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
ShareOperation shareOperation = args.ShareOperation;
try
{
if (shareOperation.Data.Contains(StandardDataFormats.WebLink))
{
var URL = await shareOperation.Data.GetWebLinkAsync();
var mAloudURI = #"myApp:?URL=" + URL;
// Launch the URI
await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
var success = await Windows.System.Launcher.LaunchUriAsync(new Uri(mAloudURI));
});
shareOperation.ReportCompleted();
}
}
catch (Exception exc)
{
var i = 0;
}
}
After that, I use OnActivated to read the link and open my MainPage on the passed link.
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
var url = eventArgs.Uri.PathAndQuery.Substring("?URL=".Length);
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)
{
rootFrame.Navigate(typeof(MainPage), url);
}
}
}
When I'm debugging I get the expected behavior when I F10 (step through) the line calling LaunchUriAsync, then OnActivated gets executed and MainPage gets displayed with the URL I wanted...
However if I try to share again, the secondary popup window for my app gets displayed for a while and then it disappears. Non of my break points get hit...
Is this the correct approach?
This looks to me like a very logical way of opening other apps with the content you are sharing to them...
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 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