這是代碼重現它,XAML工作:在Windows Phone 7的,在ManipulationDelta事件並不如預期
<phone:PhoneApplicationPage
x:Class="WindowsPhoneApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Canvas x:Name="LayoutRoot" Background="Gray" Width="600" Height="800">
<!--ContentPanel - place additional content here-->
<Rectangle Name="rectangle" Width="100" Height="100" Fill="Red" />
</Canvas>
</phone:PhoneApplicationPage>
,這是後臺代碼:
public partial class MainPage : PhoneApplicationPage
{
private double translationX = 0.0;
private double translationY = 0.0;
// Constructor
public MainPage()
{
InitializeComponent();
LayoutRoot.ManipulationDelta += this.PhoneApplicationPage_ManipulationDelta;
}
void PhoneApplicationPage_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
this.translationX += e.DeltaManipulation.Translation.X;
this.translationY += e.DeltaManipulation.Translation.Y;
System.Diagnostics.Debug.WriteLine(string.Format("{0},{1}", this.translationX, this.translationY));
var c = new Rectangle();
//var c = rectangle;
c.Width = 100;
c.Height = 100;
c.Fill = new SolidColorBrush(Colors.Red);
c.SetValue(Canvas.LeftProperty, this.translationX);
c.SetValue(Canvas.TopProperty, this.translationY);
LayoutRoot.Children.Clear();
LayoutRoot.Children.Add(c);
}
}
運行這個程序,拖動紅色的矩形,你會發現矩形只移動一次,它不跟隨你的手指(或鼠標指針)的移動。
現在嘗試下面的代碼兩行更改:
var c = new Rectangle();
//var c = rectangle;
這種形式:
//var c = new Rectangle();
var c = rectangle;
然後再運行它,它會按預期工作,矩形將跟隨你的發現者(或鼠標指針)的運動,爲什麼?
謝謝
是的,我知道。我問的是爲什麼第一個版本無法正常工作。 – CuiPengFei