0

I have been using windows-app-sdk and winui3 for developing my application which supports multiple windows. As an architecture, I am using ServiceLocator and DI to get the singleton and scoped services for my multiple windows. The logic of ServiceLocator.Current is as follows,

static public ServiceLocator Current
        {
            get
            {
                int currentViewId = WindowHelper.CurrentWindowId;  // it will get the window id of the current activated window
                return _serviceLocators.GetOrAdd(currentViewId, key => new ServiceLocator());
            }
        }

In a specific case, I have a window say 'A', which has a button 'Save & Print'. When the user clicks this button, then I will save the data and open a print preview in a new window say 'B' and it will be activated as current window. When window 'B' becomes activated window, I still need to perform some post save action in my window 'A'. During those post save actions, when fetching the scoped service from the service locator, it returns the scoped service of window 'B' as it is the current window not the service of window 'A'.

Hence I need a solution to fetch the scoped service from service locator, which should return the service not based on the activated window, but based on the requested Thread (it can be any window whether currently activated or background).

NAVEEN V
  • 42
  • 6
  • Can you post a simple flow of your processes? For example, 1) WindowA-[Save&Print] clicked, 2) Get a service for data saving and save data 3) After the data is saved open close WindowA and open WindowB ... – Andrew KeepCoding Oct 10 '22 at 09:59
  • According to the Doc:[ServiceLocator Class](https://learn.microsoft.com/en-us/previous-versions/msp-n-p/hh860317(v=pandp.51)) It seems that the ServiceLocator Class is outdated and is no longer being maintained. – Jeaninez - MSFT Oct 11 '22 at 02:48
  • @Jeaninez-MSFT We are using our own defined `ServiceLocator` and not from the library you have mentioned. In such custom definition we used the logic of current activated window which won't suite if the request is for background window. – NAVEEN V Oct 11 '22 at 04:15

0 Answers0