Hello,
In a windows native application I was able to use DataTemplates and MVVM to switch between content pages. I have been scouring for a similar solution for Xamarin.Forms to have the NavPage of the Master Detail behave similarly but have been unsuccessful in figuring this out. Any assistance is greatly appreciated.
From my WPF native windows application:
View:
<Window ...> <Window.Resources> <ResourceDictionary> <DataTemplate DataType="{x:type viewmodel:MonthlyViewModel}"> <view:MonthlyControl/> </DataTemplate> <DataTemplate DataType="{x:type viewmodel:WeeklyViewModel}"> <view:WeeklyControl/> </DataTemplate> </ResourceDictionary> </Window.Resources> <StackPanel> <Border> <ContentControl Content="{Binding SelectedSchedule}" /> </Border> ... </StackPanel> ... </Window>
ViewModel:
public class MyWindowViewModel { IScheduleStrategy currentSchedule; public MyWindowViewModel() { CurrentSchedule = new MonthlyViewModel(); } public ListItemViewModel<IScheduleStrategy> CurrentSchedule { get { return currentSchedule; } set { SetProperty(ref currentSchedule, value); } } }
Answers
@HuntJason,
You may want to take a look at
ControlTemplates
. Jason posted about this on his blog, here.There is also another great post here.
Another technique I have used is to make my own custom view that can swap it's content out based on a DataTemplate, using a DataTemplateSelector. Something like this:
Then, you can use it like this:
util:CustomTemplateSelector
is an instance of DataTemplateSelector, exampled here.After reading more about the Hamburger Menu pattern and why to avoid it, I changed from wanting to use the MasterDetailPage and switched to the TabbedPage as my main form of navigation within my application. I was also able to accomplish the desired MVVM pattern by modifying the TabbedPage thanks to help from the BindablePicker example here in the forums.
In my app I use Messaging Center to request a change of the detail. It's the easier clean way I found.
One problem with using the Messaging Center is that, because it's a singleton, it's not as easily testable.
Messaging Center does have it's downsides, especially if you forget to Unsubscribe
. It's still a great feature with a lot of flexibility that I'm not sure many people are aware of it's existence.
It is great, but I suspect that utilizing it as a mechanism to change/add details is somewhat of a golden hammer.
I was able to accomplish the basics of getting the MasterDetailPage to follow the MVVM pattern. I have outlined the change here.
@JohnMiller : could you please post a link to the complete solution for swapping the content of the custom view, using a DataTemplateSelector ?