4

.Net Maui

I am passing an object to a page/viewmodel but it is null in the constructor. I need to derive some data from it to pass back to the XAML page but I don't know when or how it gets set in the viewmodel.

I believe the [QueryProperty] used here is doing some magic behind the scenes using MVVM Community Toolkit

[QueryProperty("OpenJob", "OpenJob")]
public partial class NoteAvailabilityViewModel : BaseViewModel {
    
    [ObservableProperty]
    OpenJob openJob;

    public List<String> jobDates;

    public NoteAvailabilityViewModel(JobsService jobsService) {
        if (openJob == null) {
            Debug.Write("its null");

           //It's always null here
           //When, how is it set so I can access it in the viewmodel?

        }
        else {
            Debug.Write("its not null");

        }
    }
}

In the page that navigates to this one i have this ICommand that is triggered on button click

[ICommand]
public static void NoteAvailabilityAsync(OpenJob openJob) {
    Shell.Current.GoToAsync($"{nameof(NoteAvailabilityPage)}", true, new Dictionary<string, object> {
            {"OpenJob", openJob }
    });
}

This rout is registered in the APP Shell

The Page has this code I use from a tutorial (still a noob)

public partial class NoteAvailabilityPage : ContentPage {
    public NoteAvailabilityPage(ViewModel.NoteAvailabilityViewModel viewModel) {
        InitializeComponent();
        BindingContext = viewModel;
    }

    protected override void OnNavigatedTo(NavigatedToEventArgs args) {
        base.OnNavigatedTo(args);
    }
}
10
  • how are you passing the data on Navigation? Commented Jun 25, 2022 at 20:23
  • Add to question the line of code that navigates to that page. Do you have OpenJob as a query parameter in that line? Commented Jun 25, 2022 at 20:24
  • See also Process navigation data using a single method. That is an alternative way to access query properties. It might be easier to debug - you can examine the dictionary, see what it contains. (But the way you show should work too.) Commented Jun 25, 2022 at 20:29
  • 1
    You're probably finding it to be null because you're checking openJob value in the constructor and it's not been initialized yet. Commented Jun 27, 2022 at 7:53
  • 1
    You can make a use of "Loaded" event from "ContentPage". You can trigger a method from there and you will get your not null query value. Commented Sep 14, 2022 at 11:44

1 Answer 1

9

The value of the property is indeed always null in the constructor, because the object has to be constructed before the property can actually be set. (I have just learnt how to do this myself!)

You need to respond to the changed value in the OnOpenJobChanged function that ObservableProperty generates for you (after you type "partial " VS should suggest the completion for you).

[QueryProperty("OpenJob", "OpenJob")]
public partial class NoteAvailabilityViewModel : BaseViewModel {
    
    [ObservableProperty]
    OpenJob openJob;

    public List<String> jobDates;

    public NoteAvailabilityViewModel(JobsService jobsService) {
        // don't forget to save your jobsService here too :)
    }

    partial void OnOpenJobChanged(OpenJob value) {
        // process the query parameter value here
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.