简体   繁体   English

Xamarin Forms 选项卡式页面触发器属性不起作用

[英]Xamarin Forms Tabbed Page Trigger properties not working

I'm trying to follow this sample by using font awesome as icon for the tabbed page and use the triggers to change font color but when I want to apply the style using font awesome I got an error that Property="IsChecked" does not exist for the target.我正在尝试通过使用 font awesome 作为选项卡式页面的图标并使用触发器来更改字体颜色来遵循此示例,但是当我想使用 font awesome 应用样式时,我收到一个错误,即 Property="IsChecked" 不存在为目标。 I noticed in the sample they are using shells how could I replicate that example using regular tabbed pages?我在示例中注意到他们正在使用 shell,我如何使用常规选项卡式页面复制该示例?

on the app.XAML I have the following在 app.XAML 我有以下

<OnPlatform x:TypeArguments="x:String" 
            x:Key="FontAwesomeSolid">
            <On Platform="Android" 
            Value="Font5Solid.otf#Regular" />
            <On Platform="iOS" 
            Value="FontAwesome5Free-Solid" />
         </OnPlatform>
         <OnPlatform x:TypeArguments="x:String" 
            x:Key="FontAwesomeRegular">
            <On Platform="Android" 
            Value="Font5Regular.otf#Regular" />
            <On Platform="iOS" 
             Value="FontAwesome5Free-Regular" />
          </OnPlatform>

For the tabbed page I have the following对于标签页,我有以下内容

<?xml version="1.0" encoding="utf-8"?>
<TabbedPage x:Name="Tab" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="pages.Views.HomeTabbedPage">
    <!--Pages can be added as references or inline-->

    <TabbedPage.Resources>
          <ResourceDictionary>
     <Style TargetType="Tab" x:Key="FollowTab">
                <Style.Triggers>
                <Trigger TargetType="TabbedPage"
                         Property="IsChecked" Value="False">
                    <Setter Property="Icon" >
                        <Setter.Value>
                            <FontImageSource FontFamily="{StaticResource FontAwesomeRegular}" Glyph="&#xf004;"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
                <Trigger TargetType="Tab" 
                         Property="IsChecked" Value="True">
                    <Setter Property="Icon" >
                        <Setter.Value>
                            <FontImageSource FontFamily="{StaticResource FontAwesomeSolid}" Glyph="&#xf004;"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
                </Style.Triggers>
            </Style>
  </ResourceDictionary>
    </TabbedPage.Resources>

    <ContentPage Title="sample page"  />
    
</TabbedPage>

To replicate that in a normal TabbedPage you can try these steps:要在普通的TabbedPage中复制它,您可以尝试以下步骤:

1 - Create a converter to check TabbedPage.CurrentPage type: 1 - 创建一个转换器来检查TabbedPage.CurrentPage类型:

public class SelectedTabTypeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return null;

        if (!(value is Page))
            throw new ArgumentException("Expected value to be of type " + nameof(Page), nameof(value));

        // if value is a NavigationPage check against its RootPage
        if (value is NavigationPage navPage)
            return navPage.RootPage?.GetType();

        return value.GetType();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        => throw new NotImplementedException();
}

2 - Add the x:Name attribute to the TabbedPage so we can reference it latter: 2 - 将x:Name属性添加到 TabbedPage 以便我们稍后可以引用它:

<TabbedPage x:Name="MyTabbedPage" ...

3 - Add the converter resource to TabbedPage.Resources : 3 - 将转换器资源添加到TabbedPage.Resources

  <TabbedPage.Resources>
    <ResourceDictionary>
      <converters:SelectedTabTypeConverter x:Key="SelectedTabTypeConverter" />
    </ResourceDictionary>
  </TabbedPage.Resources>

4 - Add the pages and the DataTriggers : 4 - 添加页面和DataTriggers

  <TabbedPage.Children>
    <!-- Normal page tab default values -->
    <views:OnePage Title="Page1">
      <views:OnePage.Triggers>
        <DataTrigger 
            TargetType="views:OnePage"
            Binding="{Binding Source={x:Reference MyTabbedPage}, Path=CurrentPage, Converter={StaticResource SelectedTabTypeConverter}}" 
            Value="{x:Type views:OnePage}">
          <Setter Property="Title" Value="Page1 Selected" />
        </DataTrigger>
      </views:OnePage.Triggers>
    </views:OnePage>

    <!-- NavigationPage tab -->
    <NavigationPage Title="Page2">
      <x:Arguments>
        <views:TwoPage />
      </x:Arguments>
      <NavigationPage.Triggers>
        <DataTrigger 
            TargetType="NavigationPage" 
            Binding="{Binding Source={x:Reference MyTabbedPage}, Path=CurrentPage, Converter={StaticResource SelectedTabTypeConverter}}" 
            Value="{x:Type views:TwoPage}">
          <Setter Property="Title" Value="Page2 Selected" />
        </DataTrigger>
      </NavigationPage.Triggers>
    </NavigationPage>
  </TabbedPage.Children>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM