General guidance for making UWP-only code Uno compatible

This article explains adjustments that may need to be made to UWP-only code for it to run on Uno Platform, be it in an application or a class library.

Code adjustments

Add 'partial' to some class definitions

Certain class definitions will need to have the partial keyword added. This is because Uno generates additional code at compile-time to support them properly.

You'll need to do this for:

  • types that inherit from FrameworkElement, directly or indirectly
  • types that inherit directly from DependencyObject

Apart from adding partial, you don't need to worry about the generated code. You may however get misleading errors from Intellisense until the first time you try to compile the project, because the generated partial classes haven't been added yet.

Avoid nested classes that inherit from FrameworkElement

Classes that inherit from FrameworkElement directly or indirectly can't be nested inside another class; it's been observed to cause problems on Xamarin.iOS. If you have any nested FrameworkElement-derived classes, you'll need to refactor them to be top-level classes.

Disambiguate naming collisions with native properties

This is relevant if you're targeting Android, iOS, and/or macOS, where Uno views (hence, FrameworkElement) inherit from the native view type. In some cases you may find that a reference to a type from the UWP framework is confused with a native property. The fix in this case is generally to disambiguate by supplying the full namespace of the type.

Some common cases:

  • on iOS, inside a control definition, references to Window.Current will be confused with the UIView.Window property. The fix is to fully qualify this as Windows.UI.Xaml.Current.
  • on Android, inside a control definition, references to the TextAlignment enum will be confused with the View.TextAlignment property. The fix, again, is to fully qualify the reference as Windows.UI.Xaml.TextAlignment.

What do I do if I have a nested namespace with Windows in it?

If, for example, your control is defined in the CoolControls namespace, and you've also defined a CoolControls.Windows namespace, then the above will give a compilation error. You'll need to use the global keyword, eg global::Windows.UI.Xaml.Window.Current.

Adjust for unsupported runtime features

Not all .NET runtime features are supported on every platform. See here for more details.