首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin.Forms:当ToolbarItems显示时,如何在TitleView中对图像进行居中?

Xamarin.Forms:当ToolbarItems显示时,如何在TitleView中对图像进行居中?
EN

Stack Overflow用户
提问于 2020-11-10 11:22:49
回答 1查看 817关注 0票数 0

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

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

代码语言:javascript
复制
<Shell.TitleView>
    <ffimageloadingsvg:SvgCachedImage Source="resource://ShellAppSample.Resources.logoTitle.svg"
                                      DownsampleHeight="6"
                                      HeightRequest="45"/>
</Shell.TitleView>

这是工作和标志是很好的中心在NavigationBar。但是,如果页面包含ToolbarItems,则徽标不再以中心为中心。

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

代码语言:javascript
复制
<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>

是否有另一种方法通过使用或计算ToolbarItems的大小从SVG显示以CustomRenderer为标题的图像

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

我已经尝试过像这样在ToolbarItem中重新绘制TitleView

代码语言:javascript
复制
<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时那样定位ToolBarItem

在安卓系统上,结果是不正确的:徽标的高度不一样,图标不在TitleView

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-20 09:15:36

最后,我像这样管理了我的TitleView

对于Image

代码语言:javascript
复制
<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

代码语言:javascript
复制
<Shell.TitleView>
    <Label Text="{Binding Title}"
           VerticalOptions="CenterAndExpand" HorizontalOptions="Center"
           Margin="{OnPlatform Android='0,0,16,0', Default=0}"/>
</Shell.TitleView>

Label的一个“中等”大小的ToolBarItem

代码语言:javascript
复制
<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上,在小型/大型设备上,它看起来都很好。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64767959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档