Windows UWP Version Adaptive Code

UWP applications are the constant recipients of new functionality. This is, obviously, a good thing for those of us who’d like to build a meaningful user experience a la WPF or WinForms.

My first experience with writing Version Adaptive Code comes as a result of trying to remove the super-annoying selection flyout in the RichEditBox (which, unsurprisingly, I use for editing in eclecdec). This appeared in the Windows 10 October 2018 Update (1809), and has been driving me crazy, because I already provide the formatting options in the eclecdec ribbon, and I find it gets in my way and disturbs my flow:

Annoying Flyout

Right. So it appeared in 1809, and you can turn if off in 1809 (or, more specifically, API 17763) by setting the SelectionFlyout property of the RichEditBox to null. Works perfectly.

The problem comes as a result of supporting a Windows 10 version earlier than 1809 (1709 is my minimum version), where the RichEditBox doesn’t have that SelectionFlyout property. The solution is Version Adaptive Code.

Put simply, you can query if method, properties, events, etc., are present on a type before trying make use of the method, property, event, etc. It’s pretty comprehensive. Follow the above link for a list of all of the things you can query for.

Anyway, In my case, my code to turn off the selection flyout looks like this:

if (ApiInformation.IsPropertyPresent(
"Windows.UI.Xaml.Controls.RichEditBox",
"SelectionFlyout"))
{
RichTextBox.SelectionFlyout = null;
}

Basically, I’m able to test whether the property SelectionFlyout is available on the RichEditBox. If it is, I set it to null and voila, the selection flyout no longer occurs. If it isn’t available, I do nothing, which prevents anything from blowing up in earlier Windows versions.

A final note, in case it isn’t obvious: for this to work, you must be targeting the Windows version that does contain the member you want to make use of. The ApiInformation check simply prevents you from making use of the member on a version of the OS that doesn’t support .

Leave a Reply

Your email address will not be published. Required fields are marked *