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)