Csharp/C#教程:Silverlight 3.0 – 如何从UserControl访问MainPage控件值分享


Silverlight 3.0 – 如何从UserControl访问MainPage控件值

我需要从MainPage检索一些控件值到UserControl。 在这个UserControl中,我需要能够获得Frame.ActualWidth和Frame.ActualHeight值(在这种情况下,Frame元素在MainPage中,UserControl通过xaml加载到MainPage的Grid中)。 有人有样品吗? 谢谢

Josimari Martarelli ESLSistemasLogísticosSilverlightUI设计

jmartarelli@logfacil.com.br

MainPage m =(MainPage)Application.Current.RootVisual;

在像这样的实例中,我经常使用我的MainPage类对其自身的实例公开静态引用。 我将它在构造函数中设置为“this”,然后当我需要从用户控件中向下访问MainPage时,我将调用类似于:

MainPage.Instance。

首先,我在App类中创建了一个静态方法,该方法遍历父项的层次结构,直到找到基于名称的匹配项。 这不仅可以用于MainPage。 层次结构中的所有内容都应该从FrameworkElement类派生。

public static FrameworkElement GetParentByName(FrameworkElement currentPage, string ParentName) { FrameworkElement fe = (FrameworkElement)currentPage.Parent; // Walk your way up the chain of Parents until we get a match while(fe.GetType().Name != ParentName) fe = (FrameworkElement)fe.Parent; return fe; } 

然后使用这个我只是调用类似的东西:

MainPage m =(MainPage)App.GetParentByName(this,“MainPage”);

如果您使用的是登录页面,则必须使用登录页面进行投射。

Login lp = (Login)Application.Current.RootVisual;
您可以使用在登录页面中创建的参数。

上述就是C#学习教程:Silverlight 3.0 – 如何从UserControl访问MainPage控件值分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1013068.html

(0)
上一篇 2021年12月30日
下一篇 2021年12月30日

精彩推荐