2016-05-19 17 views
3

私は現在Xamarin.AndroidとMvvmCrossでモバイルアプリケーションを開発中です。私は適切に動作する異なるビューを持っています(MvxActivity)。しかし最後のビューに問題があります。ShowViewModel <MvxViewModel>でナビゲートすると、情報なしでアプリケーションがクラッシュします(yeap、stacktraceはまだ空です)。このビューでは、CoordinatorLayoutを使用して重要なタブを作成しようとしました。このために、私は(Windowsで正しく動作します)のViewModelを持っている:このViewModelにのためMvvmCross:情報なしでAndroidアプリがクラッシュする

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

アンアンドロイドビュー:このビュー(Resource.Layout.Home)用

[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レイアウト: FirstFragment、SecondFragment、ThirdFragment、次のようになります。:

明らか

<?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> 

は、私は私の3つの断片を持っています

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

私のスタックトレースの最後の行:

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

私はGitHubに例を入れました:https://github.com/andrestalavera/MvvmCrossTabsSample –

答えて

1

をリポジトリは空なので。あなたは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; 
    } 

マイmainactivityがMvxCachingFragmentCompatActivity aswellあるでこれを得た場合

[MvxFragment(typeof(MainActivityViewModel), Resource.Id.fragmentone)] 
[Register("mobilexcp.xcpandroidapp.fragments.fragmentone")] 
public class FragmentOne: BaseFragment<FirstViewModel> 

わからない:

一つの理由は、あなたが適切に要素を登録していけないということかもしれません。

+0

これはCreateViewPresenterオーバーライドなしで動作します:) –

関連する問題