Regarding AdventureWorks.Shopper - SessionStateService.RegisterKnownType?

May 17, 2015 at 8:22 PM
Edited May 17, 2015 at 9:19 PM
Hi,

I'm Serializing a ObservableCollection:
private ObservableCollection<Models.WorkSenario> workingSenarios;
public ObservableCollection<Models.WorkSenario> _WorkingSenarios
{
    get { return this.workingSenarios; }
    set
    {
        SetProperty(ref this.workingSenarios, value);
    }
}
...my class:
[System.Runtime.Serialization.DataContract(Name = "WorkSenario")]
public class WorkSenario
{
    public Models.WritingMenu WorkingSenario { get; set; }
    public int MainViewId { get; set; }
    public CoreDispatcher MainDispatcher { get; set; }
    public CoreDispatcher Dispatcher { get; set; }
    public CoreWindow Window { get; set; }
    [System.Runtime.Serialization.DataMember]
    public string Title { get; set; }
    public int ViewId { get; set; }
    [System.Runtime.Serialization.DataMember]
    public bool Released { get; set; }
    [System.Runtime.Serialization.DataMember]
    public bool Consolidated { get; set; }
}
...using:
/// <summary>Serializes an object and write to file in specified storage strategy</summary>
/// <typeparam name="T">Specified type of object to serialize</typeparam>
/// <param name="key">Path to the file in storage</param>
/// <param name="value">Instance of object to be serialized and written</param>
/// <param name="location">Location storage strategy</param>
public static async Task<bool> WriteFileAsync<T>(string key, T value, StorageStrategies location = StorageStrategies.Local)
{
    // create file
    var _File = await CreateFileAsync(key, location, Windows.Storage.CreationCollisionOption.ReplaceExisting);
    // convert to string
    var _String = Serialize(value);
    // save string to file
    await Windows.Storage.FileIO.WriteTextAsync(_File, _String);
    // result
    return await FileExistsAsync(key, location);
}
..and was wondering if this does the same thing:
protected override void OnRegisterKnownTypesForSerialization()
{
    // Set up the list of known types for the SuspensionManager
    .
    ..
    ...
    SessionStateService.RegisterKnownType(typeof(Product));
    SessionStateService.RegisterKnownType(typeof(Collection<Product>));
}
...for the class:
public class Product
{
    public string ProductNumber { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public Uri ImageUri { get; set; }
    public int SubcategoryId { get; set; }
    public double ListPrice { get; set; }
    public double DiscountPercentage { get; set; }
    public double Weight { get; set; }
    public string Color { get; set; }
    public string Currency { get; set; }
}
..without using a DataContract, because I want to use all of Prism's built in capabilities before using standard coding. Secondly) would explain how this works please?

I'm guessing this is how it works?:
public class SearchResultsPageViewModel : ViewModel
{
    .
    ..
    ...
    
    [RestorableState]
    public static Collection<Product> PreviousResults { get; private set; }

    .
    ..
    ...

    public async override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
    {
        base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
        var queryText = navigationParameter as String;
        string errorMessage = string.Empty;
        this.SearchTerm = queryText;
        this.QueryText = '\u201c' + queryText + '\u201d';

        try
        {
            Collection<Product> products;
            if (queryText == PreviousSearchTerm)
            {
                products = PreviousResults;
            }
            ...
            ..
            .
Thanks!...
May 17, 2015 at 9:35 PM
Short answer - SessionStateService uses DataContractSerializer under the covers, so it will honor your DataContract/DataMember annotations if present, if not it treats it as an implicit data contract and serializes all the public properties - this behavior has been in DataContractSerializer since .NET 3.5 SP1.

You might want to check out my article here: http://www.silverlightshow.net/items/WinRT-Business-Apps-with-Prism-App-State-Management.aspx for more background, as well as the docs: https://msdn.microsoft.com/en-us/library/windows/apps/xx130647.aspx
Brian
May 18, 2015 at 6:09 AM
Cool! Thanks for your response! I check out the articles too!