Ich versuche, vom MainViewModel zum AccountsViewModel zu navigieren. Wenn die App startet, bleibt sie im MainViewModel hängen. Ich habe einiges an Debugging gemacht und bin mehrere Beispiele durchgegangen, aber ich kann dieses Problem nicht lösen. Die relevanten Klassen sind unten bitte lassen Sie mich wissen, wenn Sie noch etwas brauchen.MvvmCross 5.4 Android-Fragmente: Navigieren zu ViewModel nicht möglich
App
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>();
}
}
}
Einrichtung
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>
Erweitern Sie vielleicht nicht die falsche Ansicht? Ihr 'AccountsView'-Fragment füllt eine XML-Ansicht mit dem Namen' CreateAccountView' und Sie haben gesagt, dass das erwartete XML-Layout 'AccountsView' heißt. Randnotiz: Sie müssen den Moderator 'MvxAppCompatViewPresenter' nicht überschreiben, wenn Sie' MvxAppCompatSetup' als Standard verwenden. – Plac3Hold3r
Sie haben Recht. Ich wollte AccountsView aufblasen. Ich habe es korrigiert und meine Überschreibung von MvxAppCompatViewPresenter entfernt. Ich bekomme immer noch einen leeren weißen Bildschirm beim Start. Ich habe die Frage bearbeitet, um meine Änderungen widerzuspiegeln. – b2norma
Alles, was im Anwendungsausgabefenster relevant ist? – nmilcoff