C#: PointerPressed has been called twice when I click the image

夙愿已清 提交于 2019-12-12 03:34:19

问题


I am beginner in UWP. I wanna add a click event to an image that change splitview's open-status. So I have an image in ma xaml:

<Image x:Name="image_1_6" HorizontalAlignment="Left" Height="200" Margin="1225,559,-171,0" VerticalAlignment="Top" Width="200" Source="Assets\images.jpg" PointerPressed="image_1_6_PointerPressed"/>

I define image_1_6_PointerPressed as like as this:

private void image_1_6_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
    {
        mySplitView.IsPaneOpen = !mySplitView.IsPaneOpen;
    }

But when I click on image the mySplitView open and close! Can any body help me on this please? Thanks.


回答1:


pointerpresseddoesn't working like as click event. It calls when you pressed and keep button and when you leave button event finishes. So when you click on image you think this event calls twice! You should call Tapped event for this.

<Image x:Name="image_1_6" HorizontalAlignment="Left" Height="200" Margin="1225,559,-171,0" VerticalAlignment="Top" Width="200" Source="Assets\images.jpg" Tapped="image_1_6_PointerPressed"/>



回答2:


Until you diagnose, you can try something like:

  private bool pointerWorking = false;
  private void image_1_6_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
    if(!pointerWorking){
        pointerWorking = true;
        mySplitView.IsPaneOpen = !mySplitView.IsPaneOpen;
        pointerWorking = false;
    }
}

You can also try a different event



来源:https://stackoverflow.com/questions/39463158/c-pointerpressed-has-been-called-twice-when-i-click-the-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!