How can I handle OnSuspending with WinUI 3 - c#

This time I must migrate an UWP App to WinUI 3, so far everything has worked well except for a few small things but the event OnSuspending is simply not triggered. In the namespace Windows.UI.Xaml you could easily use
this.Suspending += OnSuspending
in app.xaml.cs and it wasn't a problem, in Microsoft.UI.Xaml I can't do this. I have now declared a SuspendingEventhandler in app.xaml.cs
public event SuspendingEventHandler Suspending;
but this just doesn't work. How can I solve this problem?

WinUI 3 can be used to build desktop/WIn32 Windows apps (see the docs) and this kind of apps are not suspended so there is no Suspending event ever being raised.

Related

ListViewItem Drag&Drop Operation Stuck

During the drag&drop operation of listviewitems in an normal UWP ListView sometimes the windows sharing crashes
The item down is stuck there until I decide to close the app (with the "is being used for sharing popup") - In fact it seems like the Windows Explorer does not allow any drag&drop operations until the app closes!
From my debugging I can see that the DragEnter gets fired but there is no DragItemsCompleted
How to replicate this issue
Create a new UWP app
TargetAPI: 1803
MinAPI: 1803
Create a ListView
<ListView CanDragItems="True">
<ListView.Items>
<ListViewItem>asdf</ListViewItem>
<ListViewItem>asdf</ListViewItem>
<ListViewItem>asdf</ListViewItem>
<ListViewItem>asdfEx</ListViewItem>
</ListView.Items>
</ListView>
Quickly drag the item and release it on top of the listview
3.1 Sometimes it gets stuck immediately, sometimes it takes more attempts
Possible error (without seeing more code..) :you dont capture mouse, so your application doesnt detect your up event
see mouse.capture
maybe for UWP see UIElement.CapturePointer
This seems to be a specific windows 1803 bug - pressing escape seems to unblock the UI.
Nothing I have tried in the code behind seems to be working...
I may have found an answer (at least it is working for me for now). I was previously triggering the DragStarting using the following code:
Private Async Sub brdPlayer_PointerPressed(sender As Object, e As PointerRoutedEventArgs)
Await CType(sender, Border).StartDragAsync(e.GetCurrentPoint(CType(sender, Border)))
End Sub
However, this was causing DragStarting to be triggered twice (and possibly other stuff), so when I modified the code by adding:
e.Handled = True
I am still testing the other scenarios to make sure this really fixes the whole thing, but hopefully it helps you as well!

WPF browser loadCompleted/Navigated not firing on SOME machines

I have a pretty simple wpf application. Works great on my machine. But when I install it on another machine, the browser's loadCompleted event does not fire. Based on other similar questions & answers, I have tried:
- use navigated event instead of loadCompleted - does not fire either
- use windows form browser instead of wpf browser - does not fire
- ensure app is fully loaded before navigating: tried putting the navigate call in the window's Loaded event, even threw in a 5 second sleep for good luck - does not fire
Like I said, works like a charm on my machine, both from within Visual Studio and when I execute it from the command line.
So I'm thinking it's something about the other machine. Any ideas what environmental factors would prevent the loadComplete/Navigated events from firing?
Thanks!
[edited: just tried it on another co-worker's machine, and the loadCOmpleted event does not fire there either. So it seems to just fire the event on my machine/the machine it is built on. Not sure if that gives anyone a clue to this frustrating little mystery...)
OK, well this is mildly embarrassing. The events were indeed firing, but my messagebox messages were not showing up. Before it could get to that step, the app encountered a fatal error. There were two problems that worked together to cause me days of untold angst:
1) I had deployed the debug version of my app to the two remote machines. Neither of those machines were developer machines, and they did not have the debug version of the redistributable Visual Studio gunk. So it was failing trying to load one of my dlls, because of the dependency on the debug libraries.
2) Poor error handling failed to catch this error, so it just looked like the event was not firing.
Lesson learned: make sure you have proper try/catches !!

In which namespace Application.OnLaunched is located?

I am trying to add HockeyApp to my WPF app, but cannot find Application.OnLaunched ?
Any thought appreciated thanks!
https://support.hockeyapp.net/kb/client-integration-windows-and-windows-phone/hockeyapp-for-applications-on-windows#crash-reporting
For platforms other than UWP, you need to add the following line of
code at the end of the OnLaunched(LaunchActivatedEventArgs e) Method
(in App.xaml.cs)
It seems, that KB page is incorrect. There's no OnLaunched method in WPF Application class. The nearest equivalent will be OnStartup.
Note, that Silverlight Application differs from WPF one - you need to handle Startup event, because there's no virtual OnStartup method there.
it is located in the system.windows.application

Global hook mousedown and keypress events are not firing

I've used the exact same code in my c# desktop application as it is given in here : https://github.com/gmamaladze/globalmousekeyhook
It works and fires the mousedown and keypress events as long as the application is in focus. If i'm on another application, let'say on visual studio (while mousekey logger is running), it doesn't fire the events.
Is it correct behavior? If so, any help to achieve this would be appreciated.
As suggested by #Hans Passant, application must be run in elevated mode if we have to track activity in other elevated applications. Running the ClickOnce application in Elevated mode was not so easy, i used this trick to achieve it. http://antscode.blogspot.com.au/2011/02/running-clickonce-application-as.html

What is the correct event to watch for if you want to know when a Metro app is closing?

I know that in Desktop apps we have things like Form_Closing which is great for doing stuff before the app shuts down, but I need to do things before a Metro app shuts down (is closed by the user or the system) and I can't get this working.
The only events that seem to be even remotely related are the App_Suspending and pageRoot_Unloaded events.
My problem is that my code (any code at all) never gets called if I put it in the Unloaded event, which makes me believe that the Unloaded event never gets fired.
I haven't tried using the Suspending event yet because I need to do this work when the app is actually closing, or about to be closed, not just suspended.
Any ideas?
There is no diff between Closing and Suspending Metro App on the moment of suspending, so you must save state in OnSuspending override every time. But after re-Activating you can see what was happened with your App (OnLaunched (for example, here you can see value ClosedByUser)).
Here is not bad article about WinRT LifeCycle.
you can override OnNavigatedFrom method which will be invokved when page is being navigated away from.
http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.page.onnavigatedfrom.aspx
I dont think there is any explicit calls for app being terminated (accept checking the previous state in app.xaml.cs)

Categories

Resources