我想在所选页面中包含一个后退按钮。由于某种原因,我将其称为“面包屑”,请假定其名为“ BackButton” :)。
问题是导航服务无法在ioc中获得通过。
以下是我遇到的以下代码:
XAML主页-注意local:BreadcrumbControl
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="Test.Views.MainPage"
xmlns:local="clr-namespace:Test.Views">
<StackLayout>
<local:BreadcrumbControl x:Name="Breadcrumb" />
</Stacklayout>
<ContentPage>
面包屑控制XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="Test.Views.BreadcrumbControl">
<Button Text="Back" Command="{Binding NavigateCommand}" CommandParameter="NavigationPage/ManageFoodGroupsPage" />
</ContentView>
面包屑页面视图模型
public class BreadcrumbControlViewModel : BindableBase
{
INavigationService navigationService;
public DelegateCommand<string> NavigateCommand { get; set; }
// If i uncomment the navigationservice the following error occurs (*)
public BreadcrumbControlViewModel(/*INavigationService navigationService*/)
{
this.navigationService = navigationService;
NavigateCommand = new DelegateCommand<string>(Navigate);
}
private void Navigate(string name)
{
navigationService.GoBackAsync();
}
}
(*)
Microsoft.Practices.Unity.dll中发生类型为“ Microsoft.Practices.Unity.ResolutionFailedException”的异常,但未在用户代码中处理
附加信息:依赖关系的解析失败,类型=“ Test.ViewModels.BreadcrumbControlViewModel”,名称=“(none)”。
在以下期间发生异常:解析构造函数Test.ViewModels.BreadcrumbControlViewModel(Prism.Navigation.INavigationService navigationService)的参数“ navigationService”。
异常是:NullReferenceException-对象引用未设置为对象的实例。
在发生异常时,容器为:
解决Test.ViewModels.BreadcrumbControlViewModel,(无)
解析构造函数Test.ViewModels.BreadcrumbControlViewModel(Prism.Navigation.INavigationService navigationService)的参数“ navigationService”
App.cs容器/解析器注册
ViewModelLocationProvider.Register<BreadcrumbControl, BreadcrumbControlViewModel>();
Container.RegisterTypeForNavigation<MainPage>();
如何在Xamarin Forms Prism MVVM应用程序中包含常见的后退按钮样式的东西(由视图模型驱动)。
由于Xamarin.Forms(基于页面)中导航的性质,INavigationService仅适用于属于Page类型的ViewModel。它必须是一个页面,否则,您将无法导航。如果必须在此ContentView的VM中具有INavigationService,则必须在容器中注册INavigationService,但它可能无法正常运行,因为它将在Application.Current.MainPage(而不是正确的Page)上运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句