【问题标题】:Xamarin.Forms how use Xamarin Community Toolkit TouchEffect in VisualStateXamarin.Forms 如何在 VisualState 中使用 Xamarin Community Toolkit TouchEffect
【发布时间】:2021-08-04 13:49:22
【问题描述】:

我正在使用 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: 无法找到名为“Command”的公共或可访问的内部静态字段、静态属性、常量或枚举值“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}" />

【问题讨论】:

  • 我认为您需要创建一个触摸效果(将其添加到您的 XAML),在 XAML 中为其命名,然后以某种方式引用该名称。对不起,这很模糊,但也许能给你一个线索。是否有指向您正在关注的使用触摸效果的示例的链接?
  • 嗨@ToolmakerSteve,我使用此存储库作为参考,因为它是与 XamarinCommunityToolkit 合并的存储库并且有很多示例,尽管我找不到有关如何引用这些的线索或文档VisualState 中的效果:github.com/AndreiMisiukevich/TouchEffect
  • 好的,这有帮助。在声明附加了 TouchEffect 的元素时,是否可以设置初始属性值?类似于 TouchEffectSample/TogglePage.xaml 第 22-46 行,特别是第 31-41 行?那么在VisualState中应该是类似的Setter。您是否尝试过简单的 &lt;Setter Property="xct:TouchEffect.Command" Value=... ?警告:我没有尝试过类似的代码。将此语法视为类似于使用 Grid.Column 作为网格内元素的属性。
  • @ToolmakerSteve 我之前也尝试过,但 XAML 向我显示以下错误:Type 'xct:TouchEffect' is used as a markup extension but does not derived from MarkupExtension.
  • @ToolmakerSteve 是的,它允许在仅在控件中使用时设置值。很奇怪,在 VisualState 或 Trigger 中没有简单的方法来引用效果的属性。

标签: xaml xamarin.forms xamarin-community-toolkit


【解决方案1】:

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

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

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

在我的情况下,在 DataTemplate 中:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多