模板选择操作

问题描述 投票:0回答:1

我有一些模板并在

CollectionView
中使用他。

在 MAUI 中,使用

BackgroundColor
可以轻松更改根元素属性,例如
VisualState
。对于模板中的子元素来说就比较麻烦了,必须在模板中创建属性和
BindableProperty
并绑定到子元素的属性上,虽然不是很方便,但也不难。

但是,如果我计划做一些更复杂的事情,比如使用

FadeTo
或播放声音,我该怎么办?我想在
Expand
模板中使用
Collapse
.xaml.cs
方法。

VisualStateManager
不会通知
VisualElement
样式已应用到它,并且没有回调。
CollectionView
不会传递在其事件中显示数据的
VisualElement
。显然,开发人员不希望能够知道
VisualElement
的选定状态。那么应该如何实现呢?

c# .net xaml maui
1个回答
0
投票

不要为每个属性创建单独的属性,而是创建一个“Style”类型的 BindableProperty,然后使用它。

public static readonly BindableProperty MyStyleProperty =
    BindableProperty.Create(nameof(MyStyle), typeof(Style), typeof(YourClass));

public Style MyStyle
{
    get { return (Style)GetValue(MyStyleProperty); }
    set { SetValue(MyStyleProperty, value); }
}
<Button Style="{Binding MyStyle}" Text="My Button"/>

在这种情况下,这非常有用,因为您可以为按钮声明一个样式,该样式涵盖所有状态,如理想/悬停/按下,并将其分配给此 BindableProperty,您将获得其上的所有样式。

您还可以在父组件中定义多种样式,并有条件地设置 MyStyle 属性。例如,普通按钮的样式与“危险”按钮的样式。

© www.soinside.com 2019 - 2024. All rights reserved.