使用 MVVM 模型处理 Silverlight 中的 OnLoad(已加载)

发布于 2024-09-25 00:03:51 字数 352 浏览 0 评论 0原文

我是 Silverlight(版本 4)和 MVVM 的新手,我似乎无法弄清楚如何将 XAML 中的命令绑定到我的 ViewModel 以实现 UserControl 的“Loaded”事件。我可以将命令绑定到这样的按钮...

<Button Command="{Binding ShowImageClick}" />

并且效果很好。但我不知道如何做类似的加载。我尝试了这个,但它抛出了一个异常,说“无法分配属性”......

<UserControl Loaded="{Binding WindowLoad}">

有什么想法吗?

I am new to Silverlight (version 4) and MVVM, and I can't seem to figure out how to bind a command in the XAML to my ViewModel for the "Loaded" event of a UserControl. I can bind a command to a button like this...

<Button Command="{Binding ShowImageClick}" />

And it works fine. But I have no idea how to do something similiar onload. I tried this but it threw an exception saying "Failed to assign property"...

<UserControl Loaded="{Binding WindowLoad}">

Any ideas?

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

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

发布评论

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

评论(6

许仙没带伞 2024-10-02 00:03:51

一种可能的方法是使用 此代码片段 我创建的目的是使用 附加行为

我希望这有帮助。

谢谢,
达米安

One possible approach could be using this code snippet I created to hook-up commands with events using Attached Behaviors.

I hope this helps.

Thanks,
Damian

请持续率性 2024-10-02 00:03:51

Codeplex 上的 Expression Blend 示例项目可能会有所帮助:

表达混合样本

例如:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <si:InvokeDataCommand Command="{Binding Command}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

The Expression Blend Samples project on Codeplex may be helpful:

Expression Blend Samples

e.g.:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <si:InvokeDataCommand Command="{Binding Command}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
赏烟花じ飞满天 2024-10-02 00:03:51

我很喜欢达米安的答案,并且通常会使用该解决方案。

另一种常见做法是 InvokeCommandAction 或类似行为在混合中。

I'm a fan of Damian's answer and would typically use that solution.

Another common practice is the InvokeCommandAction or similar behavior in Blend.

迟到的我 2024-10-02 00:03:51

不确定这是否是最佳实践,但在 ViewModel 类中简单地使用构造函数似乎对我来说已经足够好了......

namespace App.ViewModels
{
    public class Main : INotifyPropertyChanged
    {

        public Main()
        {
            // Onload code here
        }

Not sure if this is best practice or not, but simply having a constructor in the ViewModel class seems to work well enough for me...

namespace App.ViewModels
{
    public class Main : INotifyPropertyChanged
    {

        public Main()
        {
            // Onload code here
        }
演多会厌 2024-10-02 00:03:51

我刚刚发现这可能会导致内存泄漏,并已恢复为老式的 Loaded。要检查这一点,请将终结器添加到您的用户控件/页面,并确保在执行 GC.Collect() 时调用它。

I just found that can cause a memory leak and have reverted to old-school Loaded. To check this, add a finalizer to your user control/page and ensure it is called when you do a GC.Collect().

半步萧音过轻尘 2024-10-02 00:03:51

也许这不是正确的方法,但对我有用。

视图:

<UserControl Tag="{Binding InitializeMyUserControl}">

视图模型:

public object InitializeMyUserControl
{
    get
    {
        // do some initialization in here
        // bla bla bla
        .. 

        return null;
    }
}

当 UserControl 加载时,它将尝试获取标记值。在那里你可以初始化一些东西。

Maybe this helps its not the proper way but it works for me.

View:

<UserControl Tag="{Binding InitializeMyUserControl}">

View Model:

public object InitializeMyUserControl
{
    get
    {
        // do some initialization in here
        // bla bla bla
        .. 

        return null;
    }
}

when UserControl loads it will try to get the tag value. In there you can initialize things.

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