【问题标题】:How to stop TabbedPage icon from shifting up when it is selected如何在选择时阻止 TabbedPage 图标向上移动
【发布时间】:2020-04-02 14:47:17
【问题描述】:

我正在尝试构建一个导航栏,上面有 5 个图标(每个图标都有自己的页面)。我目前遇到的问题是,每当我单击其中一个图标时,它会在您选择该页面时将其向上移动一点(类似于弹出窗口)。我知道它可能是某种必须设置为 false 的属性,但我不知道具体是哪个。 任何帮助将不胜感激,谢谢。

MainPage.xaml 的 XAML 代码:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:views="clr-namespace:StudioDen.View"
            mc:Ignorable="d"
            x:Class="StudioDen.MainPage"
            BarBackgroundColor="#FFDA00"
            UnselectedTabColor="Black"
            SelectedTabColor="Black">
            <!--The last three properties set the colours of the bar to yellow and the icons to black-->


    <!--Projects view on the tabbed page-->
    <NavigationPage IconImageSource="ic_projects.png" >
        <x:Arguments>
            <views:Projects />
        </x:Arguments>
    </NavigationPage>

    <!--Events view on the tabbed page-->
    <NavigationPage IconImageSource="ic_events.png" >
        <x:Arguments>
            <views:Events/>
        </x:Arguments>
    </NavigationPage>

    <!--Upload view on the tabbed page-->
    <NavigationPage IconImageSource="ic_uploadV2.png" >
        <x:Arguments>
            <views:Upload/>
        </x:Arguments>
    </NavigationPage>

    <!--Process view on the tabbed page-->
    <NavigationPage IconImageSource="ic_processV3.png" >
        <x:Arguments>
            <views:Process />
        </x:Arguments>
    </NavigationPage>

    <!--Login view on the tabbed page-->
    <NavigationPage IconImageSource="ic_loginV2.png" >
        <x:Arguments>
            <views:Login />
        </x:Arguments>
    </NavigationPage>
</TabbedPage>

选择了第三个图标的导航栏图像:

【问题讨论】:

    标签: c# android xaml xamarin.forms tabbedpage


    【解决方案1】:

    这是 Android 默认执行的操作。您可以通过为它写一个Effect 来禁用它。

    确保您使用的是 28.0.0.1 Android 支持库并且面向的是 Android 9.0 (Pie)。如果你不能,有一个(坏的)reflection trick 你可以试试。

    将此NoShiftEffect.cs 添加到您的 Android 项目中,当然还可以重命名特定于您的项目和解决方案的命名空间和其他值:

    using Android.Support.Design.BottomNavigation;
    using Android.Support.Design.Widget;
    using Android.Views;
    using App16.Droid;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly:ResolutionGroupName ("MyCompany")]
    [assembly:ExportEffect (typeof(NoShiftEffect), "NoShiftEffect")]
    namespace App16.Droid
    {
        public class NoShiftEffect : PlatformEffect
        {
            protected override void OnAttached ()
            {
                if (!(Container.GetChildAt(0) is ViewGroup layout))
                    return;
    
                if (!(layout.GetChildAt(1) is BottomNavigationView bottomNavigationView))
                    return;
    
                // This is what we set to adjust if the shifting happens
                bottomNavigationView.LabelVisibilityMode = LabelVisibilityMode.LabelVisibilityLabeled;
            }
    
            protected override void OnDetached ()
            {
            }
        }
    }
    

    现在将另一个NoShiftEffect.cs 添加到您的共享项目中:

    using Xamarin.Forms;
    
    namespace App16
    {
        public class NoShiftEffect : RoutingEffect
        {
            public NoShiftEffect() : base("MyCompany.NoShiftEffect")
            {
            }
        }
    }
    

    最后在你的TabbedPage中消费效果:

    <TabbedPage.Effects>
        <local:NoShiftEffect />
    </TabbedPage.Effects>
    

    感谢 James Montemagno 和他在上面的帖子 here

    【讨论】:

    • 我查看了您在该教程中分享的代码,您能否告诉我上面示例中的“MyCompany”代表什么?
    • 它可以是任何东西,它只是用作标识符。它类似于 .NET 中的命名空间。如果您有任何使用效果的插件,这可以防止任何命名冲突。在这里阅读更多:docs.microsoft.com/en-us/dotnet/api/…
    猜你喜欢
    • 2017-11-24
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多