【发布时间】:2023-03-18 21:33:01
【问题描述】:
我有以下包含 3 个按钮的 XAML 代码。目标是在按下其中一个按钮时更改其他两个按钮的不透明度。
<Grid x:Name="MainToolbar">
<Grid Grid.Row="0" Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button x:Name="pinButton" HorizontalAlignment="Center" Grid.Column="0" Background="Red">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="newsButton" Storyboard.TargetProperty="Opacity">
<DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0.5"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="weatherButton" Storyboard.TargetProperty="Opacity">
<DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0.5"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
<Button.Template>
<ControlTemplate>
<Image Source="/Assets/Top-Pin-Icon-60px.png" Stretch="None"/>
</ControlTemplate>
</Button.Template>
</Button>
<Button x:Name="newsButton" HorizontalAlignment="Center" Grid.Column="1" Background="Red">
<Image Source="/Assets/Top-News-Icon-60px.png" Stretch="None"/>
</Button>
<Button x:Name="weatherButton" HorizontalAlignment="Center" Grid.Column="2" Background="Red">
<Image Source="/Assets/Top-Weather-Icon-60px.png" Stretch="None"/>
</Button>
</Grid>
</Grid>
我认为使用 EventTrigger 将是此类需求的解决方案,但我收到错误:无法在设置 RoutedEvent 的行上分配给属性“Windows.UI.Xaml.EventTrigger.RoutedEvent”。我检查了,我确定值应该是“Button.Click”。
这是针对通用 Windows 应用程序的,所以我不确定这是否会有所作为。还有其他方法吗?或者解决这个问题? 上面的代码在 UserControl 中。
更新:
经过一些研究,看起来更好的解决方案是使用视觉状态,所以我决定尝试以下方法。
<UserControl
x:Class="Innobec.Mobile.Apps.CityScope.UserControls.TopHorizontalToolBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Innobec.Mobile.Apps.CityScope.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="80"
d:DesignWidth="400">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ActiveButtonStates">
<VisualState x:Name="PinActiveState">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="newsButton" Storyboard.TargetProperty="Opacity" From="1" To="0.5" Duration="0:0:2"/>
<DoubleAnimation Storyboard.TargetName="weatherButton" Storyboard.TargetProperty="Opacity" From="1" To="0.5" Duration="0:0:2"/>
</Storyboard>
</VisualState>
<VisualState x:Name="NewsActiveState">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="pinButton" Storyboard.TargetProperty="Opacity" From="1" To="0.5" Duration="0:0:2"/>
<DoubleAnimation Storyboard.TargetName="weatherButton" Storyboard.TargetProperty="Opacity" From="1" To="0.5" Duration="0:0:2"/>
</Storyboard>
</VisualState>
<VisualState x:Name="WeatherActiveState">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="pinButton" Storyboard.TargetProperty="Opacity" From="1" To="0.5" Duration="0:0:2"/>
<DoubleAnimation Storyboard.TargetName="newsButton" Storyboard.TargetProperty="Opacity" From="1" To="0.5" Duration="0:0:2"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid x:Name="MainToolbar">
<Grid Grid.Row="0" Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button x:Name="pinButton" HorizontalAlignment="Center" Grid.Column="0" Background="Red" Click="pinButton_Click">
<Image Source="/Assets/Top-Pin-Icon-60px.png" Stretch="None"/>
</Button>
<Button x:Name="newsButton" HorizontalAlignment="Center" Grid.Column="1" Background="Red">
<Image Source="/Assets/Top-News-Icon-60px.png" Stretch="None"/>
</Button>
<Button x:Name="weatherButton" HorizontalAlignment="Center" Grid.Column="2" Background="Red">
<Image Source="/Assets/Top-Weather-Icon-60px.png" Stretch="None"/>
</Button>
</Grid>
</Grid>
但是,当我在代码隐藏中调用以下行时,什么都没有发生,那么我现在做错了什么?:
VisualStateManager.GoToState(this, "PinActiveState", false);
【问题讨论】:
-
尝试只使用“Click”而不是 Button.Click,我只在此属性中看到过方法名称
-
我试过了,XAML 代码显示波浪线表示“无法识别或无法访问成员 Click”。这可能是因为它是通用 Windows 应用程序,所以情况发生了变化?此 XAML 代码位于 UserControl 内(如果有帮助)。
标签: xaml win-universal-app visualstatemanager