我目前正在开发一个带有Xamarin.Android和MvvmCross的移动应用程序。我有不同的视图正常工作(MvxActivity)。但是我对上一个观点有疑问:
当我使用ShowViewModel <MvxViewModel>
导航到它时,应用程序崩溃而没有信息(是的,堆栈跟踪仍为空)。在此视图中,我尝试使用CoordinatorLayout创建材质选项卡。为此,我有一个ViewModel(在Windows中正常工作):
public class HomeViewModel : MvxViewModel
{
protected readonly IService _service;
protected const string ValidateEditionMessage = "Voulez-vous modifier?";
protected const string Edit = "Modifier";
protected const string Confirmation = "Confirmation";
protected const string Cancel = "Annuler";
public IMvxCommand SaveChangedCommand
{
get
{
return new MvxCommand<Item>(async (m) =>
{
var result = await Mvx.Resolve<IDialogService>().ShowAsync(ValidateEditionMessage, Confirmation, Edit, Cancel);
if (result.Value) await _service.UpdateAsync(_item.Id, _item);
});
}
}
protected Item _item;
public Item Item
{
get { return _item; }
set { this.RaiseAndSetIfChanged(ref _item, value, () => Item); }
}
private FirstViewModel _FirstViewModel;
public FirstViewModel FirstViewModel
{
get { return _FirstViewModel; }
set { this.RaiseAndSetIfChanged(ref _FirstViewModel, value, () => FirstViewModel); }
}
private SecondViewModel _SecondViewModel;
public SecondViewModel SecondViewModel
{
get { return _SecondViewModel; }
set { this.RaiseAndSetIfChanged(ref _SecondViewModel, value, () => SecondViewModel); }
}
private ThirdViewModel _ThirdViewModel;
public ThirdViewModel ThirdViewModel
{
get { return _ThirdViewModel; }
set { this.RaiseAndSetIfChanged(ref _ThirdViewModel, value, () => ThirdViewModel); }
}
public HomeViewModel(IService service)
{
_service = service;
FirstViewModel = new FirstViewModel();
SecondViewModel = new SecondViewModel();
ThirdViewModel = new ThirdViewModel();
}
public async void Init(int id)
{
Item = await _service.GetItemAsync(id);
FirstViewModel.Item = SecondViewModel.Item = ThirdViewModel.Item = Item;
}
}
此ViewModel的Android视图:
[Activity(Theme = "@style/Theme.AppCompat")]
public class HomeView : MvxCachingFragmentCompatActivity<HomeViewModel>
{
protected override void OnCreate(Bundle bundle)
{
Mvx.Trace("HomeView OnCreate(bundle)");
base.OnCreate(bundle);
SetContentView(Resource.Layout.Home);
var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
if (viewPager != null)
{
Mvx.Trace("viewPager found in " + nameof(HomeView));
var fragments = new List<MvxCachingFragmentStatePagerAdapter.FragmentInfo>
{
new MvxCachingFragmentStatePagerAdapter.FragmentInfo("First", typeof(FirstFragment), ViewModel.FirstViewModel),
new MvxCachingFragmentStatePagerAdapter.FragmentInfo("Second", typeof(SecondFragment), ViewModel.SecondViewModel),
new MvxCachingFragmentStatePagerAdapter.FragmentInfo("Third", typeof(ThirdFragment), ViewModel.ThirdViewModel)
};
Mvx.Trace("{0} fragments", fragments.Count);
viewPager.Adapter = new MvxCachingFragmentStatePagerAdapter(this, SupportFragmentManager, fragments);
}
var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewPager);
}
}
此视图的AXML布局(Resource.Layout.Home):
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
app:tabGravity="center"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
显然,我有三个片段:FirstFragment,SecondFragment,ThirdFragment,看起来像这样:
public class FirstFragment : MvxFragment
{
public FirstFragment()
{
Mvx.Trace(nameof(FirstFragment) + " constructor");
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return this.BindingInflate(Resource.Layout.First, null);
}
}
The corresponding ViewModels are basic, run on Windows, and are available on my GitHub, here
我的stacktrace的最后一行:
mvx : Diagnostic : 17.66 Showing ViewModel HomeViewModel
[art] JNI RegisterNativeMethods: attempt to register 0 native methods for md5204979768ea66d3a79201c4efd7c602a.MvxAppCompatActivity_1
mvx : Diagnostic : 17.69 HomeView constructor
[Mono] Assembly Ref addref MyApp.Droid[0xac176060] -> Xamarin.Android.Support.Design[0xac177b00]: 2
[Mono] Assembly Ref addref Xamarin.Android.Support.Design[0xac177b00] -> Xamarin.Android.Support.v4[0xac178640]: 5
mvx : Diagnostic : 17.70 HomeView OnCreate(bundle)
[Mono] Assembly Ref addref MvvmCross.Droid.Support.V7.AppCompat[0xac1787c0] -> Xamarin.Android.Support.v4[0xac178640]: 6
mvx : Diagnostic : 17.71 Attempting to load new ViewModel from Intent with Extras
[Mono] Assembly Ref addref MvvmCross.Droid.Support.V7.AppCompat[0xac1787c0] -> Xamarin.Android.Support.v4[0xac178640]: 6
mvx : Diagnostic : 17.71 Attempting to load new ViewModel from Intent with Extras
[Mono] Assembly Ref addref MvvmCross.Droid.Support.V7.AppCompat[0xac1787c0] -> System[0xac253020]: 13
[art] Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
[Mono] [0x97cba930] worker finishing
[] suspend_thread suspend took 201 ms, which is more than the allowed 200 ms
[libc] Fatal signal 6 (SIGABRT), code -6 in tid 6991 (Threadpool work)
答案 0 :(得分:1)
由于存储库为空。
一个原因可能是您没有正确注册元素:
[MvxFragment(typeof(MainActivityViewModel), Resource.Id.fragmentone)]
[Register("mobilexcp.xcpandroidapp.fragments.fragmentone")]
public class FragmentOne: BaseFragment<FirstViewModel>
不确定您是否在Setup.cs中有此内容
/// <summary>
/// This is very important to override. The default view presenter does not know how to show fragments!
/// </summary>
protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
var mvxFragmentsPresenter = new MvxFragmentsPresenter(AndroidViewAssemblies);
Mvx.RegisterSingleton<IMvxAndroidViewPresenter>(mvxFragmentsPresenter);
return mvxFragmentsPresenter;
}
我对片段的主要活动是MvxCachingFragmentCompatActivity。