0

我正在开发一个包含4 个选项卡的Xamarin.Forms.Shell 应用程序。我想在所有这些页面上显示一个包含居中 SVG 徽标的公共。 Title

为此,我可以使用TitleView这样的:

<Shell.TitleView>
    <ffimageloadingsvg:SvgCachedImage Source="resource://ShellAppSample.Resources.logoTitle.svg"
                                      DownsampleHeight="6"
                                      HeightRequest="45"/>
</Shell.TitleView>

这很有效,并且徽标很好地位于NavigationBar. 但是,如果页面包含ToolbarItems,则徽标不再居中。

例如,在一个页面上我有一个ToolBarItem这样的:

<ContentPage.ToolbarItems>
    <ToolbarItem Text="Refresh"
                 Order="Primary"
                 Priority="0"
                 Command="{Binding RefreshCommand}"
                 CommandParameter="{x:Reference webView}">                             
        <ToolbarItem.IconImageSource>
            <FontImageSource Glyph="{StaticResource FalIconRefresh}"
                             FontFamily="FontAwesomeLight"
                             Size="Medium"/>
        </ToolbarItem.IconImageSource>
    </ToolbarItem>
</ContentPage.ToolbarItems>

是否有另一种方法可以通过使用或计算 SVG 的大小来将SVG 中的居中图像显示为 TitleCustomRendererToolbarItems

我看到一年前已经发布了一个类似的问题,但没有任何解决方案......

我已经尝试过ToolbarItemTitleView这样重绘:

<Shell.TitleView>
    <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
                 BackgroundColor="Transparent">
        <ffimageloadingsvg:SvgCachedImage Source="resource://ShellAppSample.Resources.blackLogoTitle.svg"
                                          DownsampleHeight="6"
                                          HorizontalOptions="CenterAndExpand"
                                          HeightRequest="45"
                                          BackgroundColor="Transparent"/>
        <Image HorizontalOptions="End" VerticalOptions="CenterAndExpand"
               BackgroundColor="Transparent">
            <Image.Source>
                <FontImageSource Glyph="{StaticResource FalIconRefresh}"
                                 FontFamily="FontAwesomeLight"
                                 Size="Medium"/>
            </Image.Source>
            <Image.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding RefreshCommand}" CommandParameter="{x:Reference webView}"/>
            </Image.GestureRecognizers>
        </Image>
    </Grid>
</Shell.TitleView>

在 iOS 上是正确的,但我可以看到:

  • 徽标不像不包含任何 的页面那样完全居中ToolBarItem,因为当我在页面之间切换时有一个小间隙
  • 图标的位置不像我使用ToolBarItem

在 Android 上,结果不正确:徽标的高度不同,并且图标不在TitleView.

4

1 回答 1

2

最后我TitleView像这样管理我的s:

对于Image

<Shell.TitleView >
    <Image Source="resource://ShellAppSample.Resources.logo.jpg"
          VerticalOptions="CenterAndExpand" HorizontalOptions="Center"
          Margin="{OnPlatform Android='0,0,16,0', Default=0}"/>
</Shell.TitleView>

对于Label

<Shell.TitleView>
    <Label Text="{Binding Title}"
           VerticalOptions="CenterAndExpand" HorizontalOptions="Center"
           Margin="{OnPlatform Android='0,0,16,0', Default=0}"/>
</Shell.TitleView>

对于 Label一个“中等”尺寸ToolBarItem

<Shell.TitleView>
    <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="40" /> 
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Label Text="{Binding Title}"
               Grid.Column="1"
               VerticalOptions="CenterAndExpand" HorizontalOptions="Center"
               Margin="{OnPlatform Android='0,0,16,0', Default=0}"/>
    </Grid>
</Shell.TitleView>
<ContentPage.ToolbarItems>
    <ToolbarItem Text="Refresh"
                 Order="Primary"
                 Priority="1"
                 Command="{Binding RefreshCommand}">                             
        <ToolbarItem.IconImageSource>
            <FontImageSource Glyph="{StaticResource FalIconRefresh}"
                             FontFamily="FontAwesomeLight"
                             Size="Medium"/>
        </ToolbarItem.IconImageSource>
    </ToolbarItem>
</ContentPage.ToolbarItems>

我已经通过DebugRainbows包检查了对齐情况,它在 iOS/Android 和小型/大型设备上看起来不错。

它并不完美,如果我添加其他s,我需要更新规则,ToolBarItem但它的工作原理是这样的。

于 2020-11-20T09:15:36.387 回答