I'm trying to use Autofac in Xamarin.Forms project. I created basic samples successfully, even used ViewFactory for some more complicated samples.
However, I'm unable to use MasterDetailPage
along with Navigation.
I'm using factories and services written by Jonathan Yates. You can find his code here
My Application Bootstrapper:
protected override void ConfigureApplication(IContainer container)
{
var viewFactory = container.Resolve<IViewFactory>();
var mainPage = viewFactory.Resolve<TestViewModel1>();
var navigationPage = new NavigationPage(mainPage);
var masterPage = new ContentPage();
masterPage.Title = "asd";
_application.MainPage = new MasterDetailPage()
{
Master = masterPage,
Detail = navigationPage
};
}
My TestViewModel1
has a command that navigates to TestViewModel2
:
ButtonCommand = new Command(async()=>await _navigator.PushAsync<TestViewModel2>());
However, the app crashes with Exception saying
Cannot cast from source type to destination type.
This happens when the Navigator
is pushing the next page.
StackTrace:
" at TestIoc.TestModule+<>c.b__0_0 () [0x00000] in C:\Users\dushyantb\Documents\Visual Studio 2015\Projects\TestIoc\TestIoc\TestIoc\TestModule.cs:27 \n at TestIoc.Views.PageProxy.get_Navigation () [0x00001] in C:\Users\dushyantb\Documents\Visual Studio 2015\Projects\TestIoc\TestIoc\TestIoc\Views\PageProxy.cs:39 \n at TestIoc.Navigator.get_Navigation () [0x00001] in C:\Users\dushyantb\Documents\Visual Studio 2015\Projects\TestIoc\TestIoc\TestIoc\Services\Navigator.cs:26 \n at TestIoc.Navigator+d__8`1[TestIoc.ViewModels.TestViewModel2].MoveNext () [0x0003a] in C:\Users\dushyantb\Documents\Visual Studio 2015\Projects\TestIoc\TestIoc\TestIoc\Services\Navigator.cs:58 "
what am I doing wrong?
Note: The navigation works when there's no MasterDetailPage.
Be sure not to override the PageResolver, eg. don't take the override Jonathan uses in his WeatherApp sample:
The default implementation of the PageResolver Func in the core component takes care of MasterDetail scenarios. See AutoFacModule.cs:
I have tested MasterDetail scenario with NavigationPage DetailViews succesfully with Jonathan's framework. So it should work.
In my case, the bootstrapping code is
That means, i have a MainPageView, which is a MasterDetailPage and which takes care of setting up it's child views:
The MenuPageViewModel also uses IViewFactory to switch the MasterPages' Detail:
Hope this helps.