2016-10-11 1 views
0

프리즘 MVVM 프레임 워크를 사용하는 제 xamarin 폼 앱에서 IoC 컨테이너는 어떻게 사용해야합니까? 내 생각에는Xamarin Forms : 프리즘 - 단일 IoC 사용과 관련된 문제?

Container.Resolve<Interface,Implentation> 

우리는 IOC의를 해결하고 app.xaml.cs.에 초기화 할 필요가 없습니다 있습니다 그러나 나는 그것을 할 수있는 적절한 방법을 찾을 수 없습니다. 누구든지 나를 도울 수 있습니까?

UPDATE :

페이지 :

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

    } 
} 

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

이 코드를 실행하면 IOS.Pro의 Main.cs에서 오류가 발생했습니다 오류 : Foundation.MonoTouchException NSInternalLnconistencyException 이유 : 응용 프로그램 창에 응용 프로그램의 끝에 루트보기 컨트롤러가 있어야합니다.

+0

Visual Studio Extension for Prism 템플릿 팩을 설치하고 그것을 사용하여 –

+0

솔루션을 만듭니다. – TheDeveloper

+0

컨테이너를 사용하여 IMenuList 인터페이스를 클래스로 등록하십시오. –

답변

1

당신이 찾고있는 것은 Unity의 확장 방법입니다. 코드 파일에 using Microsoft.Practices.Unity 네임 스페이스를 추가해야합니다. Container.Register<IService,MyService>()을 사용하여 서비스를 등록 할 수 있습니다.

+0

네임 스페이스를 추가했습니다. Unity 컨테이너에는 Register가 없으며 RegisterType 함수가 있습니다. 그래서, 나는'container.RegisterType ();'을 추가했지만 여전히 같은 오류가 발생합니다. – TheDeveloper

+0

그러면 뭔가 잘못하고있는 것입니다. 코드에 다른 문제가있을 수 있습니다. –

+0

예. 당신이 옳았. 그것을 해결했습니다. – TheDeveloper