C# 滚动视图中画布上的矩形跟随手指
我在Windows Phone 7.1中使用画布和ScrollViewer构建了视图:C# 滚动视图中画布上的矩形跟随手指,c#,silverlight,canvas,windows-phone-7.1,scrollviewer,C#,Silverlight,Canvas,Windows Phone 7.1,Scrollviewer,我在Windows Phone 7.1中使用画布和ScrollViewer构建了视图: <Grid x:Name="LayoutRoot"> <ScrollViewer x:Name="SV" Margin="0,8,0,-8" ManipulationCompleted="ScheduleBackground_ManipulationCompleted" Hold="SV_Hold"> <Canvas x:Name="ScheduleVie
<Grid x:Name="LayoutRoot">
<ScrollViewer x:Name="SV" Margin="0,8,0,-8" ManipulationCompleted="ScheduleBackground_ManipulationCompleted" Hold="SV_Hold">
<Canvas x:Name="ScheduleView" Grid.Row="1" Margin="0,0" Height="1560" Loaded="ScheduleView_Loaded" >
<Canvas x:Name="ScheduleBackground" >
<Grid x:Name="HoursLegend" />
</Canvas>
</Canvas>
</ScrollViewer>
</Grid>
要存储矩形的我的类:
class TaskCreator
{
private System.Windows.Controls.Canvas ScheduleView;
private System.Windows.Input.GestureEventArgs e;
private Rectangle rec;
public TaskCreator(System.Windows.Controls.Canvas ScheduleView, System.Windows.Input.GestureEventArgs e)
{
this.ScheduleView = ScheduleView;
this.e = e;
CreateTask();
}
private void CreateTask()
{
if (rec != null) throw new InvalidOperationException("Cannot create task without clear the previous rectangle");
rec = new Rectangle();
SolidColorBrush brush = new SolidColorBrush(Colors.Red);
rec.Stroke = brush;
rec.Fill = brush;
rec.Width = this.ScheduleView.ActualWidth;
rec.Height = 45;
Point pos = e.GetPosition(this.ScheduleView);
rec.Margin = new Thickness(0, pos.Y, 0, 0);
this.ScheduleView.Children.Add(rec);
}
public void Clear()
{
if (rec != null && ScheduleView != null)
{
this.ScheduleView.Children.Remove(rec);
rec = null;
}
}
}
而且它也有效。当用户将手指放在画布上时,显示矩形,当用户将手指移开后,矩形消失
现在我想在用户移动手指时移动这个矩形(但仍然握住它)。
我该怎么做?我应该使用什么事件?
在我看来,这里最大的问题是ScrollViewer。绘制后矩形SV仍在工作(这很好),但当我滑动时,SV正在移动,并且我的矩形始终处于相同的位置(相对于画布)
我已经尝试过的:
我已将操纵delta=“SV_操纵delta”添加到SV:
在课堂上:
private TranslateTransform dragTranslation = new TranslateTransform();
internal void Move(System.Windows.Input.ManipulationDeltaEventArgs e)
{
dragTranslation.Y += e.DeltaManipulation.Translation.Y;
rec.RenderTransform = dragTranslation;
}
但是它不起作用。您可以使用另一个事件,它在手指每次移动时都会被调用
private void ManipulationDelta(object sender,System.Windows.Input.ManipulationDeltaEventArgs e)
{}
从中可以更新矩形的位置
值得注意的是,此事件并不总是在模拟器上触发,而是在设备上完美工作 好的,但是在witch对象上:SV(ScrollViewer)还是ScheduleView(Canvas)?我已经添加了一些代码,我已经尝试过了。
private TranslateTransform dragTranslation = new TranslateTransform();
internal void Move(System.Windows.Input.ManipulationDeltaEventArgs e)
{
dragTranslation.Y += e.DeltaManipulation.Translation.Y;
rec.RenderTransform = dragTranslation;
}
private void ManipulationDelta(object sender,System.Windows.Input.ManipulationDeltaEventArgs e)
{}