简体   繁体   English

WPF相当于Silverlight“RootVisual”

[英]WPF equivalent to Silverlight “RootVisual”

I am trying to port an application from silverlight to wpf. 我正在尝试将应用程序从silverlight移植到wpf。 Unfortunatley I am new to both. 不幸的是,我是两个人的新手。 Is there an equvivalent to the following Silverlight code in WPF? 是否与WPF中的以下Silverlight代码等效?

        private static Canvas GetCanvas()
        {
            var uc = Application.Current.RootVisual as UserControl;
            if (uc == null)
            {
                return null;
            }
            return uc.FindName("ChoiceCanvas") as Canvas;
        }

Currently I am using 目前我正在使用

Application.Current.MainWindow.FindName("ChoiceCanvas") as Canvas;

But this doesn't work, perhaps because ChoiceCanvas is something located in a UserControl and not in the MainWindow? 但这不起作用,也许是因为ChoiceCanvas位于UserControl而不是MainWindow中?

There is no RootVisual property in WPF. WPF中没有RootVisual属性。 As far as I understand, the "Window" is the "root". 据我所知,“窗口”是“根”。 You can get the Window that any WPF (DO) object belongs to by running the static method Window myWindow = Window.GetWindow (myControl); 您可以通过运行静态方法Window myWindow = Window.GetWindow (myControl)来获取任何WPF(DO)对象所属的Window。

FindName won't work becuase the Canvas exists in the namescope of the UserControl, try using the LogicalTreeHelper instead. FindName不起作用,因为Canvas存在于UserControl的名称范围内,请尝试使用LogicalTreeHelper。

 var canvas = LogicalTreeHelper.FindLogicalNode(
      Application.Current.MainWindow, "ChoiceCanvas") as Canvas; 

The current window is the root visual. 当前窗口是根视觉。

From MSDN WPF Graphics Rendering Overview : 来自MSDN WPF图形渲染概述

The root visual is the top-most element in a visual tree hierarchy. 根视觉是可视树层次结构中最顶层的元素。 In most applications, the base class of the root visual is either Window or NavigationWindow. 在大多数应用程序中,根视觉的基类是Window或NavigationWindow。 However, if you were hosting visual objects in a Win32 application, the root visual would be the top-most visual you were hosting in the Win32 window. 但是,如果您在Win32应用程序中托管可视对象,则根视觉将是您在Win32窗口中托管的最顶级可视对象。 For more information, see Tutorial: Hosting Visual Objects in a Win32 Application. 有关更多信息,请参阅教程:在Win32应用程序中托管可视对象。

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

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