xamarin 将Fragment转换为Android.Support.V4.App.Fragment

nr7wwzry  于 12个月前  发布在  Android
关注(0)|答案(1)|浏览(146)

我有这个代码:

using Android.App;
using Android.Content;
using Android.OS;
using MvvmCross;
using MvvmCross.Droid.Support.V7.AppCompat;
using MvvmCross.Forms.Views;
using MvvmCross.ViewModels;
using System;
using Xamarin.Forms.Platform.Android;

[....]

private void SetupPage()
        {
            if (_pageType == null) throw new NullReferenceException("ContentPage type can't be null. Call SetTypes() before creating the Intent");
            if (_viewModelType == null) throw new NullReferenceException("ViewModel type can't be null. Call SetTypes() before creating the Intent");

            var viewModel = Mvx.IoCProvider.Resolve(_viewModelType) as IMvxViewModel;

            var page = Activator.CreateInstance(_pageType) as MvxContentPage ?? 
                       Mvx.IoCProvider.Resolve(_pageType) as MvxContentPage;

            if(page == null || viewModel == null)
                return;
            
            page.ViewModel = viewModel;

            var fragment = page.CreateSupportFragment(this);
            SupportFragmentManager
                .BeginTransaction()
                .Replace(Resource.Id.fragment_container, fragment)
                .Commit();
        }

字符串
我尝试使用Android.Support.V4.App.Fragment fragment更新var fragment,但在此行出现此错误:

  • 无法将类型“AndroidX.Fragment.App.Fragment”隐式转换为“Android.Support.V4.App.Fragment”*


的数据

lbsnaicq

lbsnaicq1#

请使用AndroidX.Fragment.App.Fragment而不是AndroidX.Fragment.V4.App.Fragment
尝试替换以下代码:

Android.Support.V4.App.Fragment  fragment = page.CreateSupportFragment(this);
        SupportFragmentManager
            .BeginTransaction()
            .Replace(Resource.Id.fragment_container, fragment)
            .Commit();

字符串
使用:

AndroidX.Fragment.App.Fragment scanFragment = page.CreateSupportFragment(this);
        SupportFragmentManager
            .BeginTransaction()
            .Replace(Resource.Id.fragment_container, fragment)
            .Commit();

相关问题