我正在开发一个使用Xamarin Classis UI和MvvmCross 5.6.x的移动应用程序,我们希望利用网址基础导航添加与应用程序的深层链接。我们希望遵循与客户网站相同的网址方案,因此可以共享网址以导航到两者中的特定内容。例如,以下两个网址应按照描述进行导航:
http://www.rseg.net/rewards应该导航到忠诚度奖励列表 http://www.rseg.net/rewards/1234应显示有关特定奖励的详细信息
我已将以下两个视图模型设置为网址的目标:
[assembly: MvxNavigation(typeof(RewardsViewModel),
@"http://www.rseg.net/rewards")]
namespace MvvmNav.Core.ViewModels
{
public class RewardsViewModel : MvxViewModel
{
…
}
}
和
[assembly: MvxNavigation(typeof(RewardDetailViewModel),
@"http://www.rseg.net/rewards/(?<id>[0-9]{4})$")]
namespace MvvmNav.Core.ViewModels
{
public class RewardDetailViewModel :
MvxViewModel<RewardDetailViewModel.Parameteres>
{
…
}
}
我已将以下两个命令添加为初始视图模型。
public IMvxAsyncCommand ShowRewardsByUrl { get; protected set; }
protected async Task ShowRewardsByUrlHandler()
{
var url = "http://www.rseg.net/rewards";
if (await _navigationService.CanNavigate(url))
await _navigationService.Navigate(url);
}
public IMvxAsyncCommand ShowRewardDetailByUrl { get; protected set; }
protected async Task ShowRewardDetailByUrlHandler()
{
var url = "http://www.rseg.net/rewards/1234";
if (await _navigationService.CanNavigate(url))
await _navigationService.Navigate(url);
}
我看到的行为是两个命令最终导航到RewardsViewModel而不是导航到RewwardDetailViewModel的第二个命令;有没有办法指定导航网址,以便我可以实现上述行为?
整个示例可以从GitHub https://github.com/rsegtx/So.MvvmNav下载;我只完成了Android应用的观看次数。
先谢谢。
答案 0 :(得分:0)
我认为你需要一个Navigation facade才能工作,并将两个ViewModels的路由也改为@"http://www.rseg.net/rewards(/(?<id>[0-9]{4}))?$"
:
[assembly: MvxRouting(typeof(SimpleNavigationFacade), @"http://www.rseg.net/rewards(/(?<id>[0-9]{4}))?$")]
namespace *.NavigationFacades
{
public class SimpleNavigationFacade
: IMvxNavigationFacade
{
public Task<MvxViewModelRequest> BuildViewModelRequest(string url,
IDictionary<string, string> currentParameters, MvxRequestedBy requestedBy)
{
var viewModelType = typeof(RewardsViewModel);
if(currentParameters.TryGetValue("id", out string paramId) && !string.IsNullOrEmpty(paramId))
viewModelType = typeof(RewardDetailViewModel);
return Task.FromResult(new MvxViewModelRequest(viewModelType, string.IsNullOrEmpty(paramId) ? new MvxBundle() : new MvxBundle(currentParameters), null));
}
}
}
HIH