Xamarin.Forms 如何在 VisualState 中使用 Xamarin Community Toolkit TouchEffect

问题描述

我正在使用 VisualState 在我的 XAML 中实现一个条件,但我不知道如何在 VisualState 中更改诸如 Xamarin Community Toolkit TouchEffect 之类的效果属性,有什么建议吗?

        <StackLayout HorizontalOptions="FillAndExpand"
                     VerticalOptions="FillAndExpand"
                     BackgroundColor="Red"
                     HeightRequest="200">
        <visualstatemanager.VisualStateGroups>
           <VisualStateGroup>
               <VisualState x:Name="test">
                   <VisualState.StateTriggers>
                      <CompareStateTrigger Property="{Binding sampleBool}" Value="true" />
                    </VisualState.StateTriggers>
              <VisualState.Setters>
<!--Here in the Property there should be some way to refer to the Command property of the TouchEffect-->
                <Setter Property="xct:TouchEffect.Command" Value="{Binding sampleCommand}" />
             </VisualState.Setters>
            </VisualState>
         </VisualStateGroup>
    </visualstatemanager.VisualStateGroups>
    </StackLayout>

我试过以这种方式引用,但它引发了以下错误:x:Static:无法找到公共或可访问的内部静态字段、静态属性、常量或枚举值“xct:TouchEffect”。

<Setter Property="{x:Static xct:TouchEffect.Command}" Value="{Binding sampleCommand}" />

我也尝试过使用 Type,但出现此错误:无法解析类型“TouchEffect.Command”

<Setter Property="{x:Type xct:TouchEffect.Command}" Value="{Binding sampleCommand}" />

解决方法

这是正确的做法,我的问题是因为其他原因,我将代码放在 DataTemplate 中,并且必须引用页面视图模型。

在正常情况下,这会起作用:

<Setter Property="xct:TouchEffect.Command" Value="{Binding sampleCommand}" />

在我的情况下,在 DataTemplate 中:

<Setter Property="xct:TouchEffect.Command" Value="{Binding Source={x:Reference SamplePage},Path=BindingContext.sampleCommand}" />