In Xamarin app, tabs from TabbedView not Populated at the Same Time in Android Tablet emulator as in Android Phone emulator

夙愿已清 提交于 2020-06-29 04:23:29

问题


I am trying to set icons on tabs in my Xamarin app's TabbedView based on the state of the page displayed on the tab. I am able to access a tab individually and setting its icon during rendering on an Android phone emulator, but it does not work on Android tablet emulator.

I am using the DispatchDraw method in a TabbedRenderer based on this post, but I don't have a good understanding though of how that method is called and am having difficulty finding guidance on it.

My code does get called in both devices, but I find that (this.Context as Activity).ActionBar.TabCount is 0 in the tablet every time I check it, while it it is 0 initially but then DispatchDraw is called a second time and its value at that point is 2 (i.e. the tabs have loaded) on the phone, but not on the tablet. That's what allows me to set the icon on the phone, but not the tablet.

Any idea what would differ based only on the change of emulators, to make them render differently in this way?

Here is my TabbedRenderer code:

[assembly: ExportRenderer(typeof(FormTabbedPage), typeof(ExtendedTabbedPageRenderer))]
namespace MyNamespace
{
    public class ExtendedTabbedPageRenderer: TabbedRenderer
    {
        public ExtendedTabbedPageRenderer(Context context) : base(context) { }

        protected override void DispatchDraw(global::Android.Graphics.Canvas canvas)
        {
            base.DispatchDraw(canvas);

            SetTabIcons();
        }

        private void SetTabIcons()
        {

            var element = this.Element;
            if (null == element)
            {
                return;
            }

            Activity activity = this.Context as Activity;
            if ((null != activity && null != activity.ActionBar && activity.ActionBar.TabCount > 0))
            {
                bool hasErrors = false;
                for (int i = 0; i < element.Children.Count; i += 1)
                {
                    hasErrors = ((i % 2) > 0); // This is a placeholder for app specific logic based on page content
                    int resourceID = Resources.GetIdentifier("error", "drawable", Context.PackageName);
                    if (hasErrors)
                    {
                        Drawable icon = Context.GetDrawable(resourceID);
                        Android.App.ActionBar.Tab tab = activity.ActionBar.GetTabAt(i);
                        tab.SetIcon(icon);
                    }
                }


            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
           // do nothing - I have this method for debugging purposes
        }

}

I find that on the tablet, if I initially render the form, I don't get icons, but if I then click on one of the tabs, and if I run the tab logic in OnElementPropertyChanged method instead of DispatchDraw at that point, by then the tabs on the ActionBar ARE populated and I DO get the icons showing.

(Sidenote: one other odd thing is that there is a warning over the ActionBar class saying it's obsolete - not sure what to make of that, since the Activity class contains it as a property and that isn't obsolete.)

Many thanks!

来源:https://stackoverflow.com/questions/62272149/in-xamarin-app-tabs-from-tabbedview-not-populated-at-the-same-time-in-android-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!