I am making a Windows 8.1 speech based application. My problem is that when I give Cortana the input it launches my app and the app closes at the splashscreen, but when I run my app in background (minimize the app) or when the app is running, the Cortana input works perfect.
Where am I going wrong? Here is my app.xaml.cs code, in the OnActivatedMethod:
if (args.Kind == ActivationKind.VoiceCommand)
{
VoiceCommandActivatedEventArgs vcArgs = (VoiceCommandActivatedEventArgs)args;
string voiceCommandName = vcArgs.Result.RulePath.First(); // What command launched the app?
switch (voiceCommandName) // Run the action specific to the command
{
case "comand1": // User said comand1
rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
break;
case "comand2": // User said comand2
rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
break;
case "comand3": // User said comand3
rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
break;
case "comand4":
rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
break;
}
and in the OnNavigated method of the speech handling page:
SpeechRecognitionResult vcArgs = e.Parameter as SpeechRecognitionResult;
RcvdCommand = vcArgs.Text.ToUpper();
// Check for null!
string commandMode = vcArgs.SemanticInterpretation.Properties["commandMode"][0];
if (commandMode == "voice") // Did the user speak or type the command?
{
RequestHanding();
MyTextblock.Text = vcArgs.Text;
// SpeakText(audioPlayer, String.Format(" app heard you say {0}", RcvdCommand ));
// HandleNlpCommand(vcArgs);
}
else if (commandMode == "text")
{
// messageTextBox.Text = string.Format("Working on your request \"{0}\"", RcvdCommand);
// HandleNlpCommand(vcArgs);
}
When the application is not running, and is activated by voice command, the OnLaunched() method is not called. So you need to call the code that ensures the root frame is created in the OnActivated() method as well:
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();
// ... restore app state, etc.
Window.Current.Content = rootFrame;
}
Related
How can i get the click event of notification from notification history
Using
toastNotification.Activated += ToastNotification_Activated;
but it's not working when I click on notification popup from history
You need to override OnActivated in App.Xaml.cs, and you will get ToastNotificationActivatedEventArgs in OnActivated method like the follow. For more please refer Send a local toast notification
protected override void OnActivated(IActivatedEventArgs e)
{
// Get the root frame
Frame rootFrame = Window.Current.Content as Frame;
// TODO: Initialize root frame just like in OnLaunched
// Handle toast activation
if (e is ToastNotificationActivatedEventArgs)
{
var toastActivationArgs = e as ToastNotificationActivatedEventArgs;
// Parse the query string (using QueryString.NET)
QueryString args = QueryString.Parse(toastActivationArgs.Argument);
// See what action is being requested
switch (args["action"])
{
// Open the image
case "viewImage":
// The URL retrieved from the toast args
string imageUrl = args["imageUrl"];
// If we're already viewing that image, do nothing
if (rootFrame.Content is ImagePage && (rootFrame.Content as ImagePage).ImageUrl.Equals(imageUrl))
break;
// Otherwise navigate to view it
rootFrame.Navigate(typeof(ImagePage), imageUrl);
break;
// Open the conversation
case "viewConversation":
// The conversation ID retrieved from the toast args
int conversationId = int.Parse(args["conversationId"]);
// If we're already viewing that conversation, do nothing
if (rootFrame.Content is ConversationPage && (rootFrame.Content as ConversationPage).ConversationId == conversationId)
break;
// Otherwise navigate to view it
rootFrame.Navigate(typeof(ConversationPage), conversationId);
break;
}
// If we're loading the app for the first time, place the main page on
// the back stack so that user can go back after they've been
// navigated to the specific page
if (rootFrame.BackStack.Count == 0)
rootFrame.BackStack.Add(new PageStackEntry(typeof(MainPage), null, null));
}
// TODO: Handle other types of activation
// Ensure the current window is active
Window.Current.Activate();
}
I am creating a uwp app with some media and video features, I have tested the app repeatedly on debug mode and it works just perfect as desired, after commenting and uncommenting of some code I was able to figure out that recursion of a specific method is causing the app to crash.
I also tried to display the exception with message dialog ( for release mode ). But no exception caught and app just freezes and crashed.
I run the app once on debug mode and then stop visual studio and then just directly open the app from my PC to test it on release mode.
MyScenario:
I am using OnFileActivated event to open video files directly on double click in my PC with my app. That works just fine. Below I will show you the code and reason for crashing. Please let me know how to solve this.
Thanks in advance.
Code
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
//Some Media Stuff which works fine everytime even in release mode.
await speechRecognizer.CompileConstraintsAsync();// I am using speech input to play pause my media player.
ListenandAct();
}
private async void ListenandAct()
{
var result = await speechRecognizer.RecognizeAsync();
if (result.Text == "Pause" || result.Text == "pause")
{
Media.Pause();
}
else if (result.Text == "Play" || result.Text == "play")
{
Media.Play();
}
//ListenandAct(); //if I comment this line for recursion the app doesnt crash even in release mode, but If I uncomment it than app works perfect in debug mode but crashes in release mode.
}
OnFileActivated method in app.xaml.cs
protected async override void OnFileActivated(FileActivatedEventArgs args)
{
string ParentName = "";
if (args.Files.Count > 0)
{
Type TargetType = typeof(MainPage);
if (args.Files.Count == 1)
{
SingleFileToPlay = args.Files[0] as StorageFile;
TargetType = typeof(SingleFilePlay);
}
else if (args.Files.Count > 1)
{
ParentName = (await (args.Files[0] as StorageFile).GetParentAsync()).DisplayName;
MultiFilesToPlay = new List<StorageFile>();
TargetType = typeof(MultiFilePlay);
foreach (var file in args.Files)
{
MultiFilesToPlay.Add(file as StorageFile);
}
}
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;
// 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(TargetType, ParentName);
//}
// Ensure the current window is active
Window.Current.Activate();
}
}
NOTE
When I run a video file by double clicking it , it works perfect without crashing, but when my app is already running a video file and I double click another video file again then my app tries to run the second video file, the second video file is shown on the app as it is suposed to and then app freezes and crashes after a couple of seconds.
I'm writing a UWP app, and I have a ScheduledToastNotification that is added to the schedule when the app is suspended (e.g. like a reminder). However, if I close the app, the notification appears on time, but when I click on the notification (no buttons, just on the notification in general), the app doesn't launch correctly, stopping at the splash screen.
How do I get the app the re-launch correctly?
Thanks.
You should override OnActivated in App.Xaml.cs and handle this like
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.ToastNotification)
{
var toastArgs = args as ToastNotificationActivatedEventArgs;
var arguments = toastArgs.Argument;
if (arguments == "ARG")
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
Window.Current.Content = rootFrame;
}
rootFrame.Navigate(typeof(YOURPAGE));
Window.Current.Activate();
}
}
}
this is my first time to use stackoverflow :) ,
I was working with grid template app in windows 8 store app and i wanted to add welcome page after the splash screen loaded , to do that i tried to add this statement
this.Frame.Navigate(typeof(WelcomePage));
to the OnLaunched method that was created by default with the grid template in the App.xmal.cs. after i created the Welcomepage it self and i created a button in the welcome page to navigate to the GroupedItemsPage.
My logic isn't working.
this is the default method code in the App.xmal.cs if that will help
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
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();
//Associate the frame with a SuspensionManager key
SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
// Restore the saved session state only when appropriate
try
{
await SuspensionManager.RestoreAsync();
}
catch (SuspensionManagerException)
{
//Something went wrong restoring state.
//Assume there is no state and continue
}
}
// 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
if (!rootFrame.Navigate(typeof(GroupedItemsPage), "AllGroups"))
{
throw new Exception("Failed to create initial page");
}
}
// Ensure the current window is active
Window.Current.Activate();
}
I want to enter win32 desktop from a metro app when launch the metro app (press the app's tile on metro startup screen). One way is to open a file (e.g. a TXT file) when start the metro app. I add the following code logic into OnLaunched, sometimes it can open the file and enter desktop, but sometimes, it doesn't. Could someone help me? (just create a blank app in VS2012).
async protected override void OnLaunched(LaunchActivatedEventArgs args) {
// Do not repeat app initialization when already running, just ensure that the window //is active
if (args.PreviousExecutionState == ApplicationExecutionState.Running)
{
Window.Current.Activate();
return;
}
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Create a Frame to act navigation context and navigate to the first page
// var rootFrame = new Frame();
if (!rootFrame.Navigate(typeof(MainPage)))
{
throw new Exception("Failed to create initial page");
}
// Place the frame in the current Window and ensure that it is active
Window.Current.Content = rootFrame;
Window.Current.Activate();
{
string txtfile = #"Assets\a.txt";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(txtfile);
if (file != null)
{
bool success = await Windows.System.Launcher.LaunchFileAsync(file);
if (success)
{
}
else
{
}
}
}
}
BatRT allows you to execute a batch file from a metro app. This could be used to run any desktop application from a metro app. As soon as the metro app starts execute a batch file to run your desired desktop application.