Click here to Skip to main content
14,933,534 members
Articles / Mobile Apps / Xamarin
Article
Posted 4 Apr 2017

Stats

7.4K views
1 bookmarked

Getting Started with Codon - Part 2

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
4 Apr 2017CPOL2 min read
Storing and retrieving settings with the cross-platform Codon framework's settings service.

Codon is a zero-dependency cross-platform MVVM framework for creating UWP, WPF, and Xamarin based applications. It provides much of what you need to rapidly create sophisticated yet maintainable applications.

Introduction

In the previous article we familiarised ourselves with view-model initialization and working with properties and commands. In this article, you see how to save and retrieve persistent data using the settings service.

The code presented herein is located in Sample001 in the Samples repository

Just about every app needs to store and retrieve settings. Codon provides an abstracted API for storing and retrieving settings via its ISettingsService implementations. You may wonder why it might be a good idea to abstract a set of APIs that is seemingly present across all supported platforms. There are a couple of reasons. While there exists an Isolated Storage Settings API in .NET Standard, the API doesn’t behave as you might expect across all platforms. In particular, in the WPF implementation IsolatedStorageSettings.ApplicationSettings is null. Codon works around this issue with a custom implementation supporting WPF *.

Another reason for abstracting the settings API is that it affords you the opportunity to swap out the underlying storage provider with, for example, an SQLite implementation. I did this for one of my apps when users started reporting settings being reset and I realised a more robust storage provider was required.

* See the IsolatedStorageSettingsWpf class in the Codon.Platform.Shared project if you’re interested in the custom WPF IsolatedStorageSettings implementation.

NOTE: The settings service is able to serialize and store any object or primitive. Binary serialization is used for complex types.

Demonstrating the Settings Service

The Page1ViewModel class’s UpdateCreationCount method is called whenever the view-model is instantiated. See Listing 1. The method retrieves the setting with the key Page1CreationCount along with a default value. If no such setting exists, then the default value is returned.

The setting is then incremented via ISettingsService.SetSetting. The view-model’s CreationCount property is set to the setting value.

Listing 1. Page1ViewModel.UpdateCreationCount Method

C#
void UpdateCreationCount()
{
	const string key = "Page1CreationCount";
	/* Use the settings service to retrieve, increment, 
	 * and store a counter. 
	 * The settings service is cross-platform. */
	int count = settingsService.GetSetting(key, 0);
	settingsService.SetSetting(key, ++count);

	CreationCount = count;
}

We bind the CreationCount property to a text field in UWP project, like so:

XML
<TextBlock Text="{x:Bind ViewModel.CreationCount, Mode=OneWay}" />

In the WPF project, it is bound in a similar manner (without using x:Bind):

XML
<TextBlock Text="{Binding CreationCount}" />

While in the Android project, we do this:

XML
<TextView
        l:Binding="Target=Text, Path=CreationCount"
        android:id="@+id/Page1_TextView_ShownCount" ... />

NOTE: Codon’s Android data-binding infrastructure requires that views with data-bindings have an android:id attribute.

Conclusion

In this article, you saw how to save and retrieve persistent data using the settings service. In the next part, we look at passing messages between app components.

I hope you find this project useful. If so, then please rate it and/or leave feedback below.

History

April 2 2017

  • First published

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Daniel Vaughan
Engineer Microsoft
United States United States
Daniel Vaughan is a Senior Software Engineer at Microsoft.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions

 
-- There are no messages in this forum --