XAML 如何使用Community Toolkit NuGet包中的TabView重新创建此处显示的设计?(链接在详细信息中提供)

nbysray5  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(95)

我有问题复制在这里的链接中提出的设计:Extending TabbedPage in Xamarin Forms
我从头开始创建了一个项目,并使用了提供的所有代码片段。我在共享项目中创建了一个文件夹,并添加了一个继承TabbedPage的类。在Android项目中,我创建了一个CustomRenderer文件夹,其中包含最后一个代码片段,该代码片段在选定的选项卡项下添加了一行:

public class ExtendedTabbedPageRenderer : TabbedPageRenderer
    {
        Xamarin.Forms.TabbedPage tabbedPage;
        BottomNavigationView bottomNavigationView;
        private bool firstTime = true;

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TabbedPage> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                tabbedPage = e.NewElement as ExtendedTabbedPage;
                bottomNavigationView = (GetChildAt(0) as Android.Widget.RelativeLayout).GetChildAt(1) as BottomNavigationView;
                bottomNavigationView.NavigationItemSelected += BottomNavigationView_NavigationItemSelected;
            }

        }
       
        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);
           
            if (firstTime && bottomNavigationView != null)
            {
                for (int i = 0; i < Element.Children.Count; i++)
                {
                    var item = bottomNavigationView.Menu.GetItem(i);
                    if (bottomNavigationView.SelectedItemId == item.ItemId)
                    {
                        SetupBottomNavigationView(item);
                        break;
                    }
                }
                firstTime = false;
            }
        }

        void BottomNavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
        {
            SetupBottomNavigationView(e.Item);
            this.OnNavigationItemSelected(e.Item);
        }

        //Adding line view
        void SetupBottomNavigationView(IMenuItem item)
        {
            int lineBottomOffset = 8;
            int lineWidth = 4;
            int itemHeight = bottomNavigationView.Height - lineBottomOffset;
            int itemWidth = (bottomNavigationView.Width / Element.Children.Count);
            int leftOffset = item.ItemId * itemWidth;
            int rightOffset = itemWidth * (Element.Children.Count - (item.ItemId + 1));
            GradientDrawable bottomLine = new GradientDrawable();
            bottomLine.SetShape(ShapeType.Line);
            bottomLine.SetStroke(lineWidth, Xamarin.Forms.Color.DarkGray.ToAndroid());

            var layerDrawable = new LayerDrawable(new Drawable[] { bottomLine });
            layerDrawable.SetLayerInset(0, leftOffset, itemHeight, rightOffset, 0);

            bottomNavigationView.SetBackground(layerDrawable);
        } 
}

但是,如果我运行该项目,我看不到对TabbedPage所做的任何更改,这是徒劳的。
如果可能的话,我希望使用TabView重新创建这个设计,如问题标题中所指定的,或者至少了解我在这里做错了什么。
任何人都可以帮助我的解决方案吗?我也可以提供我的项目结构,以帮助您更好地了解我的问题。感谢提前和任何回应是非常感谢。

0dxa2lsx

0dxa2lsx1#

如果你看一下sample sources for that xamgirl article,你会发现他们在名称空间声明之前省略了一个ESSENTIAL行[assembly: ExportRenderer( ...

using Xamarin.Forms;
using CustomTabbedPage.Droid;

[assembly: ExportRenderer(typeof(ExtendedTabbedPage), typeof(ExtendedTabbedPageRenderer))]
namespace CustomTabbedPage.Droid
{
    public class ExtendedTabbedPageRenderer : TabbedPageRenderer

如果没有这行代码,Xamarin就不知道如何使用自定义渲染器。
如果名称不同,请替换ExtendedTabbedPageExtendedTabbedPageRenderer的名称。
CustomTabbedPage.Droid替换为自定义呈现器文件中的命名空间。请注意,在using ...;中也必须更改此命名空间
重要提示:如本文所述,此渲染器将不用于TabbedPage类。
它将仅用于ExtendedTabbedPage : TabbedPage类。
使用它的例子见文章或其来源。
IIRC,要将其用于应用中的所有TabbedPage,请将关键行更改为:

[assembly: ExportRenderer(typeof(TabbedPage), typeof(ExtendedTabbedPageRenderer))]

相关问题