0

我正在使用 Xamarin.Forms 4.0,其外壳左侧有一个弹出按钮。我试图在 Shell.TitleView 的中心显示一个图像,但是,我不知道如何正确实现这一点。结果就像官方页面中建议的那样,图像并不是真正位于中心(但有点在右侧)。这可能是因为左侧的汉堡菜单图标,也许(?)

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/configuration#display-views-in-the-navigation-bar

我试图在视图渲染后计算汉堡菜单图标的宽度并在图像的边缘进行补偿,但由于没有页面加载事件,此时我只能在构造函数中硬编码延迟内容页面。但这被认为是相当肮脏的,当右侧有图标时会出现问题。

public SomeContentPage()
{
    InitializeComponent();
    Device.StartTimer(new System.TimeSpan(0, 0, 0, 0, 500), () =>
            {
                var titleViewBound = Shell.GetTitleView(this).Bounds;
                DisplayInfo displayInfo = DeviceDisplay.MainDisplayInfo;
                double leftButtonWidth = displayInfo.Width / displayInfo.Density - titleViewBound.Width;
                TitleImage.Margin = new Thickness(0, 0, leftButtonWidth, 0);
                return false;
            });
}

我希望无论右侧有汉堡菜单图标或其他图标,都会有更好的方法在 TitleView 中居中图像。提前致谢!

---添加了参考样本---

我给这个应用贴上了白标签,并把它放在 Github 上供大家参考。谢谢!参考示例:https ://github.com/ltin071/XFShellFlyoutSample

TitleImage 不在中心,直到按下下方的调整按钮(执行上述计算)。我希望我错过了一些实现这一目标的标准方法。

4

0 回答 0