Xamarin.Forms Shell 自定义呈现器Xamarin.Forms Shell Custom Renderers

Xamarin.Forms Shell 应用程序的优势之一是可通过各种 Shell 类公开的属性和方法对其外观和行为进行高度自定义。One of the advantages of Xamarin.Forms Shell applications is that their appearance and behavior is highly customizable through the properties and methods that the various Shell classes expose. 不过,在需要更为复杂的平台特定的自定义时,也可创建 Shell 自定义呈现器。However, it's also possible to create a Shell custom renderer when more sophisticated platform-specific customizations are required. 与其他自定义呈现器一样,可以将 Shell 自定义呈现器添加到一个平台项目以自定义外观和行为,同时允许其他平台上的默认行为;或者可将其他 Shell 自定义呈现器添加到各个平台项目以自定义 iOS 和 Android 上的外观和行为。As with other custom renderers, a Shell custom renderer can be added to just one platform project to customize appearance and behavior, while allowing the default behavior on the other platform; or a different Shell custom renderer can be added to each platform project to customize appearance and behavior on both iOS and Android.

在 iOS 和 Android 上,使用 ShellRenderer 类呈现 Shell 应用程序。Shell applications are rendered using the ShellRenderer class on iOS and Android. 在 iOS 上,ShellRenderer 类位于 Xamarin.Forms.Platform.iOS 命名空间。On iOS, the ShellRenderer class can be found in the Xamarin.Forms.Platform.iOS namespace. 在 Android 上,ShellRenderer 类位于 Xamarin.Forms.Platform.Android 命名空间。On Android, the ShellRenderer class can be found in the Xamarin.Forms.Platform.Android namespace.

创建 Shell 自定义呈现器的过程如下所示:The process for creating a Shell custom renderer is as follows:

  1. 创建 Shell 类的子类。Subclass the Shell class. 此操作将已在 Shell 应用程序中完成。This will already be accomplished in your Shell application.
  2. 使用子类 Shell 类。Consume the subclassed Shell class. 此操作将已在 Shell 应用程序中完成。This will already be accomplished in your Shell application.
  3. 在所需的平台上创建派生自 ShellRenderer 类的自定义呈现器类。Create a custom renderer class that derives from the ShellRenderer class, on the required platforms.

创建自定义呈现器类Create a custom renderer class

创建 Shell 自定义呈现器类的过程如下所示:The process for creating a Shell custom renderer class is as follows:

  1. 创建 ShellRenderer 类的子类。Create a subclass of the ShellRenderer class.
  2. 重写所需的方法,以执行所需的自定义。Override the required methods to perform the required customization.
  3. ExportRendererAttribute 添加到 ShellRenderer 子类,以指定要使用它来呈现 Shell 应用程序。Add an ExportRendererAttribute to the ShellRenderer subclass, to specify that it will be used to render the Shell application. 此属性用于向 Xamarin.Forms 注册自定义呈现器。This attribute is used to register the custom renderer with Xamarin.Forms.

备注

可选择在每个平台项目中提供 Shell 自定义呈现器。It's optional to provide a Shell custom renderer in each platform project. 如果未注册自定义呈现器,则将使用默认的 ShellRenderer 类。If a custom renderer isn't registered, then the default ShellRenderer class will be used.

ShellRenderer 类公开下列可重写的方法:The ShellRenderer class exposes the following overridable methods:

iOSiOS AndroidAndroid
SetElementSize
CreateFlyoutRenderer
CreateNavBarAppearanceTracker
CreatePageRendererTracker
CreateShellFlyoutContentRenderer
CreateShellItemRenderer
CreateShellItemTransition
CreateShellSearchResultsRenderer
CreateShellSectionRenderer
CreateTabBarAppearanceTracker
Dispose
OnCurrentItemChanged
OnElementPropertyChanged
OnElementSet
UpdateBackgroundColor
CreateFragmentForPage
CreateShellFlyoutContentRenderer
CreateShellFlyoutRenderer
CreateShellItemRenderer
CreateShellSectionRenderer
CreateTrackerForToolbar
CreateToolbarAppearanceTracker
CreateTabLayoutAppearanceTracker
CreateBottomNavViewAppearanceTracker
OnElementPropertyChanged
OnElementSet
SwitchFragment
Dispose

FlyoutItemTabBar 类是 ShellItem 类的别名,而 Tab 类是 ShellSection 类的别名。The FlyoutItem and TabBar classes are aliases for the ShellItem class, and the Tab class is an alias for the ShellSection class. 因此,在为 FlyoutItem 对象创建自定义呈现器时应重写 CreateShellItemRenderer 方法,在为 Tab 对象创建自定义呈现器时应重写 CreateShellSectionRenderer 方法。Therefore, the CreateShellItemRenderer method should be overridden when creating a custom renderer for FlyoutItem objects, and the CreateShellSectionRenderer method should be overridden when creating a custom renderer for Tab objects.

重要

iOS 和 Android 上都包含其他 Shell 呈现器类,如 ShellSectionRendererShellItemRendererThere are additional Shell renderer classes, such as ShellSectionRenderer and ShellItemRenderer, on both iOS and Android. 但这些其他呈现器类由 ShellRenderer 类的重写创建而成。However, these additional renderer classes are created by overrides in the ShellRenderer class. 因此,可以创建这些其他呈现器类的子类,并在子类 ShellRenderer 类的相应重写中创建该子类的实例,以此方式来自定义这些呈现器类的行为。Therefore, customizing the behavior of these additional renderer classes can be achieved by subclassing them, and creating an instance of the subclass in the appropriate override in the subclassed ShellRenderer class.

iOS 示例iOS example

以下代码示例说明了用于 iOS 的子类 ShellRenderer,它将设置 Shell 应用程序导航栏的背景图像:The following code example shows a subclassed ShellRenderer, for iOS, that sets a background image on the navigation bar of the Shell application:

using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(Xaminals.AppShell), typeof(Xaminals.iOS.MyShellRenderer))]
namespace Xaminals.iOS
{
    public class MyShellRenderer : ShellRenderer
    {
        protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
        {
            var renderer = base.CreateShellSectionRenderer(shellSection);
            if (renderer != null)
            {
                (renderer as ShellSectionRenderer).NavigationBar.SetBackgroundImage(UIImage.FromFile("monkey.png"), UIBarMetrics.Default);
            }
            return renderer;
        }
    }
}

MyShellRenderer 类重写 CreateShellSectionRenderer 方法,并检索基类创建的呈现器。The MyShellRenderer class overrides the CreateShellSectionRenderer method, and retrieves the renderer created by the base class. 然后它将设置导航栏的背景图像,以修改呈现器,然后再返回呈现器。It then modifies the renderer by setting a background image on the navigation bar, before returning the renderer.

Android 示例Android example

以下代码示例说明了用于 Android 的子类 ShellRenderer,它将设置 Shell 应用程序导航栏的背景图像:The following code example shows a subclassed ShellRenderer, for Android, that sets a background image on the navigation bar of the Shell application:

using Android.Content;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Xaminals.AppShell), typeof(Xaminals.Droid.MyShellRenderer))]
namespace Xaminals.Droid
{
    public class MyShellRenderer : ShellRenderer
    {
        public MyShellRenderer(Context context) : base(context)
        {
        }

        protected override IShellToolbarAppearanceTracker CreateToolbarAppearanceTracker()
        {
            return new MyShellToolbarAppearanceTracker(this);
        }
    }
}

MyShellRenderer 类重写 CreateToolbarAppearanceTracker 方法,然后返回 MyShellToolbarAppearanceTracker 类的实例。The MyShellRenderer class overrides the CreateToolbarAppearanceTracker method, and returns an instance of the MyShellToolbarAppearanceTracker class. 以下示例说明了派生自 ShellToolbarAppearanceTracker 类的 MyShellToolbarAppearanceTracker 类:The MyShellToolbarAppearanceTracker class, which derives from the ShellToolbarAppearanceTracker class, is shown in the following example:

using Android.Support.V7.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

namespace Xaminals.Droid
{
    public class MyShellToolbarAppearanceTracker : ShellToolbarAppearanceTracker
    {
        public MyShellToolbarAppearanceTracker(IShellContext context) : base(context)
        {
        }

        public override void SetAppearance(Toolbar toolbar, IShellToolbarTracker toolbarTracker, ShellAppearance appearance)
        {
            base.SetAppearance(toolbar, toolbarTracker, appearance);
            toolbar.SetBackgroundResource(Resource.Drawable.monkey);
        }
    }
}

MyShellToolbarAppearanceTracker 类重写 SetAppearance 方法,然后设置工具栏的背景图像,以修改工具栏。The MyShellToolbarAppearanceTracker class overrides the SetAppearance method, and modifies the toolbar by setting a background image on it.

重要

只需将 ExportRendererAttribute 添加到派生自 ShellRenderer 类的自定义呈现器。It's only necessary to add the ExportRendererAttribute to a custom renderer that derives from the ShellRenderer class. 其他子类 Shell 呈现器类由子类 ShellRenderer 类创建而成。Additional subclassed Shell renderer classes are created by the subclassed ShellRenderer class.