[UWP] [XAML]便携式更改控件在事件上的外观 [英] [UWP][XAML] Portable changing control appearance on event
问题描述
我开始编写新的UWP应用程序,但无法找到满足以下所有条件的解决方案:
I am starting to write a new UWP app, and am unable to find a solution that does all of the following:
- 更改控件特定事件的样式(更改背景颜色:鼠标悬停,聚焦,按下等等)。
- 上述内容仅适用于指定的控件。例如,我有两种类型的按钮,我希望鼠标在一种类型上看起来不同,而另一种不同。
- 不重复我想要的每个控件实例的样式代码外观发生了变化。
所有这些都可以使用WPF Style.Triggers配置来完成。但是,UWP似乎不支持触发器。
All these can be done using the WPF Style.Triggers configuration. However, Triggers don't appear to be supported in UWP.
做我需要的最好的方法是什么?我已经探索了以下内容:
What is the best way to do what I need? I've explored the following:
- 轻量级风格。这打破了要求#2。 (https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/xaml-styles#lightweight-styling)
- 控制模板。我承认,我对这个话题没有很好的理解,但看起来非常复杂。为了达到我想要的目的,我需要完全重写我想要这种行为的所有控件的模板。
(https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/control-templates) - 视觉状态。这打破了要求#3,因为可视状态代码需要针对XAML中的特定目标。我正在寻找一种可移植的方法,其中样式可以应用于多个项目。 (https://docs.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.VisualStateManager)
请告诉我实现所有3项要求的最佳方法是什么。绝对是我对UWP和XAML的有限理解让我忽略了上述主题中的重要细节,因此非常感谢更正:)
Please let me know what the best way of achieving all 3 requirements are. It can absolutely be the case where my limited understanding in UWP and XAML has me overlooking important details in the above topics, so corrections are much appreciated :)
温暖的问候,
Sean
推荐答案
Sean,
>>所有这些都可以使用WPF Style.Triggers配置来完成。但是,UWP似乎不支持触发器。什么是我需要的最佳方式?
>> All these can be done using the WPF Style.Triggers configuration. However, Triggers don't appear to be supported in UWP. What is the best way to do what I need?
在UWP中,
XAML行为来执行类似的操作。
In UWP, XAML Behaviors is supported to do the similar things.
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
<Button Content="Behavior">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Click">
<Core:ChangePropertyAction PropertyName="Background">
<Core:ChangePropertyAction.Value>
<SolidColorBrush Color="Red"/>
</Core:ChangePropertyAction.Value>
</Core:ChangePropertyAction>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
在使用之前,您需要安装行为SDK
NuGet 。
问候,
Stanly
这篇关于[UWP] [XAML]便携式更改控件在事件上的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!