gpt4 book ai didi

c# - 在带有 Prism mvvm View 模型的xamarin形式的xaml中包括xaml。无法解析INavigationService

转载 作者:行者123 更新时间:2023-12-03 10:26:52 25 4
gpt4 key购买 nike

我想在所选页面中包含一个后退按钮。由于某种原因,我将其称为“面包屑”,请假定其名为“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>

面包屑页面 View 模型
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();
}
}

(*)

An exception of type 'Microsoft.Practices.Unity.ResolutionFailedException' occurred in Microsoft.Practices.Unity.dll but was not handled in user code

Additional information: Resolution of the dependency failed, type = "Test.ViewModels.BreadcrumbControlViewModel", name = "(none)".

Exception occurred while: Resolving parameter "navigationService" of constructor Test.ViewModels.BreadcrumbControlViewModel(Prism.Navigation.INavigationService navigationService).

Exception is: NullReferenceException - Object reference not set to an instance of an object.


At the time of the exception, the container was:

Resolving Test.ViewModels.BreadcrumbControlViewModel,(none)

Resolving parameter "navigationService" of constructor Test.ViewModels.BreadcrumbControlViewModel(Prism.Navigation.INavigationService navigationService)



App.cs容器/解析器注册
ViewModelLocationProvider.Register<BreadcrumbControl, BreadcrumbControlViewModel>();

Container.RegisterTypeForNavigation<MainPage>();

如何在Xamarin Forms Prism MVVM应用程序中包含常见的后退按钮样式的东西(由 View 模型驱动)。

最佳答案

由于Xamarin.Forms(基于页面)中导航的性质,INavigationService仅适用于属于Page类型的ViewModel。它必须是一个页面,否则,您将无法导航。如果必须在此ContentView的VM中具有INavigationService,则必须在容器中注册INavigationService,但是它可能无法正常运行,因为它将在Application.Current.MainPage而不是正确的Page上运行。

关于c# - 在带有 Prism mvvm View 模型的xamarin形式的xaml中包括xaml。无法解析INavigationService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39426929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com