0

To start, I found this post and used it as a reference. I'm attempting to subscribe to events from the View Model of my application. Doing this would allow me to easily execute code from my Model classes and perform various activities when backgrounding and foregrounding an application. This is something I utilize a lot in my existing Xamarin applications.

I started with the following code inside of my App.xaml.cs file:

    protected override Window CreateWindow(IActivationState activationState)
    {
        Window window = base.CreateWindow(activationState);
        window.Activated += (sender, eventArgs) => {
            Models.Debug.Log(LogType.Info, "Activated");
        };
        window.Deactivated += (sender, eventArgs) => {
            Models.Debug.Log(LogType.Info, "Deactivated");
        };
        return window;
    }

This worked as intended and logged everything to the Output window. (Debug.Log is my own little utility I built for logging)

I then updated the code in the App file to the following, similar to the previously mentioned Stack Overflow post as well as another MAUI Blog post.

    public static Window Window { get; private set; }
    protected override Window CreateWindow(IActivationState activationState)
    {
        Window window = base.CreateWindow(activationState);
        Window = window;
        return window;
    }

Then I went to the view model for my apps main page and added this:

var window = App.Window;

window.Activated += (s, e) =>
{
   Debug.Log(LogType.Info, "Activated");
};

window.Deactivated += (s, e) =>
{
   Debug.Log(LogType.Info, "Deactivated");
};

I end up getting an object reference error.

Object Reference Error

The confusion here is that the CreateWindow method is clearly getting called, since I can see the logs when I use the first version of the code I posted above. So why would window not be set to an instance when the app has already advanced to loading the first content page?

Any advice on what to try next is appreciated. If there's a better way to go about managing lifecycles in MAUI then please let me know.

Timothy
  • 135
  • 1
  • 9
  • The problem is that main page is needed during App constructor. This is "too early" - App and Window aren't hooked up the way you need yet, when main page ands its VM are constructed. You can prove this by doing nothing if null, then after main page is displayed, do something else that needs your override. Viola, now its not null. Bottom line: you'll have to rewrite code so that when get null, whatever you are trying to do is done later, after the main page is displayed. Could [start app on a "landing page"](https://stackoverflow.com/a/76799602/199364). Let that display first. – ToolmakerSteve Aug 01 '23 at 01:11
  • Adding a landing page didn't work but moving my subscription code to the Pages "OnAppearing" method did. – Timothy Aug 01 '23 at 16:54

1 Answers1

1

I was able to resolve this by moving the subscription code into the OnAppearing Page method. This allowed the page to initialize correctly before trying to add the listeners.

I did try adding a landing page first and then passing the app over to my code with the constructor. That still failed with a null reference exception.

The landing page is still there in my final implementation so that may be the most important aspect of having this work. I'm just calling the subscriber code from OnAppearing instead of the page constructor.

Timothy
  • 135
  • 1
  • 9
  • Good to hear that code works in `OnAppearing`; I wasn't sure it would. I am interested in the failed attempt. Did the landing page actually display before your code ran with the null exception? Did you do it the way I linked, so there was a one second delay before that code? – ToolmakerSteve Aug 01 '23 at 19:57
  • I did not put a delay in there as I had some functional code process some things before transitioning to the main page. I may give that another try at some point just to test it but it seems to work well in OnAppearing on the second page (without any delays forced in). Note that I do still have the landing page in the design so that might be a significant part of the solution. I'll update my answer to reflect that. – Timothy Aug 01 '23 at 21:53