这是Powershell/WPF(加载的程序集是从我的项目粘贴的,并且大部分不需要)
我正在制作一个动画弹出按钮,它按预期工作,我单击该按钮,它滑入到位,在动画结束时它隐藏,一个不同的按钮出现在其位置,并准备好等待单击相反的动画,这种行为切换得很好。我想让单击任意位置(不仅仅是第二个按钮)触发将第二个按钮向后移动。
在真实的脚本中会出现一个弹出窗口,我的目的是通过单击除按钮本身之外的窗口外的任何位置来使其可关闭。我目前正在使用此功能,但如果未直接单击按钮,关闭动画将被跳过,因为我无法(不知道如何)在没有按钮的情况下触发它。
我知道很可能有人知道如何在 C# 中执行此操作,并且我愿意举例并弄清楚如何制作类似的 PS 代码隐藏,所以我也标记了 C#,但如果有人知道 PS 方法,那么将会非常有帮助。
Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms, WindowsFormsIntegration, presentationCore
[xml]$xaml='
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStartupLocation="CenterScreen" WindowStyle="None" Background="Transparent" AllowsTransparency="True" Width="500" Height="300">
<Window.Resources>
<ControlTemplate x:Key="NoMouseOverButtonTemplate" TargetType="Button">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{x:Static SystemColors.ControlLightBrush}"/>
<Setter Property="Foreground" Value="{x:Static SystemColors.GrayTextBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Canvas>
<Button Name="MovableButton1" Canvas.Left="70" Canvas.Top="0" FontSize="12" FontFamily="Calibri" FontWeight="Light" BorderBrush="#111111" Foreground="#EEEEEE" Background="#111111" Height="18" Width="70" Template="{StaticResource NoMouseOverButtonTemplate}">Button1<Button.Triggers>
<EventTrigger RoutedEvent="PreviewMouseUp">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Name="ButtonAnimation1" From="70" To="207" Duration="0:0:0.25" Storyboard.TargetProperty="(Canvas.Left)" AutoReverse="False" FillBehavior="Stop"/>
</Storyboard>
</BeginStoryboard></EventTrigger>
</Button.Triggers>
</Button>
<Button Name="MovableButton2" Canvas.Left="207" Canvas.Top="0" Visibility="Hidden" FontSize="12" FontFamily="Calibri" FontWeight="Light" BorderBrush="#111111" Foreground="#EEEEEE" Background="#111111" Height="18" Width="70" Template="{StaticResource NoMouseOverButtonTemplate}">Button2<Button.Triggers>
<EventTrigger RoutedEvent="PreviewMouseUp">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Name="ButtonAnimation2" From="207" To="70" Duration="0:0:0.25" Storyboard.TargetProperty="(Canvas.Left)" AutoReverse="False" FillBehavior="Stop"/>
</Storyboard>
</BeginStoryboard></EventTrigger>
</Button.Triggers>
</Button>
</Canvas>
</Window>'
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$window=[Windows.Markup.XamlReader]::Load($reader)
$Button1=$Window.FindName("MovableButton1")
$Button2=$Window.FindName("MovableButton2")
$ButtonAnimation1=$Window.FindName("ButtonAnimation1")
$ButtonAnimation2=$Window.FindName("ButtonAnimation2")
$Button1.Add_Click({
write-host FirstButtonClicked
})
$Button2.Add_Click({
write-host SecondButtonClicked
})
$ButtonAnimation1.Add_Completed({
write-host AnimationCompleted
$Button1.Visibility="Hidden"
$Button2.Visibility="Visible"
})
$ButtonAnimation2.Add_Completed({
write-host AnimationCompleted
$Button1.Visibility="Visible"
$Button2.Visibility="Hidden"
})
$window.Show()
$appContext=New-Object System.Windows.Forms.ApplicationContext
[void][System.Windows.Forms.Application]::Run($appContext)
我最终切换到数据触发器并使用隐藏的文本框来存储值,如果值被切换,动画就会触发。现在任何按钮(或任何其他可以更改值的按钮)都可以触发动画
一个缺点是我们确实失去了侦听附加到数据触发器的动画的“完成”事件的能力,因为它们仅从用户输入中弹出,例如点击一下
Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms, WindowsFormsIntegration, presentationCore
[xml]$xaml='
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MovingButtonSample" Height="150" Width="270">
<Window.Resources>
<ControlTemplate x:Key="NoMouseOverButtonTemplate" TargetType="Button">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{x:Static SystemColors.ControlLightBrush}"/>
<Setter Property="Foreground" Value="{x:Static SystemColors.GrayTextBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Canvas>
<TextBlock Visibility="Hidden" Name="ButtonData"/>
<Button Name="Button1" Canvas.Left="5" Canvas.Top="0" FontSize="12" FontFamily="Calibri" FontWeight="Light" BorderBrush="#111111" Foreground="#DDDDDD" Background="#111111" Height="18" Width="25" Template="{StaticResource NoMouseOverButtonTemplate}">>><Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ButtonData, Path=Text}" Value="1">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="5" To="180" Duration="0:0:0.25" Storyboard.TargetProperty="(Canvas.Left)" AutoReverse="False"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style></Button.Style>
</Button>
<Button Name="Button2" Canvas.Left="180" Canvas.Top="0" Visibility="Hidden" FontSize="12" FontFamily="Calibri" FontWeight="Light" BorderBrush="#111111" Foreground="#DDDDDD" Background="#111111" Height="18" Width="25" Template="{StaticResource NoMouseOverButtonTemplate}">Playlist<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ButtonData, Path=Text}" Value="0">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="295" To="5" Duration="0:0:0.25" Storyboard.TargetProperty="(Canvas.Left)" AutoReverse="False"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style></Button.Style>
</Button>
<Button Name="Button3" Canvas.Top="50" Height="20" Width="40"/>
</Canvas>
</Window>'
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$window=[Windows.Markup.XamlReader]::Load($reader)
$global:Pressed=0
$Button1=$Window.FindName("Button1")
$Button2=$Window.FindName("Button2")
$Button3=$Window.FindName("Button3")
$ButtonData=$Window.FindName("ButtonData")
function toggleFlyout(){
Switch($Pressed){
0{
$ButtonData.Text='1'
$Button1.Height="18"
$Button1.Width="70"
$global:Pressed=1
$Button1.Visibility="visible"
$Button2.Visibility="hidden"
$Button2.Content=">>"
$Button1.Content="Hello"
}
1{
$ButtonData.Text='0'
$Button1.Height="18"
$Button1.Width="25"
$Button1.Visibility="hidden"
$Button2.Visibility="visible"
$global:Pressed=0
}
}
}
$Button1.Add_Click({
toggleFlyout
})
$Button2.Add_Click({
toggleFlyout
})
$Button3.Add_Click({
toggleFlyout
})
$window.Show()
$appContext=New-Object System.Windows.Forms.ApplicationContext
[void][System.Windows.Forms.Application]::Run($appContext)