简体   繁体   English

检查是否已单击多边形

[英]Check if polygon has been clicked

Hi I have added a polygon to a map using: 嗨,我使用以下方法向地图添加了多边形:

//Creating a Polygon
Polygon MyPolygon = new Polygon(); 
MyPolygon.Points.Add(new Point(0, 0));
MyPolygon.Points.Add(new Point(95, 0));
MyPolygon.Points.Add(new Point(95, 35));
MyPolygon.Points.Add(new Point(10, 35));
MyPolygon.Points.Add(new Point(0, 75)); // 

MyPolygon.Stroke = new SolidColorBrush(Colors.Black);
MyPolygon.Fill = new SolidColorBrush(Colors.Black);

//Creating a MapOverlay and adding the Grid to it.
MapOverlay MyOverlay = new MapOverlay();
MyOverlay.Content = MyPolygon;
MyOverlay.GeoCoordinate = 
new GeoCoordinate(coordinate.Latitude,     coordinate.Longitude);
MyOverlay.PositionOrigin = new Point(0, 1.0);

//Creating a MapLayer and adding the MapOverlay to it            
mapLayer.Add(MyOverlay);

MyPolygon.MouseLeftButtonUp += new MouseButtonEventHandler(MyPolygon_Click);

... ...

private void MyPolygon_Click(object sender, MouseEventArgs e)
{
    TextBlock nametext;
    nametext = new TextBlock { Text = "1234" };          
}

I need to check if the user clicks on this polygon. 我需要检查用户是否单击了该多边形。 Can anyone help me with this please? 有人可以帮我吗?

Assuming a store app, the following code below is a quick and dirty sample that will react and add a TextBlock at the location you click on or touch the screen. 假设有一个商店应用程序,下面的代码是一个快速而肮脏的示例,该示例将做出反应并在您单击或触摸屏幕的位置添加一个TextBlock。

XAML XAML

<Page
    x:Class="StoreApp_PolyTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:StoreApp_PolyTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Canvas 
        Name="canvas"
        Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
        Loaded="Canvas_Loaded">

    </Canvas>
</Page>

Code behind 后面的代码

using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace StoreApp_PolyTest
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

        }

        private void Canvas_Loaded(object sender, RoutedEventArgs e)
        {
            Polygon MyPolygon = new Polygon();
            MyPolygon.Points.Add(new Point(0, 0));
            MyPolygon.Points.Add(new Point(95, 0));
            MyPolygon.Points.Add(new Point(95, 35));
            MyPolygon.Points.Add(new Point(10, 35));
            MyPolygon.Points.Add(new Point(0, 75)); // 

            MyPolygon.Stroke = new SolidColorBrush(Colors.Blue);
            MyPolygon.Fill = new SolidColorBrush(Colors.Blue);

            canvas.Children.Add(MyPolygon);

            MyPolygon.PointerPressed += MyPolygon_PointerPressed;
        }

        void MyPolygon_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            TextBlock nameText = new TextBlock() {Text="1234"};
            var point = e.GetCurrentPoint(this);
            Canvas.SetLeft(nameText, point.Position.X);
            Canvas.SetTop(nameText, point.Position.Y);
            canvas.Children.Add(nameText);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM