Xamarin 动态视图模型导航
我正在试图找到一种方法,能够将视图设置为必须导航的ViewModel。这是为了能够在不改变核心项目的情况下改变导航流程 我认为更简单的方法是创建一个界面,在那里设置目标ViewModel,然后将该界面注入ViewModel,然后执行导航Xamarin 动态视图模型导航,xamarin,mvvmcross,Xamarin,Mvvmcross,我正在试图找到一种方法,能够将视图设置为必须导航的ViewModel。这是为了能够在不改变核心项目的情况下改变导航流程 我认为更简单的方法是创建一个界面,在那里设置目标ViewModel,然后将该界面注入ViewModel,然后执行导航 public interface IModelMapping { MvxViewModel ViewModelToNavigate(); } public class MyViewModel : MvxViewModel { readonly
public interface IModelMapping
{
MvxViewModel ViewModelToNavigate();
}
public class MyViewModel : MvxViewModel
{
readonly IMvxNavigationService navigationService;
readonly IModelMapping modelMapping;
public MyViewModel(IMvxNavigationService navigationService, IModelMapping modelMapping)
{
this.navigationService = navigationService;
this.modelMapping = modelMapping;
}
public IMvxAsyncCommand GoContent
{
get
{
IMvxViewModel vm = modelMapping.ViewModelToNavigate();
IMvxAsyncCommand navigateCommand = new MvxAsyncCommand(() => navigationService.Navigate<vm>());
return navigteCommand;
}
}
}
公共接口IModelMapping
{
MvxViewModel ViewModelToNavigate();
}
公共类MyViewModel:MvxViewModel
{
只读IMvxNavigationService导航服务;
只读IModelMapping模型映射;
公共MyViewModel(IMvxNavigationService navigationService,IModelMapping modelMapping)
{
this.navigationService=导航服务;
this.modelMapping=modelMapping;
}
公共IMvxAsyncCommand GoContent
{
收到
{
IMvxViewModel vm=modelMapping.ViewModelToNavigate();
IMvxAsyncCommand navigateCommand=新的MvxAsyncCommand(()=>navigationService.Navigate());
返回navigteCommand;
}
}
}
此代码的问题是设置navigationService.Navigate()时出错。错误是“vm是一个变量,但它的使用方式与类型类似”将URI导航与facade一起使用如何?另见 假设您正在构建任务应用程序,并根据您希望显示不同视图的任务类型而定。这就是
NavigationFacades
派上用场的地方(正则表达式只能为您提供这么多)
mvx://task/?id=00 将URI导航与facade一起使用怎么样?另请参见@Martijn00,我使用URI导航对其进行排序,在程序集上设置ViewModel id,并通过接口将该id从视图发送到ViewModel。
[assembly: MvxRouting(typeof(SimpleNavigationFacade), @"mvx://task/\?id=(?<id>[A-Z0-9]{32})$")]
namespace *.NavigationFacades
{
public class SimpleNavigationFacade
: IMvxNavigationFacade
{
public Task<MvxViewModelRequest> BuildViewModelRequest(string url,
IDictionary<string, string> currentParameters, MvxRequestedBy requestedBy)
{
// you can load data from a database etc.
// try not to do a lot of work here, as the user is waiting for the UI to do something ;)
var viewModelType = currentParameters["id"] == Guid.Empty.ToString("N") ? typeof(ViewModelA) : typeof(ViewModelB);
return Task.FromResult(new MvxViewModelRequest(viewModelType, new MvxBundle(), null, requestedBy));
}
}
}