Silverlight 3 LoadedEvent 路由到 ViewModel
我的客户正在尝试挂钩视图模型中的用户控件加载事件。基本上他们想知道控件加载事件何时在视图模型内触发。他们正在寻找一种无需 Xaml 背后的代码即可完成此操作的方法。这是否可行。我正在研究是否可以将加载的事件路由到 xaml 中的视图模型。
My client is trying to hook to a usercontrols Loaded Event in the View Model. Basically they want to know when the controls loaded event triggers inside the view model. They are looking for a way to do it without code behind the xaml. Is this even feasible. I am looking into whether I can route the loaded event to the viewmodel in the xaml.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是使用 InvokeDataCommand。您可以将触发器的 EventName 指定为 Loaded,然后在触发 Loaded 事件时执行您的命令(在虚拟机中定义)。
One way of doing it is to use InvokeDataCommand. You'd specify the trigger's EventName as Loaded, and then your command (defined in your VM) would execute when Loaded event is fired.
您需要研究命令。与 WPF 相比,Silverlight 支持相当弱,但它确实包含 ICommand 接口。您可以扩展控件以赋予它们命令属性或通过附加属性实现它们。一旦 UI 中发生操作,这些命令基本上就会调用自身。它们完全独立于 UI 的构建方式(或者至少应该如此),因此可以进行完全的单元测试。
You need to look into commanding. Silverlight support is fairly weak compared to WPF but it does contain the ICommand interface. You can extend controls to give them command properties or implement them via an attached property. The commands basically invoke themselves once an action has occurred in the UI. They are totally independent of how the UI is built (or at least they should be) and so can be completely unit tested.