XAML Bindings not resolved in Visual Studio Editor but could be with ReSharper

Mobile_DanMobile_Dan USMember, Beta, University, Certified XTC Partners ✭✭

When I edit a XAML file in Visual studio and look at a bound element like this...

    <Entry Text="{Binding Username}" />

I see a little squiggly green line under the first two letters of Username. This is my beloved ReSharper letting me know that it "Cannot resolve symbol 'Username' due to unknown DataContext".

This started to annoy me so I did a little research. I found that if I added the following properties to the ContentPage element the little warning disappeared. (this is used in WPF development)

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:rootViewModel="clr-namespace:MyApp.ViewModels;assembly=MyApp"
    d:DataContext="{d:DesignInstance rootViewModel:LoginViewModel}"

Not only did the annoying warnings disappear but many benefits were unlocked! (available only with ReSharper)

  • If the bound field was misspelled, I get a ReSharper warning telling me the property is not found in the bound class.
  • I can right click and "Go To Declarartion" to the bound property from the XAML.
  • If I right click and "Find Usages" to the bound property from the class, the XAML reference shows up is the usage list. I can "Find Usages" from the XAML also.
  • I can rename the bound property using the ReSharper renaming option from the XAML or the class and it will change the other.

This makes the XAML more integrated with the code and could reduce defects and improve efficiency. I was preparing to write a blog post highlighting this trick. I figured I better actually run the app first, just as a sanity check, to make sure it runs and... Crash! ... "Xamarin.Forms.Xaml.XamlParseException: MarkupExtension not found for d:DesignInstance". Reality bytes.

If Xamarin.Forms was modified to ignore these designer XAML properties, it would unlock some great development features for ReSharper users. (There may be a way to enable these features without Resharper but might be more involved. It is so close with ReSharper)

Posts

  • powerdudepowerdude USMember ✭✭

    Hi @Mobile_Dan ,
    please report a bug at http:\bugzilla.xamarin.com

  • Mobile_DanMobile_Dan USMember, Beta, University, Certified XTC Partners ✭✭

    Thanks Cliff.

    I filed a bug here: https://bugzilla.xamarin.com/show_bug.cgi?id=27295

    Dan

  • MikeEEEMikeEEE USMember ✭✭

    100% agree! I just landed on this issue, myself. I have been coding luckily away in the designer/Xaml and just tried to launch it and landed on this issue. It is so common/ubiquitous in WPF-land that I really didn't think twice about this. Please consider adding support for this very awesome (and expected!!) feature.

  • MikeEEEMikeEEE USMember ✭✭

    I do have to say that I have been MOST impressed with the development/design experience w/ ReSharper and Xamarin.Forms. It has been quite amazing. This little hiccup has been really the only rub so far. I'd rate it at a surprising 90% experience when compared to WPF. 10% going to this problem. :)

  • MikeEEEMikeEEE USMember ✭✭

    FWIW, keep your eyes on this thread... we might have found a workaround in the meantime (and feel free to "me too" to help nudge it along, :innocent: ): https://github.com/firstfloorsoftware/xcc/issues/3

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    Cheers for this heads-up. I'm another happy resharper forms user, didn't even think this was possible.

    We use design time data very successfully in WPF. I did get the BindingContext setup in XAML using a ViewModelLocator and MVVM Light rather than setting it in code using code.

    BindingContext="{Binding Source={x:Static mvvm:App.Locator}, Path=Search}"

  • MikeEEEMikeEEE USMember ✭✭

    WOW! The owner of that project got that feature in already! Pretty impressive. You can see the documentation here:
    https://github.com/firstfloorsoftware/xcc/wiki/Remove-ignorable-content

    I have installed the latest version (available today) and verified that it works and strips out the DesignInstance and all d: namespace elements. Pretty awesome. :)

  • Mobile_DanMobile_Dan USMember, Beta, University, Certified XTC Partners ✭✭

    Thanks Michael!

    I have the NuGet package you referenced, XCC, successfully removing design time properties from XAML in Visual Studio.

    Unfortunately XCC does not compile in Xamarin Studio on the Mac. I filed an issue with the author on GitHub.

    I do most Xamarin development in Visual Studio but occasionally run iOS from Xamarin Studio on the Mac. I won't use any component that doesn't also work in Xamarin Studio so I'll hold off using XCC for now.

    XCC might be a good option for those who only use Visual Studio. Note: I did not try XCC on Xamarin Studio on the PC.

    I'll report back here if XCC starts working on Xamarin Studio on the Mac.

  • powerdudepowerdude USMember ✭✭

    Hi, i'm trying to use this technique, but it doesn't seem to help for ListView item templates. I still get a squiggly for property bindings i have in a TextCell. Any ideas here?

  • Mobile_DanMobile_Dan USMember, Beta, University, Certified XTC Partners ✭✭

    I got it working by declaring the designer DataContext for the TextCell like this...

        <ListView.ItemTemplate>
                <DataTemplate>
                        <TextCell d:DataContext="{d:DesignInstance dataModels:MessageAccessModel}" Text="{Binding Username}" Detail="{Binding Subject}" />
                </DataTemplate>
        </ListView.ItemTemplate>
    
  • powerdudepowerdude USMember ✭✭

    thanks @Mobile_Dan , that helps.

  • OtaMaresOtaMares DEMember ✭✭

    Is this only Visual Studio related or would that also help us poor xamarin studio guys?

  • Mobile_DanMobile_Dan USMember, Beta, University, Certified XTC Partners ✭✭

    I was only able to get the link between XAML and the bound class working in Visual Studio with ReSharper. I briefly tried it in Xamarin Studio and I don't see that the designer properties mentioned above have any beneficial effect. Unfortunately they do not compile so if you're running the same project in Visual Studio and Xamarin Studio (as I do) these designer properties will not run in Xamarin Studio.

    The designer properties have a beneficial effect only in Visual Studio with ReSharper but do not run from Visual Studio. @MichaelDeMond noted that a NuGet Package for Visual Studio called XCC will remove these designer attributes allowing the project to run .

    I wasn't able to get XCC to work in Xamarin Studio on the Mac. I filed an issue with the author on GitHub. (XCC would not get the link working between XAML and the bound class but at least designer attributes would be stripped out allowing the project to run)

    For now I am using XCC on Visual Studio and occasionally when I run in Xamarin Studio, I use a global search replace tool to remove the XAML attributes that cause problems.

    I am searching in all XAML files and removing the following two string expressions:

    1. d:DataContext="{d:*}" // where * is a wild card
    2. mc:Ignorable="d"

    I am using a tool called Search and Replace but there are many other options including Xamarin Studio itself.

  • jonathanyatesjonathanyates GBMember ✭✭✭

    I'm experiencing the same issue with xcc in Xamarin Studio. It would be great if this could be fixed.

  • MarkMadlangbayanMarkMadlangbayan USUniversity

    Same issue, it doesn't work with Xamarin Studio. I work on VS, but my co-worker wants to develop on OSX.

  • codenutzcodenutz GBMember

    I also have this issue

Sign In or Register to comment.