Xamarin.forms: Autocompletion for view model properties does not work for DataTemplates with specified x:DataType

Created on 13 Jun 2018  路  22Comments  路  Source: xamarin/Xamarin.Forms

Description

With latest update of visual studio(15.7.X) autocompletion for view model properties stopped working in DataTemplates with x:DataType specified.

Steps to Reproduce

  1. Create any DataTemplate with valid view model type specified in _x:DataType_
  2. Try to add element with binding, type

Expected Behavior

After typing Binding in such data template, ViewModel properties should be shown like intellisense

My proposal:
Make _x:DataType_ enable autocompletion for ViewModel properties for all XAML elements, or at least top element like ContentPage, it would make development experience much better, now it's necessary to create design time BidndingContext which is not worth the effort for small XAML files.

Actual Behavior

ViemModel properties are not shown

Basic Information

  • Version with issue:
  • Last known good version: Visual Studio 15.6.X
  • IDE: Visual Studio 15.7.3
  • Platform Target Frameworks:

    • Android: 7.1

  • Nuget Packages: none
  • Affected Devices: IDE issue

Screenshots

image

image

Xaml </> external-intellisense unverified bug

Most helpful comment

Just checked and seems x:DataType is meant for every element! When i specify x:DataType on my ContentPage and try to bind to non existing property, my project wont build.
It would be great to convince Visual Studio team to support intellisense for all elements with x:DataType then

All 22 comments

I have already create bug report for visual studio but I figured it would be good to discuss that here as well:
https://developercommunity.visualstudio.com/content/problem/255781/visual-studio-1571-and-xamarin-forms-xdatatype-can.html

/cc @mgoertz-msft

Any updates?

There are really two parts to this issue.

  1. x:DataType is treated as an error by our XAML Language Service.
  2. Binding IntelliSense inside of DataTemplates does not take x:DataType into account.

A fix for the first part will ship with 15.8. The 2nd part will then also work if you set x:DataType on the DataTemplate itself but not further up the parent chain (that is more of a missing feature which I'm afraid will no longer meet the bar for 15.8).

Any updates?

Setting x:DataType on the DataTemplate itself should already work in 15.8. Let me know if it doesn't do what you expect.

Right now I am working on supporting x:DataType anywhere in the tree.

I updated visual studio to 15.8 and it indeed works :) Thanks.
However XAML editor still marks x:DataType attribute as incorrect, please see screenshot:
image
(Edit: autocompletion for typing x:DataType and ="{x:Type vm:ViewModel} works, it's just maked as error in editor)

Support for x:DataType anywhere will be awsome 馃憤

@StephaneDelcroix can correct me if I'm wrong but I believe you cannot use the x:Type MarkupExtension here. You should use the type name directly, i.e.: x:DataType="vm:ViewModel", and that's what the IntelliSense supports currently.

x:Type markup extension worked in previous versions(by worked i mean autocompletion and no errors shown) of visual studio but maybe that was just an accident and I was using it all wrong.

Anyway one more problem then: when typing x:DataType="vm:ViewModel" there is no autocompletion for vm:ViewModel.

Also do you know if I used it wrong, would it skip XAMLC compilation?

x:DataType expects a string literal if I remember correctly. if you're passing a type (through x:Type) it might not generate any errors, but it's more than probably ignored, and your binding isn't compiled.

https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs#L377-L379

Anyway one more problem then: when typing x:DataType="vm:ViewModel" there is no autocompletion for vm:ViewModel.

In 15.8 you should get type completions if x:DataType is directly on the DataTemplate, so in the example from the screenshot above you should be able to get "nodeVm:NodeBooleanViewModel". Is that not the case?

@mgoertz-msft
I did some more testing, it turned out it works indeed in basic scenario (class with parameterless constructor placed in ViewModels folder)

It doesn't seem to work for me when:

  • ViewModel has constructor with parameters
  • ViewModel is placed in nested directory, for example ViewModels/Login/LoginViewModel.cs
  • ViewModel doesn't have public constructor

It does work though when view model contains method with syntax error

@StephaneDelcroix
This issue is already fixed in latest visual studio so i think we can close this bug.
There is one more thing to fix though, if user specifies x:DataType through unsupported syntax, for example x:DataType={x:Type vm:LoginViewModel} XamlC compliator should produce an error.

Also I heard that x:DataType is meant only for DataTemplates, is that true? I thought that specyfying x:DataType on any element, especially ContentPage would enable XamlC compilation of whole page and speed things up.

Just checked and seems x:DataType is meant for every element! When i specify x:DataType on my ContentPage and try to bind to non existing property, my project wont build.
It would be great to convince Visual Studio team to support intellisense for all elements with x:DataType then

I agree with @toomasz ... If we could get intellisence based on x:DataType for all elements, it'd be great! Especially for when the BindingContext is set in the C# code.

@SuperCorks Yes, that would be great. @StephaneDelcroix If x:DataType can be specified on any element, do you think it's reasonable that Visual Studio provides intellisense based on that specified type?

For now, only DataTemplates are supported

@mgoertz-msft ^^

This was fixed last year for VS 2019. x:DataType shows up as a suggestion on any element and will offer value completions for all types in scope.

Is this not working for you?

closing per @mgoertz-msft comment. open a new issue for any new problems with this

@mgoertz-msft No, it's not working for me, working only for DataTemplate element.
image
Does it work for you?

I second that. You only get value completion if you set the <ContentPage.BindingContext><local:MyViewModel /></ContentPage.BindingContext> property or in DataTemplates using x:DataType="local:MyViewModel". If you set x:DataType="local:MyViewModel" in the content page or in any element other than DataTemplate it doesn't autocomplete as you'd expect.

I tested that in VS2019 v16.0.3 and VS2019 Preview v16.1.0

@toomasz, @SuperCorks Ah, I was talking about the IntelliSense for x:DataType itself, i.e. x: will suggest DataType for any element and x:DataType="" will have all possible types inside of the quotes. The logic where this data type is being used has not changed.

@StephaneDelcroix Does x:DataType indeed work the way @toomasz has shown in the post above, i.e. with no BindingContext needed?

Was this page helpful?
0 / 5 - 0 ratings