最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xaml - Pointer Events were not triggered in WinUi3 - Stack Overflow

programmeradmin0浏览0评论

My Xaml Structure :

   <Canvas>
      <Canvas  x:Name="BackCanvas" Width="10000"  Height="1000" Background="Transparent" PointerPressed="Canvas_PointerPressed"/>
      <Canvas  x:Name="FrontCanvas" Width="10000" Height="1000" />
   </Canvas>

C# code :

  private void Canvas_PointerPressed(object sender, PointerRoutedEventArgs e)  {  Debug.WriteLine("BackCanvas");   }

When i have structure like above, BackCanvas pointerPressed event is triggered correctly,because i didn't set background color for FrontCanvas.That's why the pointer Event is received by BackCanvas.

Incase if have Structure like below, the PointerPressed Event was not triggering...

My Xaml Code :

 <Canvas>
    <Canvas  x:Name="BackCanvas" Width="10000"  Height="1000" Background="Transparent" PointerPressed="Canvas_PointerPressed"/>
    <ScrollViewer x:Name="Scroller">
        <Canvas Width="5000" Height="5000"/>
    </ScrollViewer> 
</Canvas>

Here i'm having Scrollviewer instead of FrontCanvas.Here also i didn't set any background color for scrollviewer.But the BackCanvas PointerPressed event was not firing.

What i'm trying to acheive is i want to receive viewChange from scrollviewer and receive pointer events from BackCanvas while pointer actions.

NOTE: I can't keep the BackCanvas inside the ScrollViewer.My app structure was not like that.

My Xaml Structure :

   <Canvas>
      <Canvas  x:Name="BackCanvas" Width="10000"  Height="1000" Background="Transparent" PointerPressed="Canvas_PointerPressed"/>
      <Canvas  x:Name="FrontCanvas" Width="10000" Height="1000" />
   </Canvas>

C# code :

  private void Canvas_PointerPressed(object sender, PointerRoutedEventArgs e)  {  Debug.WriteLine("BackCanvas");   }

When i have structure like above, BackCanvas pointerPressed event is triggered correctly,because i didn't set background color for FrontCanvas.That's why the pointer Event is received by BackCanvas.

Incase if have Structure like below, the PointerPressed Event was not triggering...

My Xaml Code :

 <Canvas>
    <Canvas  x:Name="BackCanvas" Width="10000"  Height="1000" Background="Transparent" PointerPressed="Canvas_PointerPressed"/>
    <ScrollViewer x:Name="Scroller">
        <Canvas Width="5000" Height="5000"/>
    </ScrollViewer> 
</Canvas>

Here i'm having Scrollviewer instead of FrontCanvas.Here also i didn't set any background color for scrollviewer.But the BackCanvas PointerPressed event was not firing.

What i'm trying to acheive is i want to receive viewChange from scrollviewer and receive pointer events from BackCanvas while pointer actions.

NOTE: I can't keep the BackCanvas inside the ScrollViewer.My app structure was not like that.

Share Improve this question asked Feb 4 at 7:52 PremKumar ShanmugamPremKumar Shanmugam 4414 silver badges15 bronze badges 5
  • This looks like by design. The ScrollViewer will get the pointer event automatically. If you don't change the layout, then you may try to manually call the Canvas_PointerPressed event in the` ScrollViewer` pointer event. – Ax1le Commented Feb 4 at 8:28
  • we are rendering in SwapChainPanel. This rendering and my view Layer(like backCanvas) should sync while scrolling the scrollviewer and i need interaction with view Layer. My SwapChain,View,and Scrollviewer all were children inside a Canvas. – PremKumar Shanmugam Commented Feb 4 at 8:53
  • Manually calling the canvas_pointerPressed is possibe.But i have lots of views inside the backCanvas,i want interaction in backCanvas – PremKumar Shanmugam Commented Feb 4 at 8:55
  • The Canvas inside the ScrollViewer will register "pointer presses". You can try: Point pos = e.GetPosition( this.BackCanvas ); ... and see if it "translates". However, I think a "parent Grid" (cell) would be more friendly than a Canvas when it comes to "stacking" elements. – Gerry Schmitz Commented Feb 4 at 20:40
  • @GerrySchmitz , if i try like Point pos = e.GetPosition( this.BackCanvas ) for scrollviewer's canvas pointerPress it will give the relative position, but it won't trigger the backCanvas pointer evnts right !? – PremKumar Shanmugam Commented Feb 5 at 4:10
Add a comment  | 

1 Answer 1

Reset to default 0

You can call Canvas_PointerPressed directly:

<Canvas>
    <Canvas x:Name="BackCanvas"
        Width="10000"
        Height="1000"
        Background="Transparent" />
    <ScrollViewer x:Name="Scroller"
        PointerPressed="Scroller_PointerPressed"
        ViewChanged="Scroller_ViewChanged">
        <Canvas
            Width="5000"
            Height="5000" />
    </ScrollViewer>
</Canvas>
    private void Scroller_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Scroller_ViewChanged");
    }

    private void Scroller_PointerPressed(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Scroller_PointerPressed");
        // Setting `Handled` to `false` does not seem to work.
        // e.Handled = false;
        Canvas_PointerPressed(sender, e);
    }

    private void Canvas_PointerPressed(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("BackCanvas_PointerPressed");

    }
发布评论

评论列表(0)

  1. 暂无评论