The UWP Notepad Project (Redux): User Settings (Premium)

Remember how easy it was to create user and application settings in the WinForms and WPF versions of .NETpad? Well, forget all about that, because it doesn’t work the same way in UWP: There’s no visual designer, and we have to implement settings manually in C# code. This isn’t horrible, but it does introduce some complexities. For example, what happens the first time we run the app and those settings don’t even exist yet?

To find out, let’s start with the font settings.

UWP’s support for fonts is very similar to that in WPF, which is nice because there’s less of a learning curve. (Moving from WinForms to WPF, by comparison, was difficult in this regard.) As with the WPF version of the app, we’ll keep track of the font family (what most would think of as “the font”) and the font size chosen by the user, plus whether it is bold and/or italic; so the font-related user settings we’ll create and interact with will include MyFontFamily, MyFontSize, MyFontBold, and MyFontItalic. We’ll load the associated user settings when the application first runs, and we’ll save them when the user changes any font configuration. Ideally, we’d also save them when the application closes, as we do in the WinForms and WPF versions of the app, but UWP makes that difficult.

To get started, open MainPage.xaml and scroll to the top. Locate the Page tag right at the top (it starts with <Page), and place the cursor somewhere within it. Then, switch the Properties pane to the event handlers list. And then double-click next to Loading to display a new, blank Page_Loading event handler in MainPage.xaml.cs.

Add the following line(s) of code inside this event handler:
// Read settings
The ReadSettings method doesn’t exist yet, so you’ll see a red squiggly line under it. That’s OK: We’re going to create it now and place all of the code we need to read user settings within it.

Below the Page_Loading event handler, create an empty ReadSettings method by adding the following code:
private void ReadSettings()

Now, add the following code inside of this method:
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;

string fontName = settings.Values["MyFontFamily"].ToString();
TextBox1.FontFamily = new FontFamily(fontName);
The ApplicationDataContainer class is used to create a container for the app’s settings, and it will have a red squiggly line under it, indicating that we need to add the Windows.Storage namespace to the C# file’s using section. I’ll assume you know how to do this now and will not usually mention this kind of thing going forward.

The next two lines should be familiar if you followed along with the WPF version of the app: We read the setting MyFontFamily and apply it to TextBox1’s FontFamily property, in effect changing the font displayed in the text box.

Well, theoretically. If you run the app now, you’ll run into a little problem: It throws an...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC