OnLoad handling (loading) in Silverlight using the MVVM model

I'm new to Silverlight (version 4) and MVVM, and I can't figure out how to associate a command in XAML with my ViewModel for the Loaded event in UserControl. I can bind a command to such a button ...

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

And it works great. But I do not know how to do something similar onload. I tried this, but he threw an exception: "Failed to assign property" ...

<UserControl Loaded="{Binding WindowLoad}">

Any ideas?

+3
source share
6 answers

, , .

, .

, Damian

+2

Samples Expression Blend Samples:

:.

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

, , ViewModel ...

namespace App.ViewModels
{
    public class Main : INotifyPropertyChanged
    {

        public Main()
        {
            // Onload code here
        }
0

I just found what could cause a memory leak and go back to the old Loaded school. To test this, add a finalizer to your user control / page and make sure it is called when you execute GC.Collect ().

0
source

It may not help in the right direction, 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 loading UserControl it will try to get the tag value. There you can initialize things.

0
source

Source: https://habr.com/ru/post/1766460/


All Articles