I'm making web browser for UWP on all Windows versions. So, I'm noticed that when WebView navigating from YouTube site to YouTube video (when navigation not completed on that time), when I pressed the hardware back button, the app crashes. The strangest things are that this doesn't occur when the app is running from debug or release mode. Also, when the same code I write the same code on Back button of my app, the function works perfectly. Also, the same crash occurring when I'm writing some code on SystemNavigationManager.GetForCurrentView().BackRequested event no matter what. The same thing is on HardwareButtons.BackPressed. The code is:
private void CurrentView_BackRequested(object sender, BackRequestedEventArgs e)
{
e.Handled = true;
if (appView.IsFullScreenMode)
{
appView.ExitFullScreenMode();
}
else
{
Frame frame = Window.Current.Content as Frame;
if (!frame.CanGoBack && currentWebView.CanGoBack)
{
currentWebView.GoBack(); //the real function
}
else
{
if (PivotMain.Items.Count > 1)
{
CloseOneTab(PivotMain.SelectedWebViewItem);
}
else
{
e.Handled = false;
}
}
}
}
Update 1: the app doesn't crash when WebView is set on WebViewExecutionMode.SeparateThread. But somewhy I think that it's risky 🤷♂️.
Related
I've implemented this class of Microsoft.Windows.SDK.Contracts nuget package for monitoring the actions (play and pause) of a third party music player.
Everything works except that after some time the events are no longer triggered.
This is the code:
public void startMediaListener()
{
GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult().CurrentSessionChanged += callbackSessionChanged;
GlobalSystemMediaTransportControlsSession currentSession = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult().GetCurrentSession();
if (currentSession == null || !currentSession.SourceAppUserModelId.ToLower().Contains("appNameToMonitor"))
return;
Console.WriteLine("Started");
currentSession.PlaybackInfoChanged += callbackPlaybackStatus;
currentHashSession = currentSession.GetHashCode();
serviceRunning = true;
}
Event when session changed
private void callbackSessionChanged(GlobalSystemMediaTransportControlsSessionManager session, CurrentSessionChangedEventArgs e)
{
Console.WriteLine("Session changed" + session.GetCurrentSession().GetHashCode().ToString());
if (session.GetCurrentSession() == null || !session.GetCurrentSession().SourceAppUserModelId.ToLower().Contains("appNameToMonitor"))
return;
session.GetCurrentSession().PlaybackInfoChanged += callbackPlaybackStatus;
currentHashSession = session.GetCurrentSession().GetHashCode();
}
Event for detecting pause/play
private void callbackPlaybackStatus(GlobalSystemMediaTransportControlsSession session, PlaybackInfoChangedEventArgs e)
{
if (!session.SourceAppUserModelId.ToLower().Contains("spotify") || currentHashSession != session.GetHashCode())
return;
GlobalSystemMediaTransportControlsSessionPlaybackStatus status = session.GetPlaybackInfo().PlaybackStatus;
double currentTime = session.GetTimelineProperties().LastUpdatedTime.TimeOfDay.TotalMilliseconds;
//Do work...
}
I also check the hashcode because if I listen to Spotify then youtube then Spotify again the 2 sessions of Spotify have different hashcode and it will trigger the event twice (when it works) and I want to monitor only the last one.
The problem is that in startMediaListener() I can find the session of my app and everything works for few minutes but after some time the callbackPlaybackStatus is no longer called and meanwhile no session changed so I suppose that the session is still up but not firing the event.
Can you help me solve this mistery?
Ps: I've tried to put everything inside a thread but this solution doesn't works.
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 wish to display a message dialog on a page when the back button is pressed on that page.
I have already made the back button works as it should. So I am not including the full code that makes the back button work. What I need to is for the message dialog to show on the specific page (SomePage in this case).
I have this code in App.xaml.cs under the OnBackRequested event:
if (rootFrame.CanGoBack && rootFrame.SourcePageType.Name != "SomePage")
{
MessageDialog errormessage = new MessageDialog("Something");
errormessage.Commands.Add(new UICommand("Some Option", new UICommandInvokedHandler(this.CommandInvokedHandler)));
errormessage.ShowAsync();
}
private void _CommandInvokedHandler(IUICommand command)
{
Frame aFrame = Window.Current.Content as Frame;
if (command.Label == "Some Option")
{
aFrame.GoBack();
}
}
So after debugging line by line, System.ArgumentException in my application is written in the debugging console when the ShowAsync() function is triggered. I think I cannot call ShowAsync() on App.xaml.cs to show on SomePage but maybe I must call it on SomePage. I am not sure how to do it.
In my App you can open a Site where you can switch on and off the Flashlight.
The first time it works, but if I try to switch the flashlight on a second time the App crashes.
I think this is a Problem with AudioVideoCaptureDevice.OpenAsync. If I call it a second time the App crashes with a System.Reflection.TargetInvocationException WinRT-Informationen: Unable to acquire the camera. You can only use this class while in the foreground.
Someone know this Problem?
protected AudioVideoCaptureDevice Device { get; set; }
public Page10()
{
InitializeComponent();
}
async void tglSwitch_Checked(object sender, RoutedEventArgs e)
{
var sensorLocation = CameraSensorLocation.Back;
if (this.Device == null)
{
// get the AudioVideoCaptureDevice
this.Device = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
}
var supportedCameraModes = AudioVideoCaptureDevice
.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
{
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
// set flash power to maxinum
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower,
AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
this.tglSwitch.Content = "Light on";
this.tglSwitch.SwitchForeground = new SolidColorBrush(Colors.Green);
}
}
void tglSwitch_Unchecked(object sender, RoutedEventArgs e)
{
var sensorLocation = CameraSensorLocation.Back;
sensorLocation = CameraSensorLocation.Back;
var supportedCameraModes = AudioVideoCaptureDevice
.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
if (this.Device != null && supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.Off))
{
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
this.tglSwitch.Content = "Light off";
}
}
I would recommend to initialize the camera with OpenAsync ONE TIME in page lifecycle, for example in OnNavigatedTo event. And only makeSetProperty() methods calls code in your checkbox events to control light. It is also very important to dispose camera correctly then leaving the page, for example in OnNavigatedFrom event, by calling device.Dispose(). This option also make your flashlight to work faster.
Keep in mind that Windows Phone 8.1 now has dedicated API for torch, which works great and the code is more beautiful. You can use in Silverlight project as well, but you have to migrate your project. Here is more about this http://developer.nokia.com/community/wiki/Using_the_camera_light_in_Windows_Phone_7,_8_and_8.1 and https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.devices.torchcontrol.
I've been playing around with Kinect for Windows SDK 1.8 for a bit, just re-familiarizing myself with it after some time away. I have a basic application running that uses the color and skeleton streams to overlay a skeleton on a video feed of the user, while also displaying their torso's X, Y, and Z coordinates in realtime. All of this works perfectly, but I've run into an issue with shutting the application down. A first, my Window_Close event looked like this:
private void Window_Closed(object sender, EventArgs e)
{
// Turn off timers.
RefreshTimer.IsEnabled = false;
RefreshTimer.Stop();
UpdateTimer.IsEnabled = false;
UpdateTimer.Stop();
// Turn off Kinect
if (this.mainKinect != null)
{
try
{
this.mainKinect.Stop();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.TxtBx_KinectStatus.Text += "\n[" + DateTime.Now.TimeOfDay.ToString() + "] " + this.mainKinect.UniqueKinectId.ToString() + " has been turned off.";
}
// Shut down application
Application.Current.Shutdown();
}
I added the 'Application.Current.Shutdown()' only because my program would hang and never actually close when I closed the window. I stepped through the function to find that it hangs on this.mainKinect.Stop(), where mainKinect is the Kinect object referring to the physical Kinect. I thought that maybe it couldn't shut down both streams properly, so I added
this.mainKinect.ColorStream.Disable();
this.mainKinect.SkeletonStream.Disable();
just before the Stop(). I found out that it actually hangs on the SkeletonStream.Disable(), and I don't know why. Most of the rest of my code is straight from their examples, so I don't know why this doesn't work. If you have any ideas, or would like me to post more of my code, please don't hesitate.
I always check all streams, if they are enabled. Any enabled stream I disable, the next step to detach all previously attached eventhandlers, at the end I call the Stop() in a try-catch block and logs the Exception Message to get a hint in the case of any problem.
public void StopKinect()
{
if (this.sensor == null)
{
return;
}
if (this.sensor.SkeletonStream.IsEnabled)
{
this.sensor.SkeletonStream.Disable();
}
if (this.sensor.ColorStream.IsEnabled)
{
this.sensor.ColorStream.Disable();
}
if (this.sensor.DepthStream.IsEnabled)
{
this.sensor.DepthStream.Disable();
}
// detach event handlers
this.sensor.SkeletonFrameReady -= this.SensorSkeletonFrameReady;
try
{
this.sensor.Stop()
}
catch (Exception e)
{
Debug.WriteLine("unknown Exception {0}", e.Message)
}
}
hope this helps.