WPF渲染问题

时间:2015-07-24 11:06:58

标签: c# wpf xaml

我在画布上有一个圆圈,我的动画向右和向左旋转一条线,这是圆的半径,带有A和D键。但是当它被旋转时,它太滞后了,它冻结了一段时间。这是我的代码:

CameraView.xaml:

<UserControl x:Class="IKA.Views.CameraView"
  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" 
    xmlns:mui="http://firstfloorsoftware.com/ModernUI"
    xmlns:cal="http://www.caliburnproject.org"
    xmlns:model="clr-namespace:IKA.ViewModels"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ika="clr-namespace:IKA"
    d:DataContext="{x:Type model:CameraViewModel}"
    d:DesignHeight="300" d:DesignWidth="300"
    PreviewKeyDown="Move"     
         >
<Grid x:Name="grid">
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/DirectionControl.xaml" />
                <ResourceDictionary Source="/Resources/MediaPlayer.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <ControlTemplate  x:Key="PlayStyle" TargetType="Button">
                <Button Style="{StaticResource PlayButtonStyle}"  Width="100" Height="100" HorizontalAlignment="Center" />
            </ControlTemplate>
            <ControlTemplate x:Key="StopStyle" TargetType="Button">
                <Button Style="{StaticResource StopButtonStyle}"  Width="100" Height="100" HorizontalAlignment="Center" />
            </ControlTemplate>
        </ResourceDictionary>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Rectangle x:Name="BeforeVideo" Fill="Black" Grid.Row="0" Grid.RowSpan="6" />
    <MediaElement x:Name="myControl"  Grid.Row="0" Grid.RowSpan="6" Stretch="Fill"  LoadedBehavior="Manual" UnloadedBehavior="Stop" />
    <Button x:Name="PlayButton" Grid.Row="0"  Grid.RowSpan="6" Click="OnPlayButtonClick" Template="{StaticResource PlayStyle}">Play</Button>
    <Button x:Name="StopButton" Grid.Row="0" Grid.RowSpan="6" Click="OnStopButtonClick" Template="{StaticResource StopStyle}">Stop</Button>
    <Label x:Name="Degree" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Bottom"  Content="0"/>
    <Canvas x:Name="canvas" Grid.Column="1" Grid.Row="4" Grid.RowSpan="2" Height="100" Width="100" Focusable="True">
        <Ellipse  Stretch="Fill" Fill="White" StrokeThickness="3" Stroke="Black" Width="100" Height="100"/>
        <Line Name="CurrentLine" X1="50" Y1="50" X2="50" Y2="0" Stroke="Black" StrokeThickness="1" RenderTransformOrigin="1,1">
            <Line.RenderTransform>
                <RotateTransform x:Name="LineRotateTransform" Angle="0" ></RotateTransform>
            </Line.RenderTransform>
        </Line>
        <Line  X1="0" Y1="50" X2="10" Y2="50" Stroke="Black" StrokeThickness="1" RenderTransformOrigin="1,1" />
        <Line  X1="90" Y1="50" X2="100" Y2="50" Stroke="Black" StrokeThickness="1" RenderTransformOrigin="1,1" />
    </Canvas>
</Grid>

我的CameraView.xaml.cs:

    public partial class CameraView : UserControl
{
    public Line currentLine { get; set; }
    public DoubleAnimation myDoubleAnimation { get; set; }
    public Storyboard myStoryboard { get; set; }

    private void Animation(object sender,RoutedEventArgs e)
    {
        canvas.Focus();
        myDoubleAnimation = new DoubleAnimation();
        myDoubleAnimation.To = 0;
        myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(10));
        // Create the storyboard.
         myStoryboard = new Storyboard();
        myStoryboard.Children.Add(myDoubleAnimation);
        Storyboard.SetTarget(myDoubleAnimation,CurrentLine);
        Storyboard.SetTargetProperty(myDoubleAnimation,
            new PropertyPath("RenderTransform.(RotateTransform.Angle)"));
    }

    public CameraView()
    {
        InitializeComponent();
        this.DataContext = ViewModelsContainer.CameraViewModel;
        myControl.Visibility = Visibility.Visible;
        StopButton.Visibility = Visibility.Hidden;
        PlayButton.Visibility = Visibility.Visible;
        //string source = "http://" + ConnectionTemp.IPAdress + ":8080";
        string source = "http://192.168.1.159:8080/";
        //string source = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
        myControl.Source = new Uri(source);
        this.Loaded += Animation;
    }

    private void OnPlayButtonClick(object sender, RoutedEventArgs e)
    {
        BeforeVideo.Visibility = Visibility.Hidden;
        PlayButton.Visibility = Visibility.Hidden;
        myControl.Visibility = Visibility.Visible;
        PlayVideo();
        StopButton.Visibility = Visibility.Visible;
        canvas.Focus();
    }

    private void OnStopButtonClick(object sender, RoutedEventArgs e)
    {
        myControl.Pause();
        StopButton.Visibility = Visibility.Hidden;
        PlayButton.Visibility = Visibility.Visible;
        canvas.Focus();
    }

    private async void PlayVideo()
    {
        await PlayAsync();
    }

    private Task PlayAsync()
    {
        Task task = Task.Run(() => this.Dispatcher.Invoke(new Action(() => { myControl.Play(); })));
        return task;
    }
    public  void Move(object sender, KeyEventArgs e)
    {
        if(e.IsDown) {
            if (e.Key == Key.D)
            {
                hareket(true);
            }
            if (e.Key == Key.A)
            {
                hareket(false);
            }
        }
    }

    private void hareket(bool IncreaseOrDecrease)
    {
        if ((IncreaseOrDecrease)&&(myDoubleAnimation.To<= 90 ))
        {
            myDoubleAnimation.To++;
        }
        else if ((!IncreaseOrDecrease) && (myDoubleAnimation.To >= -90))
        {
            myDoubleAnimation.To--;
        }
        myStoryboard.Children.Add(myDoubleAnimation);
            myStoryboard.Begin();
            Degree.Content = myDoubleAnimation.To;
    }

}

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题:

       public  void Move(object sender, KeyEventArgs e)
    {
        if(e.IsDown && !isPressed)
        {
            isPressed = true;
            if (e.Key == Key.D)
            {
                hareket(true);
            }
            if (e.Key == Key.A)
            {
                hareket(false);
            }
        }
        else
        {
            isPressed = false;
        }
    }
    private void hareket(bool IncreaseOrDecrease)
    {
        myStoryboard.Children.Remove(myDoubleAnimation);
        if ((IncreaseOrDecrease)&&(myDoubleAnimation.To < 90 ))
        {
            myDoubleAnimation.To++;
        }
        else if ((!IncreaseOrDecrease) && (myDoubleAnimation.To > -90))
        {
            myDoubleAnimation.To--;
        }
        myStoryboard.Children.Add(myDoubleAnimation);
            myStoryboard.Begin();
            Degree.Content = myDoubleAnimation.To;
    }

解决我的问题的真实事实是将myStoryboard.Children.Remove(myDoubleAnimation)添加到hareket方法。我假设它试图开始所有MyDoubleAnimation s,所以在每次keydown之后它会变得更加滞后。

感谢大家的帮助和建议。