With latest update of visual studio(15.7.X) autocompletion for view model properties stopped working in DataTemplates with x:DataType specified.
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.
ViemModel properties are not shown
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.
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:
(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.
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:
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.
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?
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