![](/img/trans.png)
[英]UWP+Prism MVVM - How do I set the value of a view model property via XAML?
[英]include xaml in xaml on xamarin forms with prism mvvm view model. Cannot resolve INavigationService
我想在所选页面中包含一个后退按钮。 由于某种原因,我将其称为“面包屑”,请假定其名为“ 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应用程序中包括常见的后退按钮样式的东西(由视图模型驱动)。
我看不到您完成的其他注册的任何代码。 您是否已正确注册NavigationService? 在您的设置中,应该在注册需要它的代码之前在App.cs中注册它。
另外,您应该使用ContainerControlledLifetimeManager 。 这实际上将其实现为单例(假设您希望在整个应用程序中传递相同的NavigationService)。 这在这里解释
Container.RegisterType<NavigationService>(new ContainerControlledLifetimeManager());
由于Xamarin.Forms(基于页面)中导航的性质,INavigationService仅适用于属于Page类型的ViewModel。 它必须是一个页面,否则,您将无法导航。 如果必须在此ContentView的VM中具有INavigationService,则必须在容器中注册INavigationService,但它可能无法按预期运行,因为它将在Application.Current.MainPage而不是正确的Page上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.