MvvmLight RaisedPropertyChanged does not broadcast a PropertyChangedMessage if oldValue is null?

StackOverflow https://stackoverflow.com/questions/22234558

  •  10-06-2023
  •  | 
  •  

Question

  • .NET Framework version 4.5.51641
  • GalaSoft.MvvmLight.WP8 version 4.2.30.23483
  • GalaSoft.MvvmLight.Extras.WP8 version 4.2.30.23483

The issue is RaisedPropertyChanged does not broadcast a PropertyChangedMessage if oldValue is null.

In my MainViewModel.cs, I have the following property:

public Headline NewsHeadline
    {
        get
        {
            return _newsHeadline;
        }

        set
        {
            if (_newsHeadline == value)
            {
                return;
            }

            var oldValue = _newsHeadline;
            _newsHeadline = value;
            RaisePropertyChanged(NewsHeadlinePropertyName, oldValue, value, true);
        }
    }

In my DetailViewModel.cs, I have the following statement:

Messenger.Default.Register<PropertyChangedMessage<Headline>>
            (
            this,
            (action) => DispatcherHelper.CheckBeginInvokeOnUI(() => this.SelectedNewsHeadline = action.NewValue)
            );

In MainViewModel.cs. When a user taps on an item in the ListBox, the selected item is to the NewsHeadline property - the first time a user taps on an item, the oldValue is null; however any subsequent item selection from the ListBox, the RaisePropertyChanged does broadcast a PropertyChangedMessage.

How do I broadcast a PropertyChangedMessage from RaisedPropertyChanged if oldValue is null?

Thanks!

Était-ce utile?

La solution

The problem is probably that the message is sent before the DetailViewModel is created, and consecuently the message registered.

You can check if this is the behavior by setting breakpoints on both, sending and receiving the message. If the message is sent but not received, that's probably the scenario.

Onew way to solve this, is by forcing the app to create the DetailViewModel at the beginning of the execution, instead of waiting until the first time it is used.

For that, go to your ViewModelLocator.cs file and in the command where you register the DetailViewModel, add true parameter. That is, replace:

SimpleIoc.Default.Register<DetailViewModel>();

with:

SimpleIoc.Default.Register<DetailViewModel>(true);

This will force the DetailViewModel to be created immediately, and therefore the message will be registered.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top