MvvmCross 5.4 Android片段:无法导航到ViewModel

时间:2017-11-03 21:04:26

标签: android android-fragments xamarin cross-platform mvvmcross

我正在尝试从MainViewModel导航到AccountsViewModel。当应用程序启动时,它会卡在MainViewModel上。我做了很多调试并经历了多个例子,但无法解决这个问题。相关课程如下所示,如果您还有其他需要,请告诉我。

应用

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>

1 个答案:

答案 0 :(得分:0)

您是否已将您的安装程序类作为MvvmCross示例here

中的类

关键是使用 props: { onchangecallback: { type: Function, default() { return function() { alert('default'); }; }, }, }, created: function() { this.onchangecallback(); }