is there a way to order methods that run before the XAML starts? (MvvM)

81 Views Asked by At

In an Mvvm application, I have a page that has two methods that run in the ViewModel before the Xaml runs. They run in the wrong order, and I'm wondering if there's a way to tell it which method to run first.

The method that runs second is the implementation of the IConvertible interface, used to pass an object from a previous page. I would like it to run first.

The method that runs first is Appearing(), which is the MVVM answer to the OnAppearing method. I would like it to run second.

    // this method runs first, I would like it to run second
    [RelayCommand]
    private async Task Appearing()
    {
        // no access to VideoObj because this method runs before ApplyQueryAttributes
        string x = VideoObj.ThumbnailFilename;
    }

    // this method runs second, I would like it to run first
    public void ApplyQueryAttributes(IDictionary<string, object> query)
    {
        Package = query["Package"] as VideoPackage;
        VideoObj = Package.video;
    }

Is there a way in C# or Maui to order this kind of thing?

1

There are 1 best solutions below

1
On

Why don't you trigger a method after both have been fired, so it is order agnostic? e.g.

    bool Appeared { get; set; } = false;

    [RelayCommand]
    private async Task Appearing()
    {
        Appeared = true;
        if (Appeared && VideoObj !== null) HandleThumbnail();
    }

    public void ApplyQueryAttributes(IDictionary<string, object> query)
    {
        Package = query["Package"] as VideoPackage;
        VideoObj = Package.video;
        if (Appeared && VideoObj !== null) HandleThumbnail();
    }

    private void HandleThumbnail()
    {
        string x = VideoObj.ThumbnailFilename;
    }