Monday, June 19, 2017

Setting up XAML Compilation

When you are new on the xamarin development, one of the confusing line is when you got wrong XAML syntax's but there is not really clear on the  which line of your code occurred the errors. this caused by default setting of XAML xamarin syntax will be compiled at the run time, not at the build time, so you will only got your apps force close unexpectedly and got not so clear about the errors. this is very confusing for early xamarin developers. I will give the example and the solution how to fix this. I am using visual studio for mac and this could be same steps in xamarin studio.

here the example:
first I made a random wrong XAML syntax for testing purpose, you can do write random wrong code too. and then if I tried to build the project there would be no problem until we run the apps and came to the error page (error page must be as presenter which means occurrence show page).

and then got this errors.

as you can see there is not really clear about the errors (you must search manually the class and the line of code), this is very bothering especially for early xamarin developers.

so for handle this you must edit the assembly option of your project. this is very simple step, you only have to add one line of code on the class AssemblyInfo. go to class AssemblyInfo.cs in the properties folder.

then add this line :
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

don't forget to add namespace by  using Xamarin Forms Xaml namespace, write this on the top-line of the class :
using Xamarin.Forms.Xaml;

Here the complete source code of AssemblyInfo.cs


using System.Reflection;
using System.Runtime.CompilerServices;
using Xamarin.Forms.Xaml;
// Information about this assembly is defined by the following attributes. 
// Change them to the values specific to your project.

[assembly: AssemblyTitle("Section7")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("${AuthorCopyright}")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("1.0.*")]
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

// The following attributes are used to specify the signing key for the assembly, 
// if desired. See the Mono documentation for more information about signing.

//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]


And now if you build the project, the XAML will thrown an error on build time. like this :

and now you can see on the which line of code and class the errors has occurred, and if you double click the errors-line it will guide you to the error-class.

So finally it is your choice about the compilation. but for myself I am preferred to set this on the build time, not only for more clearly about the errors code, but it also prevent from missed errors line on your code that can caused force close unexpectedly on your apps in the future.

Hope this article help you!

No comments:

Post a Comment

Contact me


Email *

Message *