J'essaye de faire fonctionner un échantillon en utilisant MVVM Light et la classe Messaging. Dans l'exemple, j'ai un projet de test créé à partir du modèle MVVM pour Silveright 4. J'ai ajouté un bouton sur la page principale. Lorsque le bouton est cliqué, il met à jour une propriété sur ViewModel. Lorsque la propriété est mise à jour, je souhaite afficher une boîte de message avec la nouvelle valeur.MVMLight Messaging et Silverlight
La ligne clé du code est:
Messenger.Default.Register (cela, une nouvelle action (ShowMessage));
Je peux obtenir cela pour travailler dans WPF, mais pas silverlight. Il doit appeler ShowMessage avec le paramètre string lorsque la propriété change, mais ce n'est pas le cas. Si j'utilise:
Messenger.Default.Send ("Hello MVVM");
Cela fonctionne et la chaîne est envoyée en tant que message à ShowMessage.
Cependant, le message ne soit pas envoyé si les changements de propriété, même si la propriété a été créé avec l'extrait MVVMINPC et a la ligne suivante:
RaisePropertyChanged (MyPropertyPropertyName, oldValue, valeur, true);
Ceci devrait avoir le même effet que Messager.Default.Send mais il semble être ignoré. ThePropertyChangedEvent est en effet levé, mais la partie messenger semble être déconnectée.
Est-ce que je fais quelque chose de mal? Voici la pleine MainViewModel:
public class MainViewModel : ViewModelBase
{
public RelayCommand MyRelayCommand { get; set; }
public const string MyPropertyPropertyName = "MyProperty";
private string _myProperty = "test";
public string MyProperty
{
get
{
return _myProperty;
}
set
{
if (_myProperty == value)
{
return;
}
var oldValue = _myProperty;
_myProperty = value;
RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);
}
}
public void DoSomething()
{
//Messenger.Default.Send("Hello MVVM"); //Works
this.MyProperty = "Hello World"; //Doesn't work.
}
public void ShowMessage(string message)
{
MessageBox.Show(message);
}
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
Messenger.Default.Register(this, new Action<string>(ShowMessage));
MyRelayCommand = new RelayCommand(new Action(DoSomething));
this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MainViewModel_PropertyChanged);
}
void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
MessageBox.Show(e.PropertyName);
}
}public class MainViewModel : ViewModelBase
{
public RelayCommand MyRelayCommand { get; set; }
public const string MyPropertyPropertyName = "MyProperty";
private string _myProperty = "test";
public string MyProperty
{
get
{
return _myProperty;
}
set
{
if (_myProperty == value)
{
return;
}
var oldValue = _myProperty;
_myProperty = value;
RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);
}
}
public void DoSomething()
{
//Messenger.Default.Send("Hello MVVM"); //Works
this.MyProperty = "Hello World"; //Doesn't work.
}
public void ShowMessage(string message)
{
MessageBox.Show(message);
}
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
Messenger.Default.Register(this, new Action<string>(ShowMessage));
MyRelayCommand = new RelayCommand(new Action(DoSomething));
this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MainViewModel_PropertyChanged);
}
void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
MessageBox.Show(e.PropertyName);
}
}v
Votre méthode ShowMessage peut prendre PropertyChangedMessageBase au lieu du paramètre le PropertyChangedMessage générique. En outre, vous pouvez utiliser une surcharge de la méthode Register de Messenger pour vous inscrire à tous les messages provenant de PropertyChangedMessageBase si vous le souhaitez. Messenger.Default.Register (this, true, ShowMessage); –
OK, merci Matt. –
Je ne sais pas pourquoi il était si difficile de trouver un exemple de la façon de procéder. Merci de l'avoir posté! – Aligned