android - Android : Xamarin Forms 中选项卡式页面的自定义渲染器

标签 android xamarin.forms

我正在开发一个 Xamarin.Forms 应用程序。我在 iOS 中有这个选项卡式页面渲染器:

public class TabbedPageRenderer : TabbedRenderer
{
    private MainPage _page;

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {
            _page = (MainPage)e.NewElement;
        }
        else
        {
            _page = (MainPage)e.OldElement;
        }

        try
        {
            var tabbarController = (UITabBarController)this.ViewController;

            if (null != tabbarController)
            {
                tabbarController.ViewControllerSelected += OnTabBarReselected;
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
    }

    private void OnTabBarReselected(object sender, UITabBarSelectionEventArgs e)
    {

        var tabs = Element as TabbedPage;
        var playTab = tabs.Children[4];

        if (TabBar.SelectedItem.Title == "Play")
        {
            if (tabs != null)
            {
                playTab.Title = "Pause";
                playTab.Icon = "pause.png";
            }
            App.pauseCard = false;

        }
        else 
        {
            if (tabs != null)
            {
                playTab.Title = "Play";
                playTab.Icon = "play.png";
            }
            App.pauseCard = true;
        }
    }
}

这基本上将我的 Play 选项卡上的图标更改为暂停和播放。这在 iOS 中运行良好。但我正在努力研究如何在 Android 端具有相同的功能(基本上将其转换为 Android)。

能给我指出正确的方向吗?主要是帮我? :-)

注意:我是 Android 开发的新手。

编辑:这就是它在 iOS 中的样子。

暂停模式: enter image description here 游戏模式: enter image description here

最佳答案

有一个blog Xamarin 的 James Montemagno 发表的文章解释了如何实现这一要求。

基本上,它使用继承自 TabbedPage 的自定义选项卡,它初始化一个事件 UpdateIcons 以在 Tab Current Page Changed 事件 CurrentPageChanged

上触发
public class MyTabs : TabbedPage
{
    //always save a reference to the current page
    Page currentPage;
    public MyTabs()
    {
        //create the pages and set the view models
        //you could also do this in the page code behind
        Children.Add(new TabIconsPage
        {
            BindingContext = new Tab1ViewModel
            {
                IsSelected = true
            }
        });
        Children.Add(new TabIconsPage2
        {
            BindingContext = new Tab2ViewModel()
        });

        currentPage = Children[0];

        //Register for page changes
        this.CurrentPageChanged += Handle_CurrentPageChanged;
    }

    //Update the IsSelected state and trigger an Event that anyone can loop into.
    public event EventHandler UpdateIcons;
    void Handle_CurrentPageChanged(object sender, EventArgs e)
    {
        var currentBinding = currentPage.BindingContext as IIconChange;
        if (currentBinding != null)
            currentBinding.IsSelected = false;

        currentPage = CurrentPage;
        currentBinding = currentPage.BindingContext as IIconChange;
        if (currentBinding != null)
            currentBinding.IsSelected = true;

        UpdateIcons?.Invoke(this, EventArgs.Empty);
    }
}

现在 Android 需要一个自定义渲染器来订阅 UpdateIcons 事件并执行图标更改

public class MyTabs : TabbedPage
{
    //always save a reference to the current page
    Page currentPage;
    public MyTabs()
    {
        //create the pages and set the view models
        //you could also do this in the page code behind
        Children.Add(new TabIconsPage
        {
            BindingContext = new Tab1ViewModel
            {
                IsSelected = true
            }
        });
        Children.Add(new TabIconsPage2
        {
            BindingContext = new Tab2ViewModel()
        });

        currentPage = Children[0];

        //Register for page changes
        this.CurrentPageChanged += Handle_CurrentPageChanged;
    }

    //Update the IsSelected state and trigger an Event that anyone can loop into.
    public event EventHandler UpdateIcons;
    void Handle_CurrentPageChanged(object sender, EventArgs e)
    {
        var currentBinding = currentPage.BindingContext as IIconChange;
        if (currentBinding != null)
            currentBinding.IsSelected = false;

        currentPage = CurrentPage;
        currentBinding = currentPage.BindingContext as IIconChange;
        if (currentBinding != null)
            currentBinding.IsSelected = true;

        UpdateIcons?.Invoke(this, EventArgs.Empty);
    }
}

关于android - Android : Xamarin Forms 中选项卡式页面的自定义渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48415481/

相关文章:

android - 没有从android中的文档中获取所选文件的实际视频路径

c# - Xamarin.Forms:绑定(bind)到 XAML 中的代码隐藏属性

android - 谷歌云消息 'Not Registered'失败和退订最佳实践?

xamarin - 如何解决 "Unable to resolve host"错误?

android - 列表 Activity 中的上下文菜单

android - 无法从我的 Android 应用程序获取电话号码?

java - 使用Gson解析以下字符串

android - MVP 模式。将所有演示者的依赖项注入(inject)演示者的构造函数中是一个好习惯吗?

c# - 无法选择 ListView 项目两次

xamarin - 如何在我的应用程序中正确运行定时进程,该进程将在应用程序使用中或在后台启动时停止?