【问题标题】:How to get tap position to add control?如何获得点击位置以添加控制?
【发布时间】:2015-06-04 07:47:04
【问题描述】:

我有一个包含图像的 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; 

谁能帮帮我! 谢谢

这是我的 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>

【问题讨论】:

  • 什么是ContentPanel?它是画布吗?能否为ContentPanelOrginalImage 添加XAML 代码?
  • 嗨 Truong,你试过我的回答吗?对你有用吗?
  • 嗨,肯尼兹。我已经用我的函数尝试了你的答案和相同的结果(我的帖子后面的代码)。但是,如果我将根控件从 Grid 更改为 Canvas。它的位置更近了。谢谢

标签: image windows-phone-8 textbox


【解决方案1】:

问题是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;

【讨论】:

    【解决方案2】:

    获取位置

    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...

    【讨论】:

    • 嗨 Vijay,我看到了您推荐的链接,但我认为它用于整个屏幕。就我而言,我只想在图像区域中添加控件。
    • 我试过你的代码。与我的代码相同的结果。我认为问题是文本框边距厚度与 x、y 坐标不同
    • 把你的图片放到 Canvas 中并为 Handle Tap 编写事件并尝试获取位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2021-06-11
    • 2021-08-12
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    相关资源
    最近更新 更多