Search code examples
xamlxamarin.formsvisual-studio-2017xamlparseexception

Xamarin Forms in Visual Studio 2017 XAML validation


I am really new to Xamarin.Forms and trying to learn using this nice tutorial.

I have managed to set up everything in Visual Studio 2017 Community Edition and successfully deploy to both emulated and physical devices (Android only).

During various trials, I have noticed that some XAML errors are not highlighted and build is successfully performed, but XAML failed at runtime, crashing the app. E.g. misspell a style name or any an attribute value.

Running in debug mode will just hang the app. Breaking it indicates the following line:

public partial class MainPage : global::Xamarin.Forms.ContentPage {  [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
    private void InitializeComponent() {
        // this is the last line from my code that hands when XAML is invalid
        this.LoadFromXaml(typeof(MainPage));
    }
}

Resuming the execution shows a hint of what is wrong (no fancy details as provided by the new VS2017 exception interface):

Unhandled Exception:

Xamarin.Forms.Xaml.XamlParseException: occurred

Question: Is there a way to catch invalid XAML errors earlier? Or at least obtain more relevant information? Or is this a known limitation of Xamarin.Forms?

To make things worse, autocomplete for attribute values does not seem to work in VS2017 (it works in VS2015), but this is another issue for another question.

Further details about installed software:

Microsoft Visual Studio Community 2017
Version 15.0.26228.9 D15RTWSVC
Microsoft .NET Framework
Version 4.6.01586

Installed Version: Community

Xamarin   4.3.0.795 (aece090)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android SDK   7.1.0.41 (9578cdc)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK   10.4.0.123 (35d1ccd)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Solution

  • To catch XAML errors at compilation time you have to add one line to AssemblyInfo.cs :

    [assembly: XamlCompilation(XamlCompilationOptions.Compile)]
    

    More information on this can be found here.

    Unfortunately Xamarin.Forms XAML intellisense support in VS is not like for WPF or SilverLight, it's very limited currently. One suggestion can be to write your ui in code, this way you will get a feedback faster and if you are very new to Xamarin.Forms XAML you can get familiar with it much easier because of intellisense in code behind.

    Another option is to use IDE extensions, in your case as I understand it is VS 2017 for Windows so it will not help you, but for Xamarin Studio for MC you have tools like mfractor, that build to solve the issues you addressed.