App crashes when a TabbedPage has a BindingContext set and at least one child page inherits from ViewBase<T>
An app that makes use of TinyMvvm crashes under the following circumstances:
- There is a TabbePage which has its BindingContext set to, say, an instance of a class called TabbedViewModel;
- At least one of the children pages of the TabbedPage inherits from
ViewBase<T>, where T is the type of the ViewModel the child page is supposed to use, say a class called ChildViewModel
In this conditions, TinyMvvm is supposed to create the ViewModel for the child page during its OnAppearing method, and the first lines of code in that method are:
if(ViewModel == null && !CreatedByTinyMvvm)
{
CreateViewModel();
}
Property ViewModel, on its side, contains these lines of code:
if (BindingContext != null)
{
return (T)BindingContext;
}
return default(T);
This code assumes that if a BindingContext is already set, it is of type T, which is wrong in the case of a child page of a TabbedPage because, until it set its own ViewModel, it inherits the ViewModel of the TabbedPage itself so, in our case, BindingContext is here of type TabbedViewModel and not ChildViewModel, which makes the app crash.
I think the line
return (T) BindingContext;
should be changed in
return T As BindingContext;
In this way, if the BindingContext is inherited from a parent and thus not of type T, null is returned and a BindingContext of the correct type is then created in the lines that follow in the OnAppearing method.
Are you using IoC?
Yes I'm using Autofac, but I think it would be the same, the crash occurs before creating the viewmodel of the child page, when evaluating if the BindingContext has already been set. The problem is, the BC is inherited from the parent TabbedPage before creating it, so it is not of the expected type.
Can you show me the code for your TabbedPage?
It is just the TabbedPage created by Xamarin Forms template in VS 2017, slightly accommodated to test some TinyMvvm features. Here is the xaml:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:TinyMvvmApp.Views"
x:Class="TinyMvvmApp.Views.MainPage"
NavigationPage.HasNavigationBar="False"
>
<TabbedPage.Children>
<NavigationPage Title="Browse">
<NavigationPage.Icon>
<OnPlatform x:TypeArguments="FileImageSource">
<On Platform="iOS" Value="tab_feed.png"/>
</OnPlatform>
</NavigationPage.Icon>
<x:Arguments>
<views:ItemsPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="About">
<NavigationPage.Icon>
<OnPlatform x:TypeArguments="FileImageSource">
<On Platform="iOS" Value="tab_about.png"/>
</OnPlatform>
</NavigationPage.Icon>
<x:Arguments>
<views:AboutPage />
</x:Arguments>
</NavigationPage>
</TabbedPage.Children>
<TabbedPage.ToolbarItems>
<ToolbarItem Text="{Binding NavigationButtonText}" Command="{Binding NavigateTo}" CommandParameter="ItemDetailPage"></ToolbarItem>
</TabbedPage.ToolbarItems>
</TabbedPage>
... and here the code behind:
namespace TinyMvvmApp.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : TabbedPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
if (BindingContext == null)
{
BindingContext = new MainViewModel();
}
base.OnAppearing();
}
}
}
It's not so easy to just change to BindingContext as T. The reason is that the compiler does not allow it. But I looking for a solution.
I you have a solution, please submit a pull request :)
Probably is the best solution for you to use the non-generic ViewBase in this case.
Hello Daniel, you are right. The compiler does not allow to do that, because T has no "class" clause. To make it works, you should change the signature of ViewBase<T> class into:
public abstract class ViewBase<T> : ViewBase where T : class, INotifyPropertyChanged
Of course this would make it impossible to use a ViewModel that is not a class (that is, a struct).
I think it'is highly probably that users of TinyMvvm use ViewModelBase as their BindingContext or at least a class, so there should be no great problem in adding this clause.
But of course that could be a very breaking change if someone for some reason had used a struct as a ViewModel.
At least, it would be a compile-time break, not a runtime one, because one should no more be able to declare a Page as inheriting from ViewBase<T> when T is not a class.
Of course the decision is up to you... :)
As a workaround, I've come to manually create the ViewModel in the OnAppearing override of the child Page:
var vm = Resolver.Resolve<ItemsViewModel>();
BindingContext = vm;
base.OnAppearing();
This way, when the OnAppearing of ViewBase
Fixed in https://github.com/TinyStuff/TinyMvvm/commit/e785a9d77389edc087a88403010d8a03b4568253