Is float type supported in uwp xaml?

Viewed 529

I'm converting C# WPF component to UWP and it contains classes with float type properties.

When trying to set value in xaml, Visual Studio xaml editor complains: "Unable to cast object of type 'System.Double' to type 'System.Single'. When running: can't convert to Windows.Foundation.Single.

Doubles work fine.

UWP ScrollViewer has e.g. MinZoomFactor property, which is float and it produces no problems.

Also UIElement has Rotation property, which is float.

So one would guess that float should be possible, but how, if at all?

Read some documentation, which indicates that float is not supported, which is a bit strange as per ScrollViewer and UIElement.

https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/xaml-cust-ctrl

This is from test project how float property is defined.

public static DependencyProperty ValueProperty =
    DependencyProperty.Register(
        "Value",
        typeof(float),
        typeof(FloatContainer),
        new PropertyMetadata(0.0f, new PropertyChangedCallback(ValueChanged)));

public float Value
{
    get { return (float)GetValue(ValueProperty); }
    set { SetValue(ValueProperty, value); }
}
1 Answers

Is float type supported in uwp xaml?

Now, the source code of Scrollviewer is available in the GitHub . It seems that ScrollViewer has an unique mechanism when handling the float and dobule type for its properties.

As mentioned in your case.

If you want a DP with a floating-point type, then make it double (Double in MIDL 3.0). Declaring and implementing a DP of type float (Single in MIDL), and then setting a value for that DP in XAML markup, results in the error Failed to create a 'Windows.Foundation.Single' from the text ''.

We still suggest use Double dependency property.

Related