0

MainViewModel에서 AccountsViewModel로 이동하려고합니다. 앱이 시작되면 MainViewModel에 멈추게됩니다. 꽤 많은 디버깅 작업을 수행했으며 여러 가지 예제를 통해이 문제를 해결할 수는 없습니다. 관련 수업은 아래에 나와 있습니다. 다른 것이 필요한 경우 알려 주시기 바랍니다.MvvmCross 5.4 Android Fragments : ViewModel로 이동할 수 없습니다.

using DebtBuddy.Core.ViewModels; 
using MvvmCross.Core.ViewModels; 
using MvvmCross.Platform.IoC; 

namespace DebtBuddy.Core 
{ 
    public class App : MvxApplication 
    { 
     public override void Initialize() 
     { 
      CreatableTypes() 
       .EndingWith("Service") 
       .AsInterfaces() 
       .RegisterAsLazySingleton(); 

      CreatableTypes() 
       .EndingWith("Repository") 
       .AsInterfaces() 
       .RegisterAsLazySingleton(); 

      RegisterNavigationServiceAppStart<MainViewModel>(); 
     } 
    } 
} 

설정

using Android.Content; 
using MvvmCross.Core.ViewModels; 
using MvvmCross.Platform.Platform; 
using MvvmCross.Droid.Views; 
using MvvmCross.Droid.Support.V7.AppCompat; 
using DebtBuddy.Core.Repositories; 
using MvvmCross.Platform; 

namespace DebtBuddy.Droid 
{ 
    public class Setup : MvxAppCompatSetup 
    { 
     public Setup(Context applicationContext) : base(applicationContext) 
     { 
     } 

     protected override IMvxApplication CreateApp() 
     { 
      var dbConn = FileAccessHelper.GetLocalFilePath("account.db3"); 
      Mvx.RegisterSingleton(new AccountRepository(dbConn)); 

      return new Core.App(); 
     } 

     protected override IMvxTrace CreateDebugTrace() 
     { 
      return new DebugTrace(); 
     } 
    } 
} 

MainViewModel

using MvvmCross.Core.Navigation; 
using MvvmCross.Core.ViewModels; 

namespace DebtBuddy.Core.ViewModels 
{ 
    public class MainViewModel : MvxViewModel 
    { 
     private readonly IMvxNavigationService _navigationService; 

     public MainViewModel(IMvxNavigationService navigationService) 
     { 
      _navigationService = navigationService; 

      ShowAccountsViewModel = new MvxAsyncCommand(async() => await 
      _navigationService.Navigate<AccountsViewModel>()); 
     } 

     public IMvxAsyncCommand ShowAccountsViewModel { get; private set; } 
    } 
} 

MAINVIEW

using Android.App; 
using MvvmCross.Droid.Support.V7.AppCompat; 
using DebtBuddy.Core.ViewModels; 
using Android.OS; 
using MvvmCross.Droid.Views.Attributes; 

namespace DebtBuddy.Droid.Views 
{ 
    [MvxActivityPresentation] 
    [Activity(Theme = "@style/AppTheme")] 
    public class MainView : MvxAppCompatActivity<MainViewModel> 
    { 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      SetContentView(Resource.Layout.MainView); 

      ViewModel.ShowAccountsViewModel.ExecuteAsync(); 
     } 
    } 
} 

MainView.axml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

AccountsViewModel

using System.Collections.ObjectModel; 
using DebtBuddy.Core.Extensions; 
using DebtBuddy.Core.Interfaces.IDataServices; 
using DebtBuddy.Core.Models; 
using MvvmCross.Core.Navigation; 
using MvvmCross.Core.ViewModels; 

namespace DebtBuddy.Core.ViewModels 
{ 
    public class AccountsViewModel : MvxViewModel 
    { 
     private readonly IAccountDataService _accountDataService; 
     private readonly IMvxNavigationService _navigationService; 

     public AccountsViewModel(IMvxNavigationService navigationService, IAccountDataService accountDataService) 
     { 
      _navigationService = navigationService; 
      _accountDataService = accountDataService; 

      ShowCreateAccountViewModelCommand = new MvxAsyncCommand(async() => await _navigationService.Navigate<CreateAccountViewModel>()); 
      ShowAccountDetailViewModelCommand = new MvxAsyncCommand<Account (item => _navigationService.Navigate<AccountDetailViewModel, Account>(item)); 
     } 

     public IMvxAsyncCommand ShowCreateAccountViewModelCommand { get; private set; } 

     public IMvxAsyncCommand<Account> ShowAccountDetailViewModelCommand { get; private set; } 

     private ObservableCollection<Account> _accounts; 
     public ObservableCollection<Account> Accounts 
     { 
      get => _accounts; 
      set 
      { 
       _accounts = value; 
       RaisePropertyChanged(() => Accounts); 
      } 
     } 

     public override void Prepare() 
     { 
      LoadAccountsFromDatabase(); 
     } 

     private async void LoadAccountsFromDatabase() 
     { 
      Accounts = (await _accountDataService.GetAllAccounts()).ToObservableCollection(); 
     } 
    } 
} 

AccountsView

using Android.OS; 
using MvvmCross.Droid.Views.Attributes; 
using DebtBuddy.Core.ViewModels; 
using MvvmCross.Droid.Support.V4; 
using Android.Runtime; 
using Android.Views; 
using MvvmCross.Binding.Droid.BindingContext; 

namespace DebtBuddy.Droid.Views 
{ 
    [MvxFragmentPresentation(typeof(MainViewModel), Resource.Id.content_frame, true)] 
    [Register(nameof(AccountsView))] 
    public class AccountsView : MvxFragment<AccountsViewModel> 
    { 
     public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     { 
      base.OnCreateView(inflater, container, savedInstanceState); 

      var view = this.BindingInflate(Resource.Layout.AccountsView, null); 

      return view; 
     } 
    } 
} 

AccountsView.axml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_frame" 
    android:orientation="vertical" 
    android:background="@color/white" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <Mvx.MvxListView 
     android:id="@+id/account_list" 
     android:divider="@null" 
     android:scrollbars="vertical" 
     android:choiceMode="singleChoice" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_gravity="left|start" 
     local:MvxItemTemplate="@layout/item_account" 
     local:MvxBind="ItemsSource Accounts; ItemClick ShowAccountDetailViewModelCommand" /> 
    <Button 
     android:id="@+id/addAccountButton" 
     android:background="@color/white" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:text="Add Account" 
     android:textSize="17dp" 
     android:textColor="@color/black" 
     local:MvxBind="Click ShowCreateAccountViewModelCommand" /> 
</LinearLayout> 
+0

잘못된보기를 팽창시키지 않았습니까? 당신의'AccountsView' 조각은'CreateAccountView'라는 xml 뷰를 팽창시키고 당신이 기대하는 xml 레이아웃은'AccountsView'라고합니다. 참고로, MvxAppCompatSetup을 사용할 때'MvxAppCompatViewPresenter' 프리젠터를 오버라이드 할 필요가 없습니다. – Plac3Hold3r

+0

정확합니다. 나는 AccountsView를 팽창시키고 싶었다. 나는 그것을 정정하고 MvxAppCompatViewPresenter를 무시했다. 시작할 때 여전히 빈 화면이 나타납니다. 변경 사항을 반영하도록 질문을 수정했습니다. – b2norma

+0

응용 프로그램 출력 창과 관련된 것이 있습니까? – nmilcoff

답변

0

당신은 MvvmCross 샘플의 하나로 설정 클래스를이 있었나요

요점은 MvxAppCompatViewPresenter을 사용하는 것입니다.

+0

죄송합니다. 설치 클래스를 포함하는 것을 잊었습니다. MvxAppCompatViewPresenter를 사용하고 있습니다. – b2norma

+0

코드를 공유 할 수 있습니까? –

관련 문제