FreshMvvm
FreshMvvm copied to clipboard
"CoreMethods" is null when manually setting Binding context to a FreshMvvm ViewModel
I have a Page that contains a custom view that I created with it's own ViewModel. In order for the ViewModel to be used for this View, it seems that I needed to manually set the binding context in the view.
From my View I am setting the binding context to the ViewModel manually like below:
view.BindingContext = new MyViewModel(FreshIOC.Container.Resolve<IMyApiService>(), FreshIOC.Container.Resolve<App>());
The dependencies are being propagated as expected, but for some reason when I try to call a CoreMethods.PushPageModel<T>() I am recieving a Null reference exception. For some reason "CoreMethods" is 'null' in this ViewModel. Any idea what could be causing this?
I'm not entirely sure since I haven't looked into the FreshMVVM code enough yet but because it prefers convention over configuration it doesn't treat the custom view as a whole nother page and therefore probably doesn't populate the base viewmodel methods correctly. If I were you I'd set up messaging to tell the main page to navigate when the custom view is interacted with. I'm assuming you have a button in the custom view or something that you'd like to initiate the navigation from.
Thank you @WiteCastle . I am still new to Xamarin Forms, so of course I am still discovering what it has to offer. I ended up doing something with Event Handlers.. but this messaging way seems much cleaner.. Will probably switch to that ASAP :).
@jbowmanp1107 (cc: @WiteCastle & @rid00z ) - Did you find a solution to your issue above?
I believe I am attempting to do something very similar, here is my situation:
I have a Xamarin.Forms ContentPage (MyPage) with a corresponding model (MyPageModel). In the MyPage xaml markup I have a custom ContentView (MyView) which I created a corresponding model for (MyViewModel). I am unfortunately having to create the MyViewModel manually in the code behind of MyView.xaml.cs and explicitly assign the BindingContext to a new instance of MyViewModel. In the MyViewModel I want to use FreshMvvm's CoreMethods to push a new page model on the stack, however CoreMethods is null.
Any ideas? Or is this just an odd and/or not recommended approach to this issue?
Thank you in advance!