简体   繁体   English

如何正确使用VisualTreeHelper#HitTest和TagVisualizer

[英]How to use VisualTreeHelper#HitTest and TagVisualizer correctly

In my application I want to ensure that a TagVisualization is only displayed if the tagged object is placed on a Ellipse. 在我的应用程序中,我想确保仅当将标记的对象放置在Ellipse上时才显示TagVisualization。 So I used this code to do that: 所以我用下面的代码来做到这一点:

 private void TagVisualizer_VisualizationAdded(object sender, TagVisualizerEventArgs e)
        {
            Console.WriteLine("Hitlist");

            //Notes
            if (e.TagVisualization.GetType() == typeof(NoteVisualization))
            {
                bool found = false;
                Point pt = e.TagVisualization.Center;
                hitResultsList.Clear();
                VisualTreeHelper.HitTest(RootLayer, null, new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(pt));

                if (hitResultsList.Count > 0)
                {
                    foreach (DependencyObject o in hitResultsList)
                    {
                        if (o.GetType() == typeof(Ellipse))
                        {

                            Console.WriteLine("Placed on a Sourcefile");

                            SourceFile sf = (((o as Ellipse).Tag) as SourceFile);
                            GroupBox gp = e.TagVisualization.FindName("GroupHeader") as GroupBox;
                            gp.Header = sf.getFullName();
                            e.TagVisualization.Tag = sf;

                            SurfaceButton save = e.TagVisualization.FindName("NoteSave") as SurfaceButton;
                            save.Tag = sf;

                            found = true;
                            break;
                        }



                    }
                }

                if (!found)
                {
                    e.TagVisualization.Visibility = System.Windows.Visibility.Collapsed;
                    Console.WriteLine("Placed somewhere else");
                }
            }
        }

I'm not really sure if this is the correct way, since I don't avoid that the TagVisualization is displayed, but instead I instantly set the Visibility to collpased. 我不确定这是否正确,因为我不会避免显示TagVisualization,而是立即将Visibility设置为collapsed。 I think there have to be better ways to do that? 我认为必须有更好的方法来做到这一点?

official guidance for how to do this is shown in one of the sdk samples: http://msdn.microsoft.com/en-us/library/ee804861(v=Surface.10).aspx sdk示例之一中显示了有关如何执行此操作的官方指南: http : //msdn.microsoft.com/zh-cn/library/ee804861( v=Surface.10) .aspx

-robert (former program manager for the surface dev platform) -robert(Surface开发平台的前程序经理)

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

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