简体   繁体   English

Xamarin形式:棱镜—使用统一IoC是否存在问题?

[英]Xamarin Forms: Prism — Issue with using unity IoC?

In my xamarin forms app using Prism MVVM Framework, How should I use IoC containers? 在使用Prism MVVM Framework的xamarin表单应用程序中,应如何使用IoC容器? There are no 没有

Container.Resolve<Interface,Implentation>

I think we need to resolve the IoC and initialize in app.xaml.cs. 我认为我们需要解决IoC并在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: 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: 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. 运行此代码时,我在IOS pro的Main.cs中遇到错误错误:Foundation.MonoTouchException NSInternalLnconistencyException原因:应用程序窗口应在应用程序末尾具有根视图控制器。

What you are looking for is an extension method in Unity. 您正在寻找的是Unity中的扩展方法。 You need to add the using Microsoft.Practices.Unity namespace to your code file. 您需要将using Microsoft.Practices.Unity命名空间添加到代码文件中。 The you can register your services using Container.Register<IService,MyService>() . 您可以使用Container.Register<IService,MyService>()来注册服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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