Xamarin Forms:Prism——使用 unity IoC 有问题吗?

Xamarin Forms: Prism -- Issue with using unity IoC?

在我使用 Prism MVVM 框架的 xamarin 表单应用程序中,我应该如何使用 IoC 容器? 没有

Container.Resolve<Interface,Implentation>

我认为我们需要解决 IoC 并在 app.xaml.cs 中进行初始化。但我无法找到正确的方法来做到这一点。 谁能帮帮我?

更新:

页数:

<?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="CoC.Core.Views.MenuPage">
    <ContentPage.Content>
            <ListView x:Name="info" 
            SeparatorVisibility="None"
            SeparatorColor="Transparent"
            HasUnevenRows="false"
            RowHeight="50" BackgroundColor="#485366" > 
        <ListView.ItemTemplate> 
                    <DataTemplate>
                        <!--<ImageCell Text="{Binding Title}" ImageSource="{Binding IconSource}" /> -->
                        <ViewCell>
                            <!--<Label Text="{Binding Title}" />-->
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    </ContentPage.Content>
</ContentPage>

视图模型:

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using Prism.Navigation;

namespace CoC.Core.ViewModels
{
    public class MenuPageViewModel : BindableBase , INavigationAware
    {
        IMenuList _menuList;
        public ObservableCollection<MenuItem> ConductList { get; set; }

        public void OnNavigatedFrom(NavigationParameters parameters)
        {
            //throw new NotImplementedException();
        }

        public void OnNavigatedTo(NavigationParameters parameters)
        {
            //throw new NotImplementedException();
        }  

        public MenuPageViewModel(IMenuList MenuList)
        {
            _menuList = MenuList;
            ConductList = _menuList.GetMenuList();
        }

    }
}

应用程序:

using Prism.Unity;
using CoC.Core.Views;

namespace CoC.Core
{
    public partial class App : PrismApplication
    {
        protected override void OnInitialized()
        {
            InitializeComponent();

            //NavigationService.NavigateAsync("MainPage?title=Hello%20from%20Xamarin.Forms");
            NavigationService.NavigateAsync("MenuPage");
        }

        protected override void RegisterTypes()
        {
            Container.RegisterTypeForNavigation<MainPage>();
            Container.RegisterTypeForNavigation<RootPage>();
            Container.RegisterTypeForNavigation<MenuPage>();
        }
    }
}

当我 运行 这段代码时,我在 IOS pro 的 Main.cs 中遇到错误 错误: Foundation.MonoTouchException NSInternalLnconistencyException 原因:应用程序 window 应该在应用程序末尾有一个根视图控制器。

你要找的是Unity中的扩展方法。您需要将 using Microsoft.Practices.Unity 命名空间添加到您的代码文件中。您可以使用 Container.Register<IService,MyService>().

注册您的服务