4

I am implementing a condition in my XAML using VisualState, but I don't know how I can change a property of an effect like the Xamarin Community Toolkit TouchEffect in a VisualState, any suggestions?

        <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>

I've tried to reference this way, but it throws me the following error: x:Static: unable to find a public -- or accessible internal -- static field, static property, const or enum value named "Command" in "xct:TouchEffect".

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

I have also tried to use Type but I get this error: Cannot resolve type "TouchEffect.Command"

<Setter Property="{x:Type xct:TouchEffect.Command}" Value="{Binding sampleCommand}" />
Tecnopresley
  • 73
  • 1
  • 4
  • 14
  • I think you need to create a toucheffect (add it to your XAML), give it a name in XAML, and then refer somehow to that name. Sorry, that's vague, but maybe give you a clue. Is there a link to an example that you are following, that uses toucheffect? – ToolmakerSteve May 14 '21 at 17:18
  • 1
    Hi @ToolmakerSteve , I am using this repository as a reference since it is the one that was merged with XamarinCommunityToolkit and has quite a few examples, although I cannot find clues or documentation on how to refer to these effects in a VisualState: https://github.com/AndreiMisiukevich/TouchEffect – Tecnopresley May 14 '21 at 18:57
  • OK, that helps. Are you able to set initial property values when declaring the element that has TouchEffect attached? Similar to TouchEffectSample/TogglePage.xaml lines 22-46, specifically lines 31-41? Then should be similar Setter in VisualState. Have you tried simply ` – ToolmakerSteve May 14 '21 at 19:21
  • 1
    @ToolmakerSteve I also tried that before but XAML shows me the following error: Type 'xct:TouchEffect' is used like a markup extension but does not derive from MarkupExtension. – Tecnopresley May 14 '21 at 19:26
  • Have you verified that `xct:TouchEffect` allows you to set property values correctly, when **not** inside a VisualStateManager? As I mentioned, setting initial property values when declaring. – ToolmakerSteve May 14 '21 at 19:30
  • 1
    @ToolmakerSteve Yes, it allows setting values when it is used only in the control. It is very strange that there is no trivial way to reference the property of an effect in a VisualState or in a Trigger. – Tecnopresley May 14 '21 at 19:36
  • Yes, it is strange. It should be an `attached property`, so should be just like this [answer to a related question](https://stackoverflow.com/a/60274997/199364), which is the simple syntax that gave error "used like a markup extension". I think would have a corresponding static, so the `statix` syntax should have worked too, AFAIK. It shouldn't make a difference, but does it work if instead of `StackLayout`, you apply to some simple non-layout control like `label`? – ToolmakerSteve May 14 '21 at 20:32
  • @ToolmakerSteve It also doesn't work on a label – Tecnopresley May 15 '21 at 11:21
  • Maybe add a test button with a breakpoint, examine visual tree after page has been drawn. See what those properties turns into. Might give idea how to refer to it. If can't refer to it in XAML, then maybe write code behind that finds the property? Maybe [add visual state manager in code behind](https://stackoverflow.com/a/34065056/199364). – ToolmakerSteve May 17 '21 at 19:33

1 Answers1

1

This was the correct way to do it, my problem was because of something else, I had the code inside a DataTemplate and had to reference the page view model.

In a normal case this will work:

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

In my case inside a DataTemplate:

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