如何避免在 MVVM 上下文中等待 Silverlight 模板化控件初始化

发布于 2024-09-24 22:37:58 字数 398 浏览 8 评论 0原文

我正在尝试在应用程序中制作和使用模板化控件,但我似乎遇到了计时问题。当我使用自定义模板化控件时,在 XAML 中并通过代码 (new MyControl()),并调用控件上的一个方法,该方法尝试对Controltemplate,模板中的控件几乎总是为 null,即使在 OnApplyTemplate 方法之后也是如此。一种解决方案是等待控件初始化,使用 OnApplyTemplate 方法中引发的事件,然后让代码对模板中的控件执行某些操作,但这似乎复制了我不必担心的控件/框架功能关于。

所以问题是:

从 Themes\Generic.xaml 加载 ControlTemplate 是异步的吗? 如何在 MVVM 上下文中使用这些多级模板化控件?

I'm trying to make and use templated controls in an application but I seem to run into timing isssues. When I use a custom templated control, in XAML <local:MyControl> and through code (new MyControl()), and call a method on the control that tries to do something with a control in the Controltemplate, a control in the template is almost always null even after the OnApplyTemplate method. One solution is to wait for the control to initialize, using an event thrown in the OnApplyTemplate method, and then let the code do something on the controls in the template but this seems to replicate control/framework features that I shouldn't have to worry about.

So the questions are:

Is the loading of the ControlTemplate from the Themes\Generic.xaml asynchronous?
How do I use these multiple level templated controls in a MVVM context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你拒绝所有暧昧 2024-10-01 22:37:58

在 OnApplyTemplate 中,您需要使用 GetTemplateChild 方法来访问控件。有关以下内容的说明,请参阅此处控制生命周期。

In OnApplyTemplate you need to use the GetTemplateChild method to get to the controls. See here for a description of the control lifecycle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文