Epic Build 2018 Playlists

Your one-stop shop for Microsoft Build 2018 sessions, slide decks, post links, keynotes and much more.

Living Document Note:

These Playlists will continue to be reordered and updated in real time as new content becomes available. However, the links will always remain the same and you can refresh the playlist page to get any updated ordering/content.

Advertisements

Migrating from PCLStorage to .NET Standard 2.0

If you’re a Xamarin Forms developer, you’ve likely used PCLStorage (or other Dependency Service) to interact with the target platform’s file system in the portable class library’s code. However, since November 2017, Xamarin.Forms now uses a .NET Standard 2.0 class library and PCLStorage is no longer supported.

System.IO.File

This isn’t a problem because in .NET Core 2.0 you now have access to System.IO.File’s GetFolderPath and SpecialFolder methods (see System.IO.File in .NET Core 2.0 docs).

Thus, you can get a path to the app’s local folder (the one that the app can save to) without having to write a Dependency Service (which is what PCLStorage does) by using:

var localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

With a reference to the localFolder path, you can Combine it with the file name:

var notes = File.ReadAllText(Path.Combine(LocalFolder, "notes.txt"));

Functional Demo

As a very simple example, I created a couple extension methods for Stream and Byte[] in the below FileExtensions class.

To test it, I created a ContentPage that downloads an image, saves it using the extension method and set a FileImageSource to confirm that it’s a file (instead of just using a StreamImageSource).

Note that the extension methods are very basic and shouldn’t be used in production as-is (i.e. no defensive programming code).

Here is the result at runtime on UWP:

2018-04-19_1134

Custom Themes in UI for Xamarin

Telerik UI for Xamarin comes with two themes out of the box, Default and Blue. You can easily style individual items, but you can also define an entire theme at once in a separate ResourceDictionary.

With this approach you can swap out ResourceDictionaries at runtime for a nice user-selected theme feature in your app’s settings. Let me walk you through a very simple demo to illustrate the approach.

Step 1. Create your custom theme’s ResourceDictionary

Xamarin.Forms doesn’t have a good ResourceDictionary template, so start with a Content Page (XAML) template and then change the ContentPage type to “ResourceDictionary” in both the XAML and code-behind.

Now, you can add in all the styles and colors you want in that one dictionary. You can find the Color names we use for your controls in the Themes Overview documentation.

In the custom dark theme example below, the Color resource key values are pretty clear as to what they do.

Step 2. ThemeHelper Class

I usually like to create a static class like this so that I can change the theme from anywhere in the app, but isn’t necessary. You can put this logic entirely in your settings page if you’d prefer.

You may have noticed that I’m using a RadResourceDictionary. We created this when Xamarin.Forms didn’t have MergeWith support. You can use a Xamarin.Forms ResourceDictionary type if you’re using a newer version that supports it.

Step 3. Runtime

I’m using a RadSegmentedControl to change the theme, and a RadListView to easily see the difference between themes. You can of course use whatever UI control you’d like to change themes, for example a Picker.

The important thing is that you call the helper class’s ChangeTheme method and pass the theme name that you need to change to:

That’s it!

When selecting one of the options, you’ll get one of the three results you see in this article’s header image.

If you have any questions feel free to reach out to me on Twitter at @lancewmccarthy.