C# 检查是否已单击多边形
您好,我已使用以下方法将多边形添加到地图:C# 检查是否已单击多边形,c#,map,windows-store-apps,polygon,C#,Map,Windows Store Apps,Polygon,您好,我已使用以下方法将多边形添加到地图: //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.
//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);
我需要检查用户是否单击此多边形。有人能帮我吗?假设是一个商店应用程序,下面的代码是一个快速而肮脏的示例,它将做出反应,并在您单击或触摸屏幕的位置添加一个文本块 XAML
代码隐藏
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);
}
}
}
<代码>使用Windows .Fund;
使用Windows.UI;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
使用Windows.UI.Xaml.Shapes;
//空白页项模板被记录在http://go.microsoft.com/fwlink/?LinkId=234238
名称空间StoreApp_PolyTest
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
已加载私有无效画布(对象发送方、路由目标)
{
多边形MyPolygon=新多边形();
MyPolygon.Points.Add(新点(0,0));
MyPolygon.Points.Add(新点(95,0));
MyPolygon.Points.Add(新点(95,35));
MyPolygon.Points.Add(新点(10,35));
MyPolygon.Points.Add(新点(0,75));//
MyPolygon.Stroke=新的SolidColorBrush(Colors.Blue);
MyPolygon.Fill=新的SolidColorBrush(Colors.Blue);
canvas.Children.Add(MyPolygon);
MyPolygon.PointerPressed+=MyPolygon_PointerPressed;
}
void MyPolygon_PointerPressed(对象发送器,PointerRoutedEventArgs e)
{
TextBlock name Text=new TextBlock(){Text=“1234”};
var point=e.GetCurrentPoint(本);
Canvas.SetLeft(nameText,point.Position.X);
Canvas.SetTop(nameText,point.Position.Y);
canvas.Children.Add(nameText);
}
}
}
您是否尝试将MouseUp Eventhandler添加到多边形中?是的。我编辑了我的问题以显示这一点,但单击多边形时没有发生任何情况。您使用的是什么框架?WPF、Windows应用商店或?您的单击事件仅创建一个文本块,而不显示它。因此,可能偶数被触发,但没有显示。如何在屏幕上显示文本块?
<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>
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);
}
}
}