Reputation: 557
I am trying to use FreshPageModelResolver.ResolvePageModel<>();
but I get the following exception:
System.Exception: 'XFShell.Pages.MainPage, XFShell, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null not found'.
For what I have seen in other pages the usual problem is that the ViewModel page and the Page do not follow the correct nomenclature, but I have checked mine and it seems to be correct, if you could tell me how to fix it I would appreciate it.
App.XAML.cs code:
public App()
{
InitializeComponent();
var getPage = FreshPageModelResolver.ResolvePageModel<MainViewModel>();
MainPage = new FreshNavigationContainer(getPage);
}
MainViewModel.cs code:
public class MainViewModel : FreshBasePageModel
{
public ICommand comando { get; set; }
public override void Init(object initData)
{
comando = new Command(async () =>
{
TheEvent();
});
}
public MainViewModel()
{ }
private void TheEvent()
{ }
MainPage.XAML.cs code:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
MainViewModel.cs is located in a folder called "ViewModels", and MainPage.xaml.cs and MainPage.xaml are in a folder called "Pages".
That is all, if you need more information I will provide it as soon as I see your request, thank you all for your time, hope you have a good day
Upvotes: 0
Views: 670
Reputation: 10958
Since FreshMvvn Release 2.1.0
, it adds ability to control PageModel mapping via PageModelMapper. We have the ability to control the convention of mapping ViewModel to pages now. It supports use Page and ViewModel like Pages and PageModel. You do this by using the IFreshPageModelMapper like below.
public class MyPageModelMapper : IFreshPageModelMapper
{
public string GetPageTypeName(Type pageModelType)
{
var mainpagemodel = typeof(MainPageModel);
var s = Type.GetType(mainpagemodel.AssemblyQualifiedName);
var mainviewmodel = typeof(MainViewModel);
var s2 = Type.GetType(mainviewmodel.AssemblyQualifiedName);
return pageModelType.AssemblyQualifiedName
.Replace("PageModel", "Page")
.Replace("ViewModel", "Page");
}
}
I reproduce the error, it normally caused by the type od MainViewModel return null. It means after replace ViewModel to Page, the name
(AssemblyQualifiedName of MainPage) could not get the right type of MainPage. So it thrown the not found exception.
public static Page ResolvePageModel(Type type, object data, FreshBasePageModel pageModel)
{
var name = PageModelMapper.GetPageTypeName(s);
var pageType = Type.GetType(name);
if (pageType == null)
throw new Exception(name + " not found");
var page = (Page)FreshIOC.Container.Resolve(pageType);
BindingPageModel(data, page, pageModel);
return page;
}
You could create a MainPageModel in PageModel folder to check it use the code below in MyPageModelMapper.
var mainpagemodel = typeof(MainPageModel);
var s = Type.GetType(mainpagemodel.AssemblyQualifiedName);
var mainviewmodel = typeof(MainViewModel);
var s2 = Type.GetType(mainviewmodel.AssemblyQualifiedName);
And then implement your own custom mapper and then set the PageModelMapper.
// To set MainPage for the Application
FreshPageModelResolver.PageModelMapper = new MyPageModelMapper();
var page = FreshPageModelResolver.ResolvePageModel<MainViewModel>();
var basicNavContainer = new FreshNavigationContainer(page);
MainPage = basicNavContainer;
My result:
I have upload the sample code on GitHub FreshMvvm_ViewModelDemo
folder, you could download for reference.
https://github.com/WendyZang/Test.git
Upvotes: 1