C# WPF渲染问题
我在画布上画了一个圆,我的动画用a和D关键点左右旋转一条线,这是圆的半径。但当它旋转时,它有太大的滞后,会冻结一段时间。这是我的代码: CameraView.xaml:C# WPF渲染问题,c#,wpf,xaml,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
<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;
}
}
公共部分类CameraView:UserControl
{
公共线路currentLine{get;set;}
public DoubleAnimation myDoubleAnimation{get;set;}
公共故事板myStoryboard{get;set;}
私有无效动画(对象发送器、RoutedEventArgs e)
{
canvas.Focus();
myDoubleAnimation=新的DoubleAnimation();
myDoubleAnimation.To=0;
myDoubleAnimation.Duration=新的持续时间(TimeSpan.From毫秒(10));
//创建故事板。
myStoryboard=新故事板();
mytoryboard.Children.Add(myDoubleAnimation);
Storyboard.SetTarget(myDoubleAnimation,CurrentLine);
Storyboard.SetTargetProperty(myDoubleAnimation,
新的PropertyPath(“RenderTransform.(RotateTransform.Angle)”);
}
公共摄像师()
{
初始化组件();
this.DataContext=ViewModelsContainer.CameraViewModel;
myControl.Visibility=可见性.Visibility;
StopButton.Visibility=Visibility.Hidden;
PlayButton.Visibility=可见性.Visibility;
//string source=“http://“+ConnectionTemp.ipAddress+”:8080”;
字符串源=”http://192.168.1.159:8080/";
//字符串源=”http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
myControl.Source=新Uri(源);
这个.Loaded+=动画;
}
private void onPlayButton单击(对象发送方,路由目标)
{
BeforeVideo.Visibility=可见性.Hidden;
PlayButton.Visibility=Visibility.Hidden;
myControl.Visibility=可见性.Visibility;
播放视频();
StopButton.Visibility=可见性.Visibility;
canvas.Focus();
}
private void OnStopButton单击(对象发送方,路由目标)
{
myControl.Pause();
StopButton.Visibility=Visibility.Hidden;
PlayButton.Visibility=可见性.Visibility;
canvas.Focus();
}
专用异步void PlayVideo()
{
等待PlaySync();
}
专用任务PlaySync()
{
Task Task=Task.Run(()=>this.Dispatcher.Invoke(新操作(()=>{myControl.Play();}));
返回任务;
}
公共无效移动(对象发送方,KeyEventArgs e)
{
如果(如伊斯敦){
如果(e.Key==Key.D)
{
哈雷克特(真);
}
如果(e.Key==Key.A)
{
hareket(假);
}
}
}
私人无效资产(bool增加或减少)
{
如果((递增或递减)&&(myDoubleAnimation.To=-90))
{
myDoubleAnimation.To--;
}
mytoryboard.Children.Add(myDoubleAnimation);
myStoryboard.Begin();
Degree.Content=myDoubleAnimation.To;
}
}
我已经解决了这个问题,因此:
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;
}
public void移动(对象发送方,KeyEventArgs e)
{
如果(例如,按下和按下)
{
isPressed=true;
如果(e.Key==Key.D)
{
哈雷克特(真);
}
如果(e.Key==Key.A)
{
hareket(假);
}
}
其他的
{
isPressed=false;
}
}
私人无效资产(bool增加或减少)
{
mytoryboard.Children.Remove(myDoubleAnimation);
如果((递增或递减)&(myDoubleAnimation.To<90))
{
myDoubleAnimation.To++;
}
如果((!IncreaseOrdRecrease)和(&(myDoubleAnimation.To>-90))
{
myDoubleAnimation.To--;
}
mytoryboard.Children.Add(myDoubleAnimation);
myStoryboard.Begin();
Degree.Content=myDoubleAnimation.To;
}
解决我问题的真正方法是将myStoryboard.Children.Remove(myDoubleAnimation)
添加到hareket
方法。我假设它试图开始所有的MyDoubleAnimation
s,所以每次按下键后它都会变得更滞后
感谢大家的帮助和建议。我已经解决了问题,因此:
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;
}
public void移动(对象发送方,KeyEventArgs e)
{
如果(例如,按下和按下)
{
isPressed=true;
如果(e.Key==Key.D)
{
哈雷克特(真);
}
如果(e.Key==Key.A)
{
hareket(假);
}
}
其他的
{
isPressed=false;
}
}
私人无效资产(bool增加或减少)
{
mytoryboard.Children.Remove(myDoubleAnimation);
如果((递增或递减)&(myDoubleAnimation.To<90))
{
myDoubleAnimation.To++;
}
如果((!IncreaseOrdRecrease)和(&(myDoubleAnimation.To>-90))
{
myDoubleAnimation.To--;
}
mytoryboard.Children.Add(myDoubleAnimation);
myStoryboard.Begin();
Degree.Content=myDoubleAnimation.To;
}
解决我问题的真正方法是将myStoryboard.Children.Remove(myDoubleAnimation)
添加到hareket
方法。我假设它试图开始所有的MyDoubleAnimation
s,所以每次按下键后它都会变得更滞后