Xamarin.forms: [Bug] Intellisense for bindings does not work if BindingContext is not set in Xaml

Created on 13 Jul 2019  Â·  27Comments  Â·  Source: xamarin/Xamarin.Forms

Description

Intellisense for bindings does not work if BindingContext is not set in Xaml.
However if BindingContext is set in Xaml, autocompletion of binding properties from viewmodel does work.
I'd expect autocompletion to work when x:DataType is set (at page level, in xaml) even if BindingContext is only set in the code-behind, but it's not working.

Steps to Reproduce

  1. New xamarin forms app, new xaml page and its associated viewmodel.
  2. Do not set the BindingContext in the xaml page.
  3. Enable compiled bindings by setting x:DataType at xaml page level.
  4. Try autocomplete when binding a viewmodel property.

Expected Behavior

Autocomplete should work.

Actual Behavior

Autocomplete does not work (unless BindingContext is set in xaml).

Basic Information

  • Version with issue: XF 4.1.0.581479
  • IDE: VS 2019 16.1.6
  • Platform Target Frameworks: N/A (netstandard forms project)

Screenshots

xaml-intellisense

Xaml </> binding ⛓ external-intellisense high impact bug

Most helpful comment

@samhouts @StephaneDelcroix @mgoertz-msft Do we have VS ticket for this?
I remember I was reporting that too in 2018 and my bug was also closed without fix.

Guys, adding autocompletion for x:DataType specified on any element would be great productivity boost for Xamarin.Forms developers :)

developers. It should also support finding references in XAML by given ViewModelProperty.

All 27 comments

I can reproduce the same problem with VS 2019 16.1.6. I attach an example.
Test6865.zip

@mgoertz-msft: anything we can do here ??

We are currently investigating how we can make MVVM developers more productive. This could potentially be part of it, but we also want to be able to have a better "connection" (for the lack of a better term) between the view and the view model, which may make this unnecessary.

I would love for you guys to have a look at how we would traditionally do it in WPF. In WPF we would do it by including this in the top of the Xaml file:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d"
d:DataContext="{d:DesignInstance $MyClass$, 
    IsDesignTimeCreatable=True}"

What this give you is full intellisense and ignoring the constructor!

I have actually made it work, with some modifications, in Xamarin.Forms.
https://gist.github.com/haavamoa/494bb5e7786ff341ce3d52982572d3dd
My problem was that a friend grabbed the forms repository on a Mac, and it would not build there.
I do have the Windows Desktop tools installed pararell to my Mobile Development tools from the Visual Studio installer. So I fear that I have used something from the net framework package (?) that is not available on Mac.

This is me using it in Xamarin.Forms
https://github.com/haavamoa/TeamTimer/blob/master/src/TeamTimer/Views/MatchPage.xaml
This is me doing it In WPF
https://github.com/haavamoa/RetrospectiveClient/blob/master/src/Retrospective.Clients.WPF/Views/Configuration/UserConfigurationView.xaml

I totally agree with having this working when doing a x:DataType through, it would both force you to use compiled bindings as well as getting intellisense.

I tried to make @jamesmontemagno to use this during a Livestream : http://www.twitch.tv/jamesmontemagno/v/461649019?sr=a&t=4875s

Other sources

Using d:ContentPage.BindingContext is a okay way of doing it, but my problem with that way is that the viewmodel needs a empty constructor. This is rarely the case, because you normally would have constructor injection when doing MVVM. Now, you can create an extra constructor that is empty, but for me this is breaking MVVM. You change your viewmodel because the view needs it for intellisense, that makes no sense to me.

@haavamoa I agree that you usually want ctor injection for your VM in normal forms application, but we dropped that requirement with Shell, as the queryAttributes are injected, by property, to the VM. Best of both world

@haavamoa I agree that you usually want ctor injection for your VM in normal forms application, but we dropped that requirement with Shell, as the queryAttributes are injected, by property, to the VM. Best of both world

Hello @StephaneDelcroix. That is a fair route to go regarding Shell. I still would love for you guys to check out the stuff that I mentioned in the above post regarding intellisense.

https://github.com/xamarin/Xamarin.Forms/issues/6865#issuecomment-520987351

@StephaneDelcroix you still need constructor injection with Shell - as there are other dependencies which are not navigation related (Repos / Apis / Services etc.)

Would be great if you could implement Blend DesignInstance mecanism to give Intellisense (as @haavamoa wrote it) while the team is taking time to work on the better View/ViewModel connection. For now, a bunch of us are stuck with Resharper on windows cause it's the only tool which implements it, and I will be so glad to uninstall this greedy fellow.

Setting x:DataType should enable intellisense IMHO

Setting x:DataType should enable intellisense IMHO

Thank you. That's the reason why I've opened this ticket... 7 months ago !

Try x:DataType="{x:Type ViewModel}", that helps me get intellisense inside of DataTemplates now. The intellisense only work for DataTemplate though :(

@haavamoa wow that´s new. i thought it didn´t work at all.

thanks for the tip.

Yeah, someone has added something to enable this because it did not work before. I noticed it a couple of weeks ago.

@haavamoa by chance do you have link to the change it can help role out a fix for this.

@haavamoa by chance do you have link to the change it can help role out a fix for this.

I don't, sorry. But I guess we could start a new forms project and roll back Xamarin.Forms versions until it won't give you intellisense. The problem is that I don't know if the change is with Forms or with Visual Studio.

Probably in Visual Studio.

What is the point of x:DataType if it doesn't seem to work on almost anything.

@wrkettlitz specifying x:DataType all's the Xaml Compiler to compile the binding and skip using runtime reflection

@StephaneDelcroix thanks for your quick reply.
So is set it here <DataTemplate x:DataType="{x:Type models:ExampleModel}">
And intellisense works fine for the properties of this model.
However if I try to get a property of an property, intellisense fails.
Is there a way to fix this?

@wrkettlitz I can't answer anything about intelisense, @mgoertz-msft is the authority source on this.

@wrkettlitz Could you please open a VS Feedback Ticket through Visual Studio so we can track this appropriately for VS. Please also be specific in the description. For example, is the issue...?

  1. that when you do get view model properties you don't get sub properties or
  2. that you expect view model IntelliSense when you set x:DataType on a parent element or
  3. both

For 1. we may need a small repro project as getting properties of properties is expected to work.

This should now be a VS Feedback ticket. Closing this issue. Thanks!

@samhouts Could you please post the link to the VS feedback ticket ?

@samhouts @StephaneDelcroix @mgoertz-msft Do we have VS ticket for this?
I remember I was reporting that too in 2018 and my bug was also closed without fix.

Guys, adding autocompletion for x:DataType specified on any element would be great productivity boost for Xamarin.Forms developers :)

developers. It should also support finding references in XAML by given ViewModelProperty.

@samhouts, please provide a vs feedback link if you have one. Want to follow this progress

I know it's not the solution - but for the benefit of those who have ended up here looking for a workaround - _Resharper's_ intellisense does currently support the x:DataType approach.

x:DataType="viewmodels:SomeExampleViewModel"

on any element (ContentPage, or nested element such as a StackPanel) for the element and it's children.

image

Visual Studio v16.8.1 (Windows)
Resharper 2020.2.2+

Was this page helpful?
0 / 5 - 0 ratings