我正在尝试通过我放在Form1.vb中的按钮启动我在elemnthost1中的动画
Elementhost已经包含一个工作按钮,但是我需要通过单击Form.vb按钮来点击它
单击元素主机中的按钮也可以通过单击“表单”中的“开始”按钮,或者通过单击“Form.vb”中的“开始”按钮来启动动画更好。
我一直试图解决这个问题,但我无法做到这一点
由于 STE 这是我的xaml:
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="372" Width="640">
<UserControl.Resources>
<PathGeometry x:Key="Daytona">
<PathFigure IsClosed="True" StartPoint="290,320">
<ArcSegment Point="290,320" Size="10,10" SweepDirection="Counterclockwise"></ArcSegment>
<ArcSegment Point="430,250" Size="200,75"></ArcSegment>
<ArcSegment Point="400,225" Size="30,25"></ArcSegment>
<ArcSegment Point="370,215" Size="200,200" SweepDirection="Clockwise" ></ArcSegment>
<ArcSegment Point="320,207" Size="150,200"></ArcSegment>
<BezierSegment Point1="305,207" Point2="230,200" Point3="295,175"></BezierSegment>
<ArcSegment Point="420,175" Size="570,500" SweepDirection="Clockwise"></ArcSegment>
<BezierSegment Point1="437,177" Point2="452,162" Point3="462,152"></BezierSegment>
<BezierSegment Point1="470,140" Point2="555,75" Point3="527,140"></BezierSegment>
<ArcSegment Point="455,210" Size="500,500" SweepDirection="Clockwise"></ArcSegment>
<BezierSegment Point1="445,220" Point2="450,280" Point3="545,220"></BezierSegment>
<ArcSegment Point="480,18" Size="120,115"></ArcSegment>
<ArcSegment Point="270,17" Size="520,100"></ArcSegment>
<BezierSegment Point1="260,17" Point2="250,15" Point3="228,34"></BezierSegment>
<BezierSegment Point1="218,34" Point2="200,33" Point3="180,20"></BezierSegment>
<ArcSegment Point="40,220" Size="140,121"></ArcSegment>
<ArcSegment Point="290,320" Size="690,600"></ArcSegment>
</PathFigure>
</PathGeometry>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingPath Storyboard.TargetName="Ellipse1"
Storyboard.TargetProperty="(Canvas.Left)"
PathGeometry="{DynamicResource Daytona}"
Duration="0:0:20" RepeatBehavior="Forever" Source="X" FillBehavior="Stop" />
<DoubleAnimationUsingPath Storyboard.TargetName="Ellipse1"
Storyboard.TargetProperty="(Canvas.Top)"
PathGeometry="{StaticResource Daytona}"
Duration="0:0:20" RepeatBehavior="Forever" Source="Y" FillBehavior="Stop" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</UserControl.Triggers>
<Canvas Height="357" Name="Canvas1" Width="631">
<Canvas.Background>
<ImageBrush ImageSource="/WpfCircuitLibrary;component/Images/Daytona2.png" />
</Canvas.Background>
<Ellipse Canvas.Left="292" Canvas.Top="328" Height="29" Name="Ellipse1" Stroke="Black" Width="29" Fill="#FF3DCA1F" />
<Path Canvas.Left="12" Canvas.Top="12" Height="336" Name="Daytona" Data="{StaticResource Daytona}" Stroke="Black" Width="607" StrokeThickness="5" />
<Button Content="Button" Height="30" Name="Button1" Width="92" DataContext="{Binding}" Canvas.Left="263" Canvas.Top="382" IsEnabled="{Binding}" />
</Canvas>
</UserControl>
答案 0 :(得分:0)
ElementHost Control是Wpf UserControl的容器,您创建的任何公共方法都可以从Windows.Form应用程序访问。我将获取userControls Button Click事件中的代码并创建一个Public Sub,然后从Windows窗体的Button Click事件中调用它。我假设您在代码后面创建了动画。
Windows Form Form1.vb
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
UserControl11.runAnimation()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
UserControl11.runXamlStoryBoard()
End Sub
End Class
Wpf UserControl
Imports System.Windows.Media.Animation
Imports System.Windows.Controls
Public Class UserControl1
Public Sub runAnimation()
Dim da As DoubleAnimation = New DoubleAnimation
da.From = 30
da.To = 100
da.Duration = New Windows.Duration(TimeSpan.FromSeconds(1))
da.AutoReverse = True
Button1.BeginAnimation(Button.HeightProperty, da)
End Sub
Public Sub runXamlStoryBoard()
Dim sb As Storyboard = CType(FindResource("growButton"), Storyboard)
sb.Begin()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
runAnimation()
End Sub
结束班
<强> UserControl1.Xaml 强>
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="116" d:DesignWidth="246">
<UserControl.Resources>
<Storyboard x:Key="growButton">
<DoubleAnimation
Storyboard.TargetName="Button1"
Storyboard.TargetProperty="Height"
From="30"
To="100"
Duration="0:0:1"
RepeatBehavior="4x" />
</Storyboard>
</UserControl.Resources>
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Center" Name="Button1" VerticalAlignment="Center" Width="75" />
</Grid>
</UserControl>
以下是您想要的实例。我将您的Storyboard移动到UserControl.Resources中,因此可以分配x:Key,使其在CodeBehind中可用。然后我在你的引用中引用了一个静态资源。
** UserControl1.xaml“
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="372" Width="640">
<UserControl.Resources>
<PathGeometry x:Key="Daytona">
<PathFigure IsClosed="True" StartPoint="290,320">
<ArcSegment Point="290,320" Size="10,10" SweepDirection="Counterclockwise"></ArcSegment>
<ArcSegment Point="430,250" Size="200,75"></ArcSegment>
<ArcSegment Point="400,225" Size="30,25"></ArcSegment>
<ArcSegment Point="370,215" Size="200,200" SweepDirection="Clockwise" ></ArcSegment>
<ArcSegment Point="320,207" Size="150,200"></ArcSegment>
<BezierSegment Point1="305,207" Point2="230,200" Point3="295,175"></BezierSegment>
<ArcSegment Point="420,175" Size="570,500" SweepDirection="Clockwise"></ArcSegment>
<BezierSegment Point1="437,177" Point2="452,162" Point3="462,152"></BezierSegment>
<BezierSegment Point1="470,140" Point2="555,75" Point3="527,140"></BezierSegment>
<ArcSegment Point="455,210" Size="500,500" SweepDirection="Clockwise"></ArcSegment>
<BezierSegment Point1="445,220" Point2="450,280" Point3="545,220"></BezierSegment>
<ArcSegment Point="480,18" Size="120,115"></ArcSegment>
<ArcSegment Point="270,17" Size="520,100"></ArcSegment>
<BezierSegment Point1="260,17" Point2="250,15" Point3="228,34"></BezierSegment>
<BezierSegment Point1="218,34" Point2="200,33" Point3="180,20"></BezierSegment>
<ArcSegment Point="40,220" Size="140,121"></ArcSegment>
<ArcSegment Point="290,320" Size="690,600"></ArcSegment>
</PathFigure>
</PathGeometry>
<Storyboard x:Key="MyPathAnimation">
<DoubleAnimationUsingPath Storyboard.TargetName="Ellipse1"
Storyboard.TargetProperty="(Canvas.Left)"
PathGeometry="{DynamicResource Daytona}"
Duration="0:0:20" RepeatBehavior="Forever" Source="X" FillBehavior="Stop" />
<DoubleAnimationUsingPath Storyboard.TargetName="Ellipse1"
Storyboard.TargetProperty="(Canvas.Top)"
PathGeometry="{StaticResource Daytona}"
Duration="0:0:20" RepeatBehavior="Forever" Source="Y" FillBehavior="Stop" />
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard Storyboard="{StaticResource MyPathAnimation}" />
</EventTrigger.Actions>
</EventTrigger>
</UserControl.Triggers>
<Canvas Height="357" Name="Canvas1" Width="631">
<Canvas.Background>
<ImageBrush ImageSource="/WpfCircuitLibrary;component/Images/Daytona2.png" />
</Canvas.Background>
<Ellipse Canvas.Left="292" Canvas.Top="328" Height="29" Name="Ellipse1" Stroke="Black" Width="29" Fill="#FF3DCA1F" />
<Path Canvas.Left="12" Canvas.Top="12" Height="336" Name="Daytona" Data="{StaticResource Daytona}" Stroke="Black" Width="607" StrokeThickness="5" />
<Button Content="Button" Height="30" Name="Button1" Width="92" DataContext="{Binding}" Canvas.Left="263" Canvas.Top="382" IsEnabled="{Binding}" />
</Canvas>
</UserControl>
<强> UserControl1.Xaml.vb 强>
Imports System.Windows.Media.Animation
Public Class UserControl1
Public Sub runPathAnimation()
Dim sb As Storyboard = CType(FindResource("MyPathAnimation"), Storyboard)
sb.Begin()
End Sub
End Class
<强> Form1中强>
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
UserControl11.runPathAnimation()
End Sub
End Class