在Xamarin.Forms和MAUI中使用C#设置VisualState触发器。

huangapple go评论70阅读模式
英文:

Setting VisualState Triggers in C# in Xamarin.Forms and MAUI

问题

/* 这是我的工作 XAML 代码,我想在 C# 中复制它: */

/* 这是我在 C# 中设置 VisualState 的方式: */

/* 我不知道如何为上述情景设置触发器。 
   似乎触发器应该绑定到包含的 CollectionView 的选择更改事件,
   但我不知道如何访问它。 */
英文:

For reasons not relevant here I need to set VisualState in C# rather than XAML.
However, I don't know how to change VisualState in response to a CollectionView's Selection change.

Here's the working XAML code which I'm trying to replicate in C#:

<CollectionView SelectionMode="Multiple" 
                ItemsSource="{Binding ViewModel_Contacts}"
                SelectionChanged="OnSelectionChanged">
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="sharedModels:Showuser">
            <StackLayout Orientation="Horizontal" HeightRequest="40">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal">
                            <VisualState.Setters>
                                <Setter TargetName="LabelMark" Property="Label.TextColor" 
                                        Value="Blue"/>
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter TargetName="LabelMark" Property="Label.TextColor" 
                                        Value="Red"/>
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <Label x:Name="LabelMark"/>
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

The way I have VisualState set in C# is as follows:

VisualStateManager.GetVisualStateGroups(LabelMark).Add(
    new VisualStateGroup() {
        Name= "CommonStates",
        States = {
            new VisualState {
                Name = "Normal",
                Setters = {
                    new Setter {
                        Property = Label.TextColorProperty,
                        Value = Colors.Blue 
                    }
                },
                StateTriggers= {
                    new StateTrigger {      
                        /* THIS IS THE QUESTION:
                        HOW DO I SET IT UP? */
                    }
                }
            },
            new VisualState {
                Name = "Selected",
                Setters = {
                    new Setter {
                        Property = Label.TextColorProperty,
                        Value = Colors.Red
                    }
                },
                StateTriggers= {
                    new StateTrigger {      
                        /* THIS IS THE QUESTION:
                        HOW DO I SET IT UP? */
                    }
                }
            }
        }
    }
);

I don't know how to set up trigger(s) for the above scenario.
It seems obvious that the trigger should be bound to the containing CollectionView's selection changed event, but I don't know how to get to it.

答案1

得分: 1

以下是翻译好的部分:

<s>Looking at the Microsoft docs for [StateTrigger][1] and there is only one property that you can set. It's `IsActive` and indicates whether a trigger should be applied. So `StateTrigger` would look like this.

new StateTrigger {
IsActive= true
}


If you are looking to bound your `VisualState` to a specific target, you should change your `Setters`. From the Microsoft docs for [Setter Class][2]. There is a property called `Target` so that you can target that element and apply the `Value` to it.

It looks like this what done on the XAML side as well, but with the property `TargetName` which does not exist.

Hope this helps.</s>
 <hr/>

Sorry I did look at the wrong Microsoft Docs. I went back and looked at the correct Docs as well as some examples from GitHub. Unfortunately the `StateTrigger` was only referenced in the XAML code. 

This is the code that I was looking at if you are interested. 
[XAML][3]
[ViewModel][4]

That being said I think I managed to work out how its done from trying it out in Visual Studio. You can connect the `StateTrigger` via a `BindingContext` to your `ViewModel`. In your case I'm pretty sure in your case it would be this.

new StateTrigger {
BindingContext = new ViewModel_Contacts()
}

Hope that helps for real this time. 

  [1]: https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.statetrigger?view=winrt-22621
  [2]: https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.setter?view=winrt-22621
  [3]: https://github.com/dotnet/maui-samples/blob/768d9d0458dfc253fe436788afe31b9d46a198f4/6.0/Fundamentals/TriggersDemos/WorkingWithTriggers/Views/StateTriggerDemoPage.xaml
  [4]: https://github.com/dotnet/maui-samples/blob/768d9d0458dfc253fe436788afe31b9d46a198f4/6.0/Fundamentals/TriggersDemos/WorkingWithTriggers/ViewModels/TriggerViewModel.cs

希望对你有所帮助。

英文:

<s>Looking at the Microsoft docs for StateTrigger and there is only one property that you can set. It's IsActive and indicates whether a trigger should be applied. So StateTrigger would look like this.

new StateTrigger {      
    IsActive= true
}

If you are looking to bound your VisualState to a specific target, you should change your Setters. From the Microsoft docs for Setter Class. There is a property called Target so that you can target that element and apply the Value to it.

It looks like this what done on the XAML side as well, but with the property TargetName which does not exist.

Hope this helps.</s>
<hr/>

Sorry I did look at the wrong Microsoft Docs. I went back and looked at the correct Docs as well as some examples from GitHub. Unfortunately the StateTrigger was only referenced in the XAML code.

This is the code that I was looking at if you are interested.
XAML
ViewModel

That being said I think I managed to work out how its done from trying it out in Visual Studio. You can connect the StateTrigger via a BindingContext to your ViewModel. In your case I'm pretty sure in your case it would be this.

new StateTrigger {      
    BindingContext = new ViewModel_Contacts()
}

Hope that helps for real this time.

huangapple
  • 本文由 发表于 2023年2月7日 03:59:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定