2015-03-31 87 views
0

我有一個Windows phone 8.0項目,有一個圖像。當用戶點擊圖像時,我必須得到水龍頭的位置,並添加一個文本框到這個位置(上圖)。如何獲取水龍頭位置來添加控制?

現在,我使用下面的代碼,但我總是得到一個錯誤的位置

 MessageBox.Show("vao day"); 
     var mp = GetMousePoint(e); 
     MessageBox.Show("x=" + mp.X + "_" + "y=" + mp.Y); 
     //x: 258; y:97 
     double top= 0; double right = 0; double bottom =0; double left=0; 
     top = e.GetPosition(OrginalImage).Y; //(double)mp.Y - 50; 
     left = e.GetPosition(OrginalImage).X; //(double)mp.X - 50; 

     MessageBox.Show("x=" + left.ToString() + "_" + "y=" + top.ToString()); 
     //add textbox to this position 
     TextBox newtext = new TextBox(); 
     newtext.Text = "truongpm"; 
     newtext.Margin = new Thickness(left, top, right, bottom); 
     newtext.Width = 124; 
     newtext.Height = 68; 

     //button.VerticalAlignment = 234; 
     //Add contentpanel to your page if there's not one already. 
     ContentPanel.Children.Add(newtext); 
     newtext.Visibility = System.Windows.Visibility.Visible; 

enter image description here

任何人可以幫助我請! 感謝

這是我的XAML代碼

<Grid x:Name="ContentPanel" Margin="5"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Image Grid.Row="0" MouseMove="Image_MouseMove" Name="OrginalImage" Stretch="Fill" Height="250" Source="/Assets/vd1.png"/> 
     <Button Grid.Row="1" Content="ALter" Width="100" Height="80" Click="AlterButton_Click" Foreground="#FFD66A6A" BorderBrush="#FF16C71E" Margin="10,0,360,0"/> 
     <Button Grid.Row="1" Content="Save" Width="100" Height="80" Foreground="#FFD66A6A" BorderBrush="#FF16C71E" Margin="115,0,255,0" Click="Button_Click"></Button> 
     <Button Grid.Row="1" Content="Text" Width="100" Height="80" Foreground="#FFD66A6A" BorderBrush="#FF16C71E" Margin="115,0,055,0" Click="TextButton_Click"></Button> 
     <Image Grid.Row="2" Name="AlterImage" /> 

     <!--<TextBox HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="180" Margin="110,65,0,0" Grid.Row="0"/>--> 
    </Grid> 

</Grid> 
+0

什麼是'ContentPanel'?它是一個畫布?你可以添加'ContentPanel'和'OrginalImage'的XAML代碼嗎? – kennyzx 2015-03-31 05:27:08

+0

嗨Truong,你有沒有試過我的答案?對你起作用嗎? – kennyzx 2015-03-31 10:06:57

+0

hi kennyzx。我已經試過了你的答案和我的功能相同的結果(在我的帖子後面的代碼)。但是,如果我改變從網格到畫布的根控制。它的位置更接近。感謝 – 2015-04-01 07:29:55

回答

0

爲了得到這個職位取自

void Touch_FrameReported(object sender, TouchFrameEventArgs e) 
     { 
      TouchPoint touchPoint = e.GetTouchPoints(this.ContentPanel).FirstOrDefault(); 
      if (touchPoint.Action == TouchAction.Up) 
      MessageBox.Show("Selected coordinates:"+touchPoint.Position.X + "," + touchPoint.Position.Y);//Displaying x&y co-ordinates of Touch point 
     } 

來源:This Link ...

+0

嗨Vijay,我看到了你推薦的鏈接,但我認爲它用於整個屏幕。在我的情況下,我只想在圖像區域添加控件。 – 2015-03-31 06:17:26

+0

我試過你的代碼。與我的代碼相同的結果。我認爲問題是文本框邊距的厚度不同於x,y座標 – 2015-03-31 06:27:08

+0

將圖像放入畫布併爲Handle Tap寫入事件並嘗試獲取位置。 – Vijay 2015-03-31 06:35:46

0

的問題是, TextBox位於在ContentPanel(這是一個網格)的中心默認情況下。你預計它位於左上角,對吧?如果ContentPanel是Canvas,但不適用於Grid,則爲真。

您可以通過將TextBox's Margin設置爲(0,0,0,0)來驗證此情況,請注意TextBox將位於中心位置,而不是位於左上角。

double top= 0; double right = 0; double bottom =0; double left=0; 
newtext.Margin = new Thickness(left, top, right, bottom); 

現在修復:設置Horizo​​ntalAlignment和VerticalAlignment屬性。

TextBox newtext = new TextBox(); 
newtext.HorizontalAlignment = HorizontalAlignment.Left; 
newtext.VerticalAlignment = VerticalAlignment.Top;