Home > C#, Universal Windows Platform > Two hidden gems in the Universal Windows Platform

Two hidden gems in the Universal Windows Platform

22/05/2017

Even if the Creators Update version of Windows 10 has been released a couple of month ago and we’re waiting for the next Fall Creators Update, there is still room to talk about the Anniversary Edition, because it contains a couple of gems that not many people know about.

Raise your hand if you never had a BooleanToVisibilityConverter class in any of your apps. It is necessary because the Visibility property of UIElement is an Enum, but tipically, in particular when we’re following the MVVM pattern, we just have a Boolean property that we want to use to control the visibility of an object. In this case, we could add a BooleanToVisibilityConverter class to our app, like the following one:

public sealed class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
        string language)
    {
        return (value is bool && (bool)value) 
            ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
        string language)
    {
        return value is Visibility && (Visibility)value == Visibility.Visible;
    }
}

Of course, there could be other implementations, or we could use an attached property instead of a Converter, but the scenario remains the same.

Starting from Windows 10, version 1607 (i.e., the Anniversary Edition) this is no longer necessary. In fact, if we use the {x:Bind} markup extension (that was first introduced in the initial release of Windows 10), we can cast a Boolean property to a Visibility value directly in the XAML defition:

<Image Source="Assets/Favorite.png" 
       Visibility="{x:Bind (Visibility)ViewModel.IsFavorited}" />

The meaning of this declaration is straightforward: if ViewModel.IsFavorited is true, it returns Visibility.Visible, otherwise Visibility.Collapsed. In this way, we do not need any extra class anymore.

But there is another cool feature that helps us solving a common problem. If we’re developing a “chat-like” app, we tipically have a ListView in which we add items to the bottom. In this scenario, we often have the requirement to automatically scroll the ListView as new items are inserted, so that the last ones are always visible. To obtain this, we could add a ScroolToBottom Behavior class to our app.

Again, with the Anniversary Edition we can set the ItemsUpdatingScrollMode property of the ItemsStackPanel to KeepLastItemInView, that adjusts the scroll offset to keep the last visible item showing at the bottom of the view. In other words, we just need to set the ItemsPanelTemplate of the ListView in this way:

<ListView ItemsSource="{x:Bind ViewModel.Samples}">
    ...
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <ItemsStackPanel ItemsUpdatingScrollMode="KeepLastItemInView" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

And the list will automatically scroll to bottom as new items are added to it. No code at all is needed to implement this behavior.

You can download a sample of these features using the link below:
Anniversay Update Cool Features

Advertisements
Comments are closed.
%d bloggers like this: