{"id":35336,"date":"2018-03-21T14:59:02","date_gmt":"2018-03-21T21:59:02","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=35336"},"modified":"2019-04-04T08:35:42","modified_gmt":"2019-04-04T15:35:42","slug":"debugging-xamarin-forms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/debugging-xamarin-forms\/","title":{"rendered":"Debugging Xamarin Open Source Frameworks: Part 1 &#8211; Xamarin.Forms"},"content":{"rendered":"<p>\t\t\t\tAt some point, while working with Xamarin to build awesome mobile apps, you may have received an exception or error notification from the Xamarin framework code, Mono runtime, or Base Class Library (BCL). If this happened, you may have stepped through that code to see if the error was in your code, or whether it was a bug in the framework code. After locating the error, you might even have wanted to file a pull request to merge that fix into the framework itself. Since Mono and the Xamarin frameworks, i.e. Xamarin.iOS, Xamarin.Android, and Xamarin.Forms, are all open source, you <em>can<\/em> contribute to debugging!<\/p>\n<h2>Debugging in Xamarin.Forms<\/h2>\n<p>In this four-part series, we&#8217;ll cover the debugging process and steps in each source code for Xamarin.Forms, Xamarin.iOS, Xamarin.Android, and Mono. We&#8217;ll start with building Xamarin.Forms from the source code, creating a NuGet package, and referencing that package from your solution in your apps.<\/p>\n<p><em>Although it <strong><em>is<\/em><\/strong> possible to build Xamarin.Forms on a Mac via the command line, doing so would not build the UWP platform assemblies for Xamarin.Forms. As such, we&#8217;ll be using a Windows PC with Visual Studio 2017 for this post.<\/em><\/p>\n<h3>Get the Xamarin.Forms Source Code<\/h3>\n<p>There are a number of ways to get the Xamarin.Forms source code. The official Xamarin.Forms repository on GitHub is located <a href=\"https:\/\/github.com\/xamarin\/\">here<\/a>. We recommend forking the Xamarin.Forms repository to your own GitHub account and cloning your fork to your computer. If you don&#8217;t have a GitHub account, you can create one <a href=\"https:\/\/github.com\/join\">here<\/a>. If you&#8217;re already familiar with using Git and GitHub, get the source code using your preferred method and skip to the next section.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-35466 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XFFork.png\" alt=\"\" width=\"800\" height=\"318\" \/><\/p>\n<p>In order to use the <strong>Team Explorer<\/strong> pane with Git and GitHub, you&#8217;ll also need to add the <a href=\"https:\/\/github.com\/github\/VisualStudio\/blob\/master\/docs\/getting-started\/installing-github-for-visual-studio.md#installing-the-github-extension-for-visual-studio\">GitHub Extension for Visual Studio<\/a>.<\/p>\n<h3>Cloning the Repository<\/h3>\n<p>Once installed, follow <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/devops\/repos\/git\/clone?view=vsts&amp;tabs=visual-studio\" target=\"_blank\" rel=\"noopener\">these instructions<\/a> to clone the repository. Then double-click the <strong>Xamarin.Forms<\/strong> repository in the Local Git repositories section to see the available solutions.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-35962 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/LocalGitRepos.png\" alt=\"\" width=\"446\" height=\"423\" \/><\/p>\n<p>Now that you have the Master branch opened in your IDE, let&#8217;s switch branches so that the source code lines up with the latest <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Forms\/2.5.0.280555\">2.5.0 release<\/a> of Xamarin.Forms.<\/p>\n<p>If you&#8217;re specifically looking to see if an issue is fixed in the Master, you can go ahead and attempt to build the Master branch, or download nightly builds from the Master through <a href=\"https:\/\/myget.org\/feed\/xamarinforms-ci\/package\/nuget\/Xamarin.Forms\">this NuGet package<\/a>. Test your app against this pre-built master NuGet package to easily see if the particular issue you&#8217;re experiencing has already been fixed.<\/p>\n<h2 id=\"revert-commits\">Revert to a Released Version of the Source Code<\/h2>\n<p>Before we build, go ahead and switch to the 2.5.0 branch, which has the final commits for the latest 2.5.0 release version.<\/p>\n<ol>\n<li>In the <strong>Team Explorer &#8211; Home<\/strong> tab, click the <strong>Branches<\/strong> button.\n<p><img decoding=\"async\" class=\"size-full wp-image-35493 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2018-02-07-at-3.45.53-PM.png\" alt=\"\" width=\"432\" height=\"497\" \/><\/li>\n<li>In the <strong>Team Explorer &#8211; Branches<\/strong> tab, expand the <strong>remotes\/origin<\/strong> node.<\/li>\n<li>Right-click the <strong>2.5.0<\/strong> branch and select <strong>Checkout<\/strong>. This downloads and selects the 2.5.0 branch as the active local branch.\n<p><img decoding=\"async\" class=\"size-full wp-image-35966 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/2.5.0Checkout.png\" alt=\"\" width=\"439\" height=\"723\" \/><\/li>\n<li>To see all of the commits in the 2.5.0 branch, right click on the <strong>2.5.0<\/strong> local branch and select <strong>View History<\/strong>\n<p><img decoding=\"async\" class=\"size-full wp-image-35968 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/2.5.0ViewHistory.png\" alt=\"\" width=\"439\" height=\"453\" \/><\/li>\n<li>This will open a <strong>History &#8211; 2.5.0<\/strong> document. On the right, you&#8217;ll see tags, including one for <strong>release-2.5.0-sr5<\/strong>. Reset the branch to this commit by right-clicking on the commit and selecting <strong>Reset &gt; Delete Changes (&#8211;hard)<\/strong>.\n<p><img decoding=\"async\" class=\"size-full wp-image-35970 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/2.5.0ReleaseCommit.png\" alt=\"\" width=\"800\" height=\"408\" \/><\/li>\n<li>Now go back to the <strong>Home<\/strong> tab of <strong>Team Explorer<\/strong>, and open <strong>Xamarin.Forms.2017.sln<\/strong>.<\/li>\n<\/ol>\n<p>You should now have the source code that was used to build the 2.5.0 release of Xamarin.Forms open in Visual Studio.<\/p>\n<h2 id=\"build\">Build Xamarin.Forms<\/h2>\n<p>Before building, it&#8217;s a good idea to make sure your Android SDK is up-to-date and has the latest Android platform SDKs installed. If you&#8217;re unfamiliar with the Android SDK, <a href=\"https:\/\/developer.xamarin.com\/guides\/android\/getting_started\/installation\/android-sdk\/\">here&#8217;s a guide<\/a> to working with it using the Xamarin Android SDK Manager.<\/p>\n<p>At this point, you should just have to select the <strong>&#8220;Any CPU&#8221;<\/strong> platform option and build your solution with <strong>Build &gt; Build Solution<\/strong>:<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-35987 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/BuildSolution.png\" alt=\"\" width=\"800\" height=\"160\" \/><\/p>\n<p>The build output may show that the build failed, but this might not matter. There are projects in the solution that don&#8217;t need to be built to create the NuGet package, like test projects. You may still be able to successfully create a NuGet package depending on which projects fail to build. Additionally, some projects have the <strong>Treat warnings like errors<\/strong> option enabled in the project properties.<\/p>\n<h2 id=\"create-nuget-package\">Create your Xamarin.Forms NuGet Package<\/h2>\n<p>Use the command line to create your NuGet package. You may need to make some changes to the <strong>Xamarin.Forms.nuspec<\/strong> file. There are still elements there that refer to <strong>win80\/81<\/strong> and <strong>winRT<\/strong> assemblies, so you won&#8217;t have these assemblies if you built in Visual Studio 2017. To fix this, open the <strong>Xamarin.Forms.nuspec<\/strong> file (in the <strong>.nuspec<\/strong> folder) and delete all <strong>&lt;group&gt;<\/strong> elements that refer to <strong>winRT<\/strong>, <strong>wp80<\/strong>, <strong>win81<\/strong>, or <strong>wpa81<\/strong>. Also delete all <strong>&lt;file&gt;<\/strong> elements in the <strong>&lt;&#8211;Windows Phone 8&#8211;&gt;<\/strong>, <strong>&lt;&#8211;WinRT Phone&#8211;&gt;<\/strong>, and <strong>&lt;&#8211;WinRT Desktop&#8211;&gt;<\/strong> sections. An edited version of this file is in <a href=\"https:\/\/gist.github.com\/jgold6\/3c6dff5fd4ee073b0d6562ccaa3b4c4d\">this Github repository<\/a>.<\/p>\n<ol>\n<li>Open a command prompt and navigate to the root of your local Xamarin.Forms repository.<\/li>\n<li>Enter the following to run a script that will create the NuGet package (see [1] below):\n <strong>.create-nuget.bat all<\/strong><\/li>\n<\/ol>\n<p>If successful, your <strong>Xamarin.Forms.&lt;version_number&gt;.nupkg<\/strong> file will be in the <strong>.nuspec<\/strong> folder. If the packaging fails, you&#8217;ll see output telling you what file(s) could not be found. For instance, if you didn&#8217;t update the <strong>Xamarin.Forms.nuspec<\/strong> file, as noted above, you will see errors about missing WinRT and Windows Phone assemblies.<\/p>\n<p>[1] It has come to my attention that the <strong>.create-nuget.bat<\/strong> file is not consistently updated and might not create a viable package. There is now a message to that effect in the initial output from the script. So instead of using the <strong>.create-package.bat<\/strong> script, try the following after rebuilding <strong>Xamarin.Forms.sln<\/strong>:<\/p>\n<ol>\n<li>Copy nuget.exe into the Xamarin.Forms .nuspec folder (get `nuget.exe` at <a href=\"https:\/\/www.nuget.org\/downloads\" rel=\"noopener\" target=\"_blank\">https:\/\/www.nuget.org\/downloads<\/a><\/li>\n<li>Open a command prompt<\/li>\n<li>Navigate to Xamarin.Forms .nuspec folder<\/li>\n<li>Run command:<\/li>\n<p><strong>nuget.exe pack Xamarin.Forms.nuspec -properties configuration=debug;platform=anycpu -Version 9.9.0<\/strong><\/li>\n<\/ol>\n<h2 id=\"use-nuget-package\">Use Your Xamarin.Forms NuGet Package<\/h2>\n<p>Now you need to create a folder on your system to use as a local NuGet package source. It doesn&#8217;t matter where you put this. Once you create the folder, move the <strong>Xamarin.Forms.&lt;version_number&gt;.nupkg<\/strong> file to this folder. Then:<\/p>\n<ol>\n<li>Create a new Xamarin.Forms solution using the <strong>Shared Project<\/strong> or <strong>.NET Standard<\/strong> code sharing strategy in Visual Studio.<\/li>\n<li>Right-click on your solution in the Solution Explorer and select <strong>Manage NuGet Packages for Solution<\/strong>.<\/li>\n<li>Click on the settings icon in the upper right of the <strong>NuGet &#8211; Solution<\/strong> document:\n<p><img decoding=\"async\" class=\"size-full wp-image-35561 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/NuGetSourceSettings.png\" alt=\"\" width=\"800\" height=\"98\" \/><\/li>\n<li>In the <strong>Options<\/strong> window that opens, click the <strong>+<\/strong> button.<\/li>\n<li>Enter a <strong>Name<\/strong> for the package source, like &#8220;My Local NuGet Source&#8221;.<\/li>\n<li>Click the <strong>&#8230;<\/strong> (browse) button next to the <strong>Source<\/strong> field and browse to the folder created above.\n<p><img decoding=\"async\" class=\"size-full wp-image-35565 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/AddNuGetSource.png\" alt=\"\" width=\"800\" height=\"376\" \/><\/li>\n<li>Click the <strong>Update<\/strong> button and then click <strong>OK<\/strong>.<\/li>\n<li>Select the <strong>Installed<\/strong> tab then scroll down and select <strong>Xamarin.Forms<\/strong>.<\/li>\n<li>Make sure <strong>Package source: All<\/strong> is selected in the upper right.<\/li>\n<li>Select all of the projects in the project list.<\/li>\n<li>In the Version drop down list, select the version of Xamarin.Forms you just created (likely version 9.9.x) and click the <strong>Install<\/strong> button:<\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"size-full wp-image-35567 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/ManagePackagesForSolution.png\" alt=\"\" width=\"800\" height=\"380\" \/><\/p>\n<p>Once this completes, you&#8217;re set to step into some Xamarin.Forms source code.<\/p>\n<h2 id=\"step-into-source\">Step Into the Source Code<\/h2>\n<p>You can now open any file of your Xamarin.Forms solution that makes a call into the Xamarin.Forms source code and place a breakpoint on a line that makes such a call. An obvious place to start would be the call to <strong>Xamarin.Forms.Forms.Init(&#8230;)<\/strong>, which is in <strong>AppDelegate.cs<\/strong> (iOS), <strong>MainActivity.cs<\/strong> (Android), and <strong>App.Xaml.cs<\/strong> (UWP). When the IDE breaks, step into the code and the <strong>Forms.cs<\/strong> file should open to allow you to review. You can also set breakpoints directly in the Xamarin.Forms source code files, and Visual Studio will break when those lines are hit.<\/p>\n<p>To confirm the source code file in your cloned Xamarin.Forms repository is indeed the file that opens in Visual Studio, right click on the file tab, select <strong>Open Containing Folder<\/strong>, and a File Explorer window will open with the source code file highlighted. You can then edit the Xamarin.Forms source code file, rebuild and repackage Xamarin.Forms, and test again with your new source code changes.<\/p>\n<p>Congratulations, you&#8217;re now stepping into and through Xamarin.Forms source code!<\/p>\n<h2 id=\"summary\">Summary<\/h2>\n<p>Now you can build Xamarin.Forms from the source code, create a NuGet package, reference that package from your solution, and step into the Xamarin.Forms source code to see what&#8217;s going on. If you discovered a bug or came up with an improvement during your development, please feel free to <a href=\"https:\/\/github.com\/xamarin\/\/blob\/master\/CONTRIBUTING.md\">contribute<\/a>!<\/p>\n<p>Enjoy this post? <a href=\"https:\/\/forums.xamarin.com\/124267\/\">Discuss it in the Xamarin Forums.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>At some point, while working with Xamarin to build awesome mobile apps, you may have received an exception or error notification from the Xamarin framework code, Mono runtime, or Base Class Library (BCL). If this happened, you may have stepped through that code to see if the error was in your code, or whether it [&hellip;]<\/p>\n","protected":false},"author":554,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4,16],"class_list":["post-35336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>At some point, while working with Xamarin to build awesome mobile apps, you may have received an exception or error notification from the Xamarin framework code, Mono runtime, or Base Class Library (BCL). If this happened, you may have stepped through that code to see if the error was in your code, or whether it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/35336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/554"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=35336"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/35336\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=35336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=35336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=35336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}