1

我正在尝试通过放置在 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>
4

1 回答 1

0

ElementHost 控件是您的 Wpf UserControl 的容器,您创建的任何公共方法都可以从 Windows.Form 应用程序访问。我将采用您的 userControls Button Click 事件中的代码并创建一个 Public Sub,然后从您的 Windows 窗体的 Button Click 事件中调用它。我将假设您在后面的代码中创建了动画。

Windows窗体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 用户控件

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

结束类

用户控件1.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>

这是您想要的工作示例。我将您的情节提要移到了 UserControl.Resources 中,因此可以分配 ax: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

表格1

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        UserControl11.runPathAnimation()
    End Sub
End Class
于 2012-12-05T04:58:47.813 回答