简体   繁体   中英

Xamarin Forms: Prism — Issue with using unity IoC?

In my xamarin forms app using Prism MVVM Framework, How should I use IoC containers? There are no

Container.Resolve<Interface,Implentation>

I think we need to resolve the IoC and initialize in app.xaml.cs. But I am not able to find proper way to do it. Can anyone help me out ?

UPDATE:

PAGE:

<?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>

ViewModel:

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();
        }

    }
}

APP:

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>();
        }
    }
}

When I run this code, I am getting error in Main.cs of IOS pro Error: Foundation.MonoTouchException NSInternalLnconistencyException Reason: Application window are expected to have a root view controller at the end of the application.

What you are looking for is an extension method in Unity. You need to add the using Microsoft.Practices.Unity namespace to your code file. The you can register your services using Container.Register<IService,MyService>() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM