C# 如何更改Shell Tabbar高度
我的问题是: 我将Shell与底部C# 如何更改Shell Tabbar高度,c#,android,xamarin.forms,custom-renderer,xamarin.forms.shell,C#,Android,Xamarin.forms,Custom Renderer,Xamarin.forms.shell,我的问题是: 我将Shell与底部Tabbar一起使用,我只想在Android上或者iOS上更改它的高度 我的搜索结果: 我知道,Tabbar是一个Android/iOS内置功能,具有固定的指定高度 似乎有一种方法可以使用“自定义渲染器”实现这一点。我发现的所有使用这种方法的项目都不能与我的VisualStudio一起工作(>100个错误或干脆什么都不做) 我发现很难相信我需要超过100行的代码来“仅”调整高度。是否有人能提供一个有效的解决方案来更改外壳选项卡(底部选项卡)的高度 编辑 毫无疑问
Tabbar
一起使用,我只想在Android上或者iOS上更改它的高度
我的搜索结果:
我知道,Tabbar
是一个Android/iOS内置功能,具有固定的指定高度
似乎有一种方法可以使用“自定义渲染器”实现这一点。我发现的所有使用这种方法的项目都不能与我的VisualStudio一起工作(>100个错误或干脆什么都不做)
我发现很难相信我需要超过100行的代码来“仅”调整高度。是否有人能提供一个有效的解决方案来更改外壳选项卡(底部选项卡)的高度
编辑
毫无疑问,您需要在每个平台项目上创建自定义渲染器: 安卓
MyShellRenderer
class
[assembly: ExportRenderer(typeof(App.AppShell), typeof(App.Droid.MyShellRenderer))]
namespace App.Droid
{
public class MyShellRenderer : ShellRenderer
{
public MyShellRenderer(Context context) : base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new MyBottomNavViewAppearanceTracker(this, shellItem);
}
}
}
class MyBottomNavViewAppearanceTracker : ShellBottomNavViewAppearanceTracker
{
public MyBottomNavViewAppearanceTracker(IShellContext shellContext, ShellItem shellItem) : base(shellContext, shellItem)
{
}
public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
bottomView.LayoutParameters.Height = 400;
base.SetAppearance(bottomView, appearance);
}
}
[assembly: ExportRenderer(typeof(App.AppShell), typeof(App.iOS.MyShellRenderer))]
namespace App.iOS
{
public class MyShellRenderer : ShellRenderer
{
protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
{
return new MyCreateTabBarAppearanceTracker();
}
}
}
class CreateTabBarAppearanceTracker : ShellTabBarAppearanceTracker
{
public override void SetAppearance(UITabBarController controller, ShellAppearance appearance)
{
UITabBar tabBar = controller.TabBar;
int tabBarHeight = 100;
if (tabBar.Frame.Height != tabBarHeight)
{
tabBar.Frame = new CGRect(tabBar.Frame.X, tabBar.Frame.Y + (tabBar.Frame.Height - tabBarHeight), tabBar.Frame.Width, tabBarHeight);
}
}
}
MyBottomNavViewAppearanceTracker
class
[assembly: ExportRenderer(typeof(App.AppShell), typeof(App.Droid.MyShellRenderer))]
namespace App.Droid
{
public class MyShellRenderer : ShellRenderer
{
public MyShellRenderer(Context context) : base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new MyBottomNavViewAppearanceTracker(this, shellItem);
}
}
}
class MyBottomNavViewAppearanceTracker : ShellBottomNavViewAppearanceTracker
{
public MyBottomNavViewAppearanceTracker(IShellContext shellContext, ShellItem shellItem) : base(shellContext, shellItem)
{
}
public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
bottomView.LayoutParameters.Height = 400;
base.SetAppearance(bottomView, appearance);
}
}
[assembly: ExportRenderer(typeof(App.AppShell), typeof(App.iOS.MyShellRenderer))]
namespace App.iOS
{
public class MyShellRenderer : ShellRenderer
{
protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
{
return new MyCreateTabBarAppearanceTracker();
}
}
}
class CreateTabBarAppearanceTracker : ShellTabBarAppearanceTracker
{
public override void SetAppearance(UITabBarController controller, ShellAppearance appearance)
{
UITabBar tabBar = controller.TabBar;
int tabBarHeight = 100;
if (tabBar.Frame.Height != tabBarHeight)
{
tabBar.Frame = new CGRect(tabBar.Frame.X, tabBar.Frame.Y + (tabBar.Frame.Height - tabBarHeight), tabBar.Frame.Width, tabBarHeight);
}
}
}
编辑:
iOS的一部分归功于
网间网操作系统
MyShellRenderer
class
[assembly: ExportRenderer(typeof(App.AppShell), typeof(App.Droid.MyShellRenderer))]
namespace App.Droid
{
public class MyShellRenderer : ShellRenderer
{
public MyShellRenderer(Context context) : base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new MyBottomNavViewAppearanceTracker(this, shellItem);
}
}
}
class MyBottomNavViewAppearanceTracker : ShellBottomNavViewAppearanceTracker
{
public MyBottomNavViewAppearanceTracker(IShellContext shellContext, ShellItem shellItem) : base(shellContext, shellItem)
{
}
public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
bottomView.LayoutParameters.Height = 400;
base.SetAppearance(bottomView, appearance);
}
}
[assembly: ExportRenderer(typeof(App.AppShell), typeof(App.iOS.MyShellRenderer))]
namespace App.iOS
{
public class MyShellRenderer : ShellRenderer
{
protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
{
return new MyCreateTabBarAppearanceTracker();
}
}
}
class CreateTabBarAppearanceTracker : ShellTabBarAppearanceTracker
{
public override void SetAppearance(UITabBarController controller, ShellAppearance appearance)
{
UITabBar tabBar = controller.TabBar;
int tabBarHeight = 100;
if (tabBar.Frame.Height != tabBarHeight)
{
tabBar.Frame = new CGRect(tabBar.Frame.X, tabBar.Frame.Y + (tabBar.Frame.Height - tabBarHeight), tabBar.Frame.Width, tabBarHeight);
}
}
}
CreateTabBarAppearanceTracker
class
[assembly: ExportRenderer(typeof(App.AppShell), typeof(App.Droid.MyShellRenderer))]
namespace App.Droid
{
public class MyShellRenderer : ShellRenderer
{
public MyShellRenderer(Context context) : base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new MyBottomNavViewAppearanceTracker(this, shellItem);
}
}
}
class MyBottomNavViewAppearanceTracker : ShellBottomNavViewAppearanceTracker
{
public MyBottomNavViewAppearanceTracker(IShellContext shellContext, ShellItem shellItem) : base(shellContext, shellItem)
{
}
public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
bottomView.LayoutParameters.Height = 400;
base.SetAppearance(bottomView, appearance);
}
}
[assembly: ExportRenderer(typeof(App.AppShell), typeof(App.iOS.MyShellRenderer))]
namespace App.iOS
{
public class MyShellRenderer : ShellRenderer
{
protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
{
return new MyCreateTabBarAppearanceTracker();
}
}
}
class CreateTabBarAppearanceTracker : ShellTabBarAppearanceTracker
{
public override void SetAppearance(UITabBarController controller, ShellAppearance appearance)
{
UITabBar tabBar = controller.TabBar;
int tabBarHeight = 100;
if (tabBar.Frame.Height != tabBarHeight)
{
tabBar.Frame = new CGRect(tabBar.Frame.X, tabBar.Frame.Y + (tabBar.Frame.Height - tabBarHeight), tabBar.Frame.Width, tabBarHeight);
}
}
}
额外费用
您可能缺少了一些可以解释异常大量错误的名称空间
找不到。是否缺少using指令或程序集引用
通常人们不会在答案中包含名称空间(
使用
语句),因为大多数情况下名称空间是显而易见的,并且VisualStudioIntelliSense可以轻松处理
只需将光标放在或悬停在带红色下划线的语句上,就会出现一个小的工具提示
然后单击“Show potential fix”或其中一个键盘快捷键,选择适当的补丁(通常是建议列表中的第一个),在这种情况下,IntelisSense将自动添加所需的名称空间。我找到的每个代码示例都排除了using指令。在我的情况下,我需要下面3个我找不到的。感谢@Cfun的回答,上面的代码可以添加:
using Google.Android.Material.BottomNavigation;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
对于iOS,您需要执行类似的操作,“BottomNavigationView”带红色下划线,您知道为什么吗?它给出了一个错误:namespace not found您必须使用Google.Android.Material.BottomNavigation的using语句
代码>通常由IntelliSense处理。OMFG谢谢!!我不知道为什么每个代码expample都会排除using指令……因为大多数情况下,当单击“ctrl+”或“ctrl+enter”时,名称空间是显而易见的,并且由visual studio intlisense处理当光标位于未定义的语句上时,IntelisSense将自动建议并添加所需的名称空间。仅供参考:当有一个带红色下划线的名称,并且您将鼠标悬停在该名称上时,您应该会看到一个带有选项的图标来更正该名称空间。其中一个选项是添加所需的using指令。这就是为什么人们通常不会在一个小代码段中“使用”s的原因。