如何用MVVMCross 6.2设置标题标题?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用MVVMCross 6.2设置标题标题?相关的知识,希望对你有一定的参考价值。
我试图弄清楚如何在Xamarin android中使用MVVMCross 6.2+正确设置本地化字符串作为Tab Title(或动态更改Tab Title)。我应该如何在简单的示例应用程序中设置选项卡的标题?在此先感谢您的帮助。
这是一个简单的示例应用程序:
MvvmCrossTabs.Core
HomeViewModel.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using MvvmCross.Commands;
using MvvmCross.Logging;
using MvvmCross.Navigation;
using MvvmCross.ViewModels;
namespace MvvmCrossTabs.Core.ViewModels
public class HomeViewModel : MvxNavigationViewModel
public IMvxAsyncCommand ShowInitialViewModelsCommand get; private set;
public HomeViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigationService) : base(logProvider, navigationService)
ShowInitialViewModelsCommand = new MvxAsyncCommand(ShowInitialViewModels);
private async Task ShowInitialViewModels()
await Task.WhenAll(new List<Task>
NavigationService.Navigate<Tab1ViewModel>(),
NavigationService.Navigate<Tab2ViewModel>(),
NavigationService.Navigate<Tab3ViewModel>()
);
Tab1ViewModel.cs(Tab2ViewModel.cs,Tab3ViewModel.cs)
using MvvmCross.Logging;
using MvvmCross.Navigation;
using MvvmCross.ViewModels;
namespace MvvmCrossTabs.Core.ViewModels
public class Tab1ViewModel : MvxNavigationViewModel
public Tab1ViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigationService) : base(logProvider, navigationService)
App.cs
using MvvmCross.IoC;
using MvvmCross.ViewModels;
using MvvmCrossTabs.Core.ViewModels;
namespace MvvmCrossTabs.Core
public class App : MvxApplication
public override void Initialize()
CreatableTypes()
.EndingWith("Service")
.AsInterfaces()
.RegisterAsLazySingleton();
RegisterAppStart<HomeViewModel>();
MvvmCrossTabs.Android
Mainaplikesansis
using System;
using Android.App;
using Android.Runtime;
using MvvmCross.Droid.Support.V7.AppCompat;
using MvvmCrossTabs.Core;
namespace MvvmCrossTabs.Android
[Application]
public class MainApplication : MvxAppCompatApplication<MvxAppCompatSetup<App>, App>
public MainApplication() : base()
public MainApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
home.axml
<?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/maincontent"
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.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="enterAlways"
app:tabGravity="fill"
app:tabMaxWidth="0dp" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Tab1.kasml(Tbak.kasml,Tba.kasml)
<?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:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
HomeView.cs
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Support.V7.Widget;
using MvvmCross.Droid.Support.V7.AppCompat;
using MvvmCross.Platforms.Android.Presenters.Attributes;
using MvvmCrossTabs.Core.ViewModels;
namespace MvvmCrossTabs.Android
[Activity(Label = "@string/app_name", LaunchMode = LaunchMode.SingleTask, Theme = "@style/AppTheme", MainLauncher = true)]
[MvxActivityPresentation]
public class HomeView : MvxAppCompatActivity<HomeViewModel>
protected override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.home);
// Replaces Action Bar with new Toolbar.
var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
ViewModel.ShowInitialViewModelsCommand.Execute();
Tab1View.cs(Tab2View.cs,Tab3View.cs)
using Android.OS;
using Android.Runtime;
using Android.Views;
using MvvmCross.Droid.Support.V4;
using MvvmCross.Platforms.Android.Binding.BindingContext;
using MvvmCross.Platforms.Android.Presenters.Attributes;
using MvvmCrossTabs.Core.ViewModels;
namespace MvvmCrossTabs.Android.Views
[MvxTabLayoutPresentation(TabLayoutResourceId = Resource.Id.tabs, ViewPagerResourceId = Resource.Id.viewpager, Title = "Tab 1", ActivityHostViewModelType = typeof(HomeViewModel))]
[Register(nameof(Tab1View))]
public class Tab1View : MvxFragment<Tab1ViewModel>
public override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);
// Create your fragment here
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.tab1, null);
使用ShowInitialViewModelsCommand
创建选项卡时,内置的演示者会创建MvxViewPagerFragmentInfo
对象列表,并从属性中传入Title值。您可以在MvvmCross源代码中的ShowViewPagerFragment方法中看到这种情况。
然后将MvxViewPagerFragmentInfo
对象列表传递到为ViewPager创建的MvxCachingFragmentStatePagerAdapter
。你可以看到发生here
MvxCachingFragmentStatePagerAdapter
继承自MvxFragmentPagerAdapter
类。在MvxFragmentPagerAdapter
类中最终实际使用了您提供的标题。你可以看到它在GetPageTitleFormatted
方法here中使用
因此,为了在运行时更改标题,您可以执行以下操作:
子类化默认的演示者并覆盖ShowViewPagerFragment
方法(标记为虚拟)并提供正确的本地化标题字符串,而不是属性中定义的标题字符串
以下是如何完成此操作的示例:
1.)创建自定义演示者并覆盖ShowViewPagerFragment
public class LocalizedTabPresenter : MvxAppCompatViewPresenter
public LocalizedTabPresenter(IEnumerable<Assembly> androidViewAssemblies) : base(androidViewAssemblies)
protected override Task<bool> ShowViewPagerFragment(Type view, MvxViewPagerFragmentPresentationAttribute attribute, MvxViewModelRequest request)
if (attribute.ViewModelType == typeof(Tab1ViewModel))
attribute.Title = "My Localized Title for Tab 1"
return base.ShowViewPagerFragment(view, attribute, request);
2.)在你的Setup.cs
课程中,让MvvmCross知道使用自定义演示者
protected override IMvxAndroidViewPresenter CreateViewPresenter()
return new LocalizedTabPresenter(AndroidViewAssemblies);
注意:
- 这仅在您需要在应用程序启动时设置标题并且第一次设置选项卡时才有效。
如果您在应用程序运行时标题可以多次更改,您需要继承MvxCachingFragmentStatePagerAdapter
并覆盖GetPageTitleFormatted
方法并提供更适合您的用例的自定义实现。
希望有所帮助。
你必须这样做:
if (fragments == null || fragments.Count == 0)
_firstFragment= (YourFragmentType)Activator.CreateInstance(typeof(YourFragmentType));
_firstFragment.ViewModel = YourVM;
_secondFragment= (YourFragmentType)Activator.CreateInstance(typeof(YourFragmentType));
_secondFragment.ViewModel = YourVM;
_thridFragment= (YourFragmentType)Activator.CreateInstance(typeof(YourFragmentType));
_thridFragment.ViewModel = YourVM;
// Strings from RESX Localization
fragments = new Dictionary<string, Fragment>
Strings.first_localized_string, _firstFragment,
Strings.second_localized_string, _secondFragment,
Strings.thrid_localized_string, _thridFragment
;
viewPager = View.FindViewById<ViewPager>(Resource.Id.viewpager);
adapter = new TabsFragmentPagerAdapter(ChildFragmentManager, fragments);
viewPager.Adapter = adapter;
var tabLayout = View.FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewPager);
你的适配器:
public class TabsFragmentPagerAdapter : FragmentPagerAdapter
private readonly Fragment[] fragments;
private readonly string[] titles;
public TabsFragmentPagerAdapter(FragmentManager fm, Dictionary<string, Fragment> fragments) : base(fm)
this.fragments = fragments.Values.ToArray();
this.titles = fragments.Keys.ToArray();
public override int Count => fragments.Length;
private String GetCharSeuenceFromString(string s)
return new String(s);
public override Object InstantiateItem(ViewGroup container, int position)
return base.InstantiateItem(container, position);
public override void SetPrimaryItem(ViewGroup container, int position, Object @object)
base.SetPrimaryItem(container, position, @object);
public override Fragment GetItem(int position)
return fragments[position];
public override ICharSequence GetPageTitleFormatted(int position)
return GetCurrentPageTitle(position);
private ICharSequence GetCurrentPageTitle(int position)
return GetCharSeuenceFromString(titles[position]);
快乐的编码!
附:不要使用带有泛型的碎片。
以上是关于如何用MVVMCross 6.2设置标题标题?的主要内容,如果未能解决你的问题,请参考以下文章
MvvmCross.Forms 5.4:如何设置 UWP 项目?
如何将 Xamarin Forms Shell 集成到 MvvmCross 设置中