Xamarin.forms: [Bug] x:DataType do NOT accept markup extensions

Created on 25 Jun 2019  路  9Comments  路  Source: xamarin/Xamarin.Forms

Description

After updating to latest preview 4.1.0.496342-pre2 from 4.0.0.425677, x:DataType do not allow anything but string, which means {x:Null} is not allowed either. I have to disable compile binding here since the target type is a generic one.

Steps to Reproduce

  1. Simply set an x:DataType to anything with markup.

Expected Behavior

Building without errors.

Actual Behavior

Complaining "x:DataType expects a string literal".

Basic Information

  • Version with issue: 4.1.0.496342-pre2
  • Last known good version: 4.0.0.425677
  • IDE: Visual Studio 2019
  • Platform Target Frameworks:

    • UWP: 17763

    • WPF .Net Framework 4.7.2

  • Nuget Packages: Xamarin.Forms

Screenshots

Annotation 2019-06-25 121951

Reproduction Link

Xaml </> 3 high regression high impact bug

Most helpful comment

This is a non-sense. Is there a reason, why you switched to not accepting {x:Type} markup extension? We use it in all other XAML platforms (WPF, WP8, UWP), do you know?

Well regarding {x:Null} issue:
It occurs ONLY if you try to redefine x:DataType inside a DataTemplate:

    <StackLayout BindingContext="{Binding ViewModel, Source={x:Reference Root}}">
        <ListView>
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="app3:ViewModel">
                    <!--  Try to disable compiled binding  -->
                    <Label x:DataType="{x:Null}" Text="{Binding Test}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

Please switch back to accepting {x:Type} as x:DataType.

All 9 comments

This was presumably introduced here:
https://github.com/xamarin/Xamarin.Forms/pull/5335

This was presumably introduced here:

5335

However, according to Microsoft Docs introducing to Compiled Binding:

Alternatively, the x:DataType attribute can be re-defined at any point in a view hierarchy to null using the x:Null markup extension.

So {x:Null} is a valid value for x:DataType.

I agree. I wasn't saying the change was correct. The intention was to block {x:Type SomeType}. I'm now curious whether any other value might be reasonable besides a string or {x:Null}.

@lekia Can you please attach a small project that demonstrates this issue? Thanks!

Same issue for me when upgrading to XF4.1 (x:DataType expects a string literal).
This happens when I'm re-defining the data-type, using x:DataType="{x:Null}".

Strangely, I'm also having errors with a few valid data types: <DataTemplate x:DataType="{x:Type models:MyModel}">
If I modify this to <DataTemplate x:DataType="models:MyModel">, it builds, but intellisense does not work anymore for this type.

This is a non-sense. Is there a reason, why you switched to not accepting {x:Type} markup extension? We use it in all other XAML platforms (WPF, WP8, UWP), do you know?

Well regarding {x:Null} issue:
It occurs ONLY if you try to redefine x:DataType inside a DataTemplate:

    <StackLayout BindingContext="{Binding ViewModel, Source={x:Reference Root}}">
        <ListView>
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="app3:ViewModel">
                    <!--  Try to disable compiled binding  -->
                    <Label x:DataType="{x:Null}" Text="{Binding Test}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

Please switch back to accepting {x:Type} as x:DataType.

@samhouts

I'm also seeing this issue. This change has broken a sample/doc, when we've specifically called out the ability to mix compiled bindings with classic bindings by setting x:DataType="{x:Null}".

Sample project demoing this error:

DataBindingDemos.zip

Got it! Thanks!

Was this page helpful?
0 / 5 - 0 ratings