How to get the current view of my UWP app from background thread?

2

In my UWP app, when a post call succeeds, I need to show a notification to the user. This notification has to be shown in the current window in which the user is. I will get to know the success of the post call in the background thread only. So once I receive the success code from server, I need to identify which is the current window and I have to display a notification to the user in that window.

Window.Current gives me null when accessed from background thread.

If I try to access Window.Current within the CoreApplication.MainView.Dispatcher.RunAsync it gives me the main window.

If I try to access CoreApplication.GetCurrentView().CoreWindow.Dispatcherfrom the background thread CoreApplication.GetCurrentView() throws the below exception

Exception = {System.Runtime.InteropServices.COMException (0x80070490): Element not found.

    Element not found.

    at Windows.ApplicationModel.Core.CoreApplication.GetCurrentView()

I did a small round of analysis to find out what is happening with the below code snippet.

        foreach (var iteratingview in CoreApplication.Views)
        {
            iteratingview.Dispatcher.RunOnUIThread(() =>
            {
                int id = ApplicationView.GetForCurrentView().Id;
                bool main = CoreApplication.GetCurrentView().IsMain;
                bool isMain = iteratingview.IsMain;
            });
        }

The boolean value CoreApplication.GetCurrentView().IsMain gives me true when the iteratingview is main view and false when the iteratingview is a secondary view, which means CoreApplication.GetCurrentView() returns me the view inside which dispatcher I'm calling it. ApplicationView.GetForCurrentView() also behaves in the same way.

But my requirement is to identify which is the current window/view where the user is.

Am I doing anything wrong, or is there any other way to get the current window's dispatcher from the background thread?

uwp
windows-10-universal
asked on Stack Overflow Oct 4, 2018 by Sethu Bala

2 Answers

1

I believe that there are no straightforward way to know that which window(view) is the focused one, from the outside - background thread. But, each window(view) can know it by Window.Current.CoreWindow.Activated event. In my case, I had implemented the dirty code to set/clear the flag that shows "I'm activated / deactivated!" within the window(view), and checked it from the outside. This way worked well.

answered on Stack Overflow Oct 5, 2018 by Mamoru Satoh
1

The best solution is to decouple the two and use an event from your background thread. This way, any foreground window(s) that want to display information can subscribe to the event, and the background thread doesn't need to know anything about which one is active etc. and also won't have to worry about which thread to dispatch the call into (the window(s) that subscribe to the event can trivially do the dispatching themselves with their own Dispatcher.

answered on Stack Overflow Oct 22, 2018 by Peter Torr - MSFT

User contributions licensed under CC BY-SA 3.0