Shell.TitleView 无法通过 Xamarin.Forms 应用程序在 iPhone 上运行

问题描述 投票:0回答:1

以下代码在使用 Visual Studio 2022 社区版中的 Xamarin.Forms 开发的 Android 设备上正常运行, 但同样的代码在 iPhone 上不起作用,

<Shell.TitleView>
        <Grid Margin="0,0,10,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>

            <Label x:Name="PageTitle" Grid.Row="0" Grid.Column="0" Text="" VerticalOptions="CenterAndExpand" FontSize="Medium" FontAttributes="Bold" TextColor="Black"/>

            <Image Grid.Row="0" Grid.Column="1" VerticalOptions="CenterAndExpand" HorizontalOptions="End" Source="cart.png" WidthRequest="40" HeightRequest="40">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="Cart_Clicked"/>
                </Image.GestureRecognizers>
            </Image>
            <Frame x:Name="BadgeFrame" Grid.Row="0" Grid.Column="1" Padding="0" WidthRequest="25" HeightRequest="25" CornerRadius="13" HasShadow="false" BackgroundColor="Red" HorizontalOptions="End" VerticalOptions="Start" >
                <Frame.GestureRecognizers>
                    <TapGestureRecognizer Tapped="Cart_Clicked"/>
                </Frame.GestureRecognizers>
                <Label  x:Name="BadgeLabel" Text="" TextColor="White" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" HeightRequest="20"/>
            </Frame>
        </Grid>
    </Shell.TitleView>

请提出我们做错了什么,指导我们在 iPhone 上实现此功能。

谢谢你,

我们希望对 Android 和 iPhone 使用相同的代码。 请建议是否对给定代码进行任何更改或需要设置任何 ios 项目属性。

因此相同的代码也适用于 iPhone。

xamarin.forms
1个回答
0
投票

您可能遇到了一个已知的 GitHub 问题:Shell TitleView 在 iOS 16 中无法工作 [Bug] #15512

我可以完全重现这个问题。社区给出了一个解决方法,即使用 Xamarin.Forms Shell 自定义渲染器

@adam-russell 与我们分享了一个使用自定义渲染器的好解决方法,我已经在我这边进行了测试。

总而言之,假设您创建了一个名为

AppShell
的 XAML 文件,该文件对共享代码项目中的 Shell 类进行子类化。

然后在iOS项目中,创建一个名为

CustomShellRenderer
的新文件,它是
ShellRenderer
的子类。您可以只使用@adam-russell comment中的代码。可能会有一些差异
ExportRendererAttribute

[assembly: ExportRenderer(typeof(AppShell), typeof(CustomShellRenderer))]

namespace Your.Namespace.Here
{
    public class CustomShellRenderer : ShellRenderer
    {
        protected override IShellPageRendererTracker CreatePageRendererTracker()
        {
            return new CustomShellPageRendererTracker(this);       
        }
    }
    ...

}

然后你可以添加TitleView并检查它,

<Shell.TitleView>
    <StackLayout>
        <Label TextColor="Orange"
        FontSize="Large"
        FontAttributes="Bold"
        HorizontalTextAlignment="Center"
        VerticalTextAlignment="Center"
        Text="test 2 - 2 test 2 curious"/>
    </StackLayout>
</Shell.TitleView>

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.