@DaisyTian-MSFT It is correct that the Microsoft.Toolkit.Mvvm is essentially UWP; however, it can also be used in a WPF project which uses host-standard-control-with-xaml-islands.
I am using it because the previously employed www.mvvmlight.net is depreciated.
In my viewmodels there are collections of model objects:
public class ResearchObjectsViewModel : Observable
{
public ResearchObjectsViewModel()
{
InitialiseView();
}
ObservableCollection<SearchObject> searchObjects = new ObservableCollection<SearchObject>();
public ObservableCollection<SearchObject> SearchObjects
{
get => searchObjects;
set
{
searchObjects = value;
this.OnPropertyChanged(nameof(this.SearchObjects));
}
}
.....
}
and the model objects themselves are also observable:
public class SearchObject : ObservableRecipient
{
public SearchObject()
{
}
.....
}
The data held in the models' properties may be downloaded, or entered by the user. When the app is shut down, the data are lost, so some form of serialisation is necessary in order to store the data to disk.
In order to use any existing functionality for doing this, such as how-to-serialize-and-deserialize-json-data an attribute must be applied to the class to be serialised. If [DataContract] is applied to SearchObject then it is disallowed, since the base class ObservableRecipient has no attributes. The same problem occurs with the observable collections - and in the past this was circumvented via subclassing, allowing the [Serializable] to be applied:
[Serializable]
public class SObservableCollection<T> : ObservableCollection<T>, INotifyPropertyChanged
{
....
}
I am about to implement the same process to the ObservableRecipient class, and just wanted to check if this was not way off base (pardon the pun).
If it were not possible to do, then it will be a nightmare to custom serialise without using the Microsoft standard serialisers.
Edit - I seem to have posted this as an answer, rather than a reply to DaisyTian-MSFT. Apologies.
Update - It does not seem possible to subclass the ObservableRecipient with serialisation attributes. I'm now going to try inheriting from Observable for the models, as do viewmodels. It strikes me as strange, though that the Community would release this toolkit which seems to effectively remove any way to use Microsoft serialisation. What good is that?