简体   繁体   English

如何将事件处理程序设置为控件?

[英]How do i set event handler to a control?

I am new to windows 8 app development using xaml and c#. 我是使用xaml和c#进行Windows 8应用程序开发的新手。 I created a new image using. 我使用创建了一个新图像。

  Image img = new Image();

I successfully added the image to the canvas, but i want to add an eventhandler pointerpressed to the img. 我已成功将图像添加到画布,但是我想添加一个按下img的事件处理程序指针。 How do i do it? 我该怎么做?

You have your Image instance in code 您在代码中有Image实例

Image img = new Image();

And I'm presuming you've added it to your Canvas also in code, so the logical place to hook your event in code 我假设您也已通过代码将其添加到Canvas中,因此将事件挂接到代码中的逻辑位置

Image img = new Image();
img.PointerPressed += Image_PointerPressed;

Where you have defined the Image_PointerPressed elsewhere in that same class 在同一类的其他地方定义Image_PointerPressed的位置

private void Image_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    // do what you want here...
}

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

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