gpt4 book ai didi

xamarin - 为 TabbedPage 重用 CustomRender

转载 作者:行者123 更新时间:2023-12-02 16:29:29 29 4
gpt4 key购买 nike

我的 Xamarin Forms 项目有一个选项卡式页面。选项卡过去常常滑动,但我已经实现了 CustomRender 来阻止这种情况。

这非常有效,除非我需要将页面重新添加到选项卡栏。我似乎无法重复使用自定义渲染?

现在我正在将页面重新添加到 CustomRendered TabbedPage,如下所示:

var masterTabPage = this.Parent.Parent as NoSlideTabbedPage;

Xamarin.Forms.NavigationPage Page1 = new Xamarin.Forms.NavigationPage(new Page1("example string"));
Page1.Title = "Page Example";
Page1.Icon = "pageexample.png";

masterTabPage.Children.RemoveAt(1);
masterTabPage.Children.Insert(1, Page1);

masterTabPage.CurrentPage = masterTabPage.Children[1];

我的布局页面看起来像

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Layout : NoSlideTabbedPage
{
public Layout()
{
InitializeComponent();

On<Android>().SetBarItemColor(Color.FromHex("#cccccc"));
On<Android>().SetBarSelectedItemColor(Color.FromHex("#123456"));

// SET NAV BAR TO BOTTOM
On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);

// CREATE TABS
NavigationPage Page0 = new NavigationPage(new Page0());
Page0.Title = "Page0";
Page0.Icon = "Page0.png";

NavigationPage Page1 = new NavigationPage(new Page1());
Page1.Title = "Page1";
Page1.Icon = "Page1.png";

NavigationPage Page2 = new NavigationPage(new Page2());
Page2.Title = "Page2";
Page2.Icon = "Page2.png";

NavigationPage Page3 = new NavigationPage(new Page3());
Page3.Title = "Page3";
Page3.Icon = "Page3.png";

// ADD TABS
Children.Add(Page0);
Children.Add(Page1);
Children.Add(Page2);
Children.Add(Page3);
}
}

自定义渲染器代码:

public class NoSlideTabbedPageRenderer : TabbedPageRenderer
{
private bool _isShiftModeSet;

public NoSlideTabbedPageRenderer(Context context)
: base(context)
{

}

protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
try
{
if (!_isShiftModeSet)
{
var children = GetAllChildViews(ViewGroup);

if (children.SingleOrDefault(x => x is BottomNavigationView) is BottomNavigationView bottomNav)
{
bottomNav.SetShiftMode(false, false);
_isShiftModeSet = true;
}
}
}
catch (Exception e)
{
Console.WriteLine($"Error setting ShiftMode: {e}");
}
}

private List<View> GetAllChildViews(View view)
{
if (!(view is ViewGroup group))
{
return new List<View> { view };
}

var result = new List<View>();

for (int i = 0; i < group.ChildCount; i++)
{
var child = group.GetChildAt(i);

var childList = new List<View> { child };
childList.AddRange(GetAllChildViews(child));

result.AddRange(childList);
}

return result.Distinct().ToList();
}
}

布局页面可以正常工作,因为我正在使用上面的 CustomRender。这样就可以让标签栏正常了。但是,当我删除并重新添加页面(显示在第一个代码块中)时,CustomRender 将停止使用。如何在重新添加此 Page1 的同时继续使用此自定义渲染?

最佳答案

您可以监听元素更改的属性并再次调用shiftmode。

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

if(e.NewElement !=null)
{
var children = GetAllChildViews(ViewGroup);

if (children.SingleOrDefault(x => x is BottomNavigationView) is BottomNavigationView bottomNav)
{
bottomNav.SetShiftMode(false, false);
_isShiftModeSet = true;
}
}
}

关于xamarin - 为 TabbedPage 重用 CustomRender,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54324842/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com