4

在 Xamarin.Forms 中组合TabbedPage时,如何让 UWP 使用页面的Icon属性?

如果我正确配置我的表单属性/文件, UWP 似乎可以很好地支持这一点。

这是我的 TabbedPage XAML。这些图标都已设置并适用于 iOS 和 Android,甚至 UWP 中的页面图像也可以正常呈现(这意味着文件可能正确地存在于项目中)。

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:tabbed"
             x:Class="tabbed.MainPage">
    <TabbedPage.Children>
        <local:InitialPage Title="Tab1" Icon="star.png" />
        <ContentPage Title="Tab2" Icon="gear.png">
            <ContentPage.Content>
                <StackLayout>
                    <Label Text="A nice label." />
                    <Image Source="star.png" /><!-- works here -->
                </StackLayout>
            </ContentPage.Content>
        </ContentPage>
    </TabbedPage.Children>
</TabbedPage>
4

2 回答 2

4

我在这里概述了这是如何实现的http://depblog.weblogs.us/2017/07/12/xamarin-forms-tabbed-page-uwp-with-images/

简而言之,您需要更改HeaderTemplateUWP 使用的默认值。但由于 Xamarin 表单的启动方式,这并不简单。因此,您需要将自定义模板注入到资源字典中。

示例项目在 Github 上https://github.com/Depechie/XamarinFormsTabbedPageUWPWithIcons

更长的细节:

您需要提供自己的TabbedPageStyle并切换出 Xamarin 用于其 UWP 渲染的那个。因此,新样式包含一个 Image,我们在其中将 Source 数据绑定到 Xamarin Icon 属性。

<Style x:Key="TabbedPageStyle2" TargetType="uwp:FormsPivot">
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <Image Source="{Binding Icon, Converter={StaticResource IconConverter}}" Width="15" Height="15" />
                    <TextBlock Name="TabbedPageHeaderTextBlock" Text="{Binding Title}"
                                Style="{ThemeResource BodyTextBlockStyle}" />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

实际的风格切换是这样完成App.Xaml.cs

((Style)this.Resources["TabbedPageStyle"]).Setters[0] = ((Style)this.Resources["TabbedPageStyle2"]).Setters[0];

您还需要一个转换器来确保图像控件理解 Xamarin 提供的图标源

public class IconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value != null && value is Xamarin.Forms.FileImageSource)
            return ((Xamarin.Forms.FileImageSource)value).File;

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
于 2017-11-22T07:14:48.090 回答
3

目前,UWP TabbedPage 渲染器根本不使用 Icon 属性,因此获取选项卡图标将需要自定义渲染器。即使是官方的 UWP 示例实际上似乎也没有内置此功能,需要自定义 UserControl

Android TabbedPageRendereriOS TabbedRenderer甚至macOS TabbedPageRenderer使用 Icon 属性来调整选项卡 UI,但 UWP 渲染器需要更新才能使其工作。

于 2017-11-21T19:42:15.300 回答