{"id":162,"date":"2020-03-19T12:18:00","date_gmt":"2020-03-19T19:18:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/surface-duo\/?p=162"},"modified":"2020-04-09T12:17:24","modified_gmt":"2020-04-09T19:17:24","slug":"bring-your-xamarin-apps-to-surface-duo","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/surface-duo\/bring-your-xamarin-apps-to-surface-duo\/","title":{"rendered":"Bring your Xamarin apps to Surface Duo"},"content":{"rendered":"<p>Hello Surface Duo developers!  <\/p>\n<p>To celebrate the <a href=\"https:\/\/msft.it\/6013TjukZ\">.NET Conf: Focus on Xamarin<\/a> live broadcast next week (tune in Monday!), today\u2019s blog post will outline how to support the dual-screen devices from Xamarin apps. Xamarin lets you build Android apps using C# in Visual Studio, including building and testing apps for the Surface Duo. <\/p>\n<h2>Set up the Surface Duo emulator<\/h2>\n<p>The Surface Duo emulator and SDK are already available and ready for you to get started. Follow <a href=\"https:\/\/docs.microsoft.com\/dual-screen\/android\/use-emulator?tabs=windows#xamarin-developers\">these instructions<\/a> to set up the Surface Duo emulator for use with Visual Studio. Once you have started the emulator, it will appear as a deployment option in Visual Studio and you can test your existing Xamarin.Android and Xamarin.Forms apps! <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/03\/emulator-vs-start-menu-300x154.png\" alt=\"Start menu showing the Surface Duo emulator item\" width=\"300\" height=\"154\" class=\"alignnone size-medium wp-image-165\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/03\/emulator-vs-start-menu-300x154.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/03\/emulator-vs-start-menu.png 676w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/>\nFigure 1: Start the emulator before testing in Visual Studio<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/testing-your-app-on-the-dual-screen-form-factor\/\">Step 1<\/a> in the journey to dual-screen devices is testing your app, which you can do today. From there, you can add functionality to take advantage of both screens, using the APIs described below. <\/p>\n<h2>Display mask and hinge handling<\/h2>\n<p>The <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/ring-your-app-to-surface-duo-step-3\/\">Java APIs<\/a> we talked about last week are also accessible by C# in Xamarin.Android apps.  \nAdd the Xamarin.DuoSdk NuGet to your project and you can use these code snippets to detect the display mask and hinge angle device attributes: <\/p>\n<h3>Display Mask location<\/h3>\n<pre class=\"prettyprint\">using Microsoft.Device.Display; \r\n\/\/ ... in MainActivity.OnCreate \r\nvar screenHelper = new ScreenHelper(); \r\nvar isDuo = screenHelper.Initialize(this); \r\nvar isSpanned = screenHelper.IsDualMode; \r\nvar maskBounds = screenHelper.GetHingeBounds(); \/\/ eg. Rect(1350, 0 - 1434, 1800) <\/pre>\n<h3>Hinge Angle<\/h3>\n<p>The <a href=\"https:\/\/github.com\/microsoft\/surface-duo-sdk-xamarin-samples\/blob\/master\/XamarinForms\/DualScreenDemos.Android\/HingeAngleLabelRenderer.cs\">code for the accessing the hinge angle sensor<\/a> is available on GitHub. Using the information from these APIs, you can adapt your Android app layouts for dual-screens. <\/p>\n<p>Other useful <a href=\"https:\/\/github.com\/microsoft\/surface-duo-sdk-xamarin-samples\">Xamarin.Android sample code<\/a> can be found in the GitHub repo, including different UI patterns and a drag and drop demo. <\/p>\n<h2>TwoPaneView layout control<\/h2>\n<p>Xamarin.Forms 4.5 introduces a new layout control specifically for dual-screen support: the TwoPaneView control. To use, add the <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Forms.DualScreen\/\">Xamarin.Forms.DualScreen<\/a> NuGet to your Xamarin.Forms, and include a ContentView for each pane: <\/p>\n<pre class=\"prettyprint\">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?> \r\n&lt;ContentPage \r\n    xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\" xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\" \r\n    xmlns:dualScreen=\"clr-namespace:Xamarin.Forms.DualScreen;assembly=Xamarin.Forms.DualScreen\" \r\n    x:Class=\"SampleApp\" Visual=\"Material\"> \r\n    &lt;dualScreen:TwoPaneView x:Name=\"twoPaneView\"> \r\n        &lt;dualScreen:TwoPaneView.Pane1> \r\n            &lt;Label Text=\"Left screen\"\/> \r\n        &lt;\/dualScreen:TwoPaneView.Pane1> \r\n        &lt;dualScreen:TwoPaneView.Pane2> \r\n            &lt;Label Text=\"Right screen\"\/> \r\n        &lt;\/dualScreen:TwoPaneView.Pane2> \r\n   &lt;\/dualScreen:TwoPaneView> \r\n&lt;\/ContentPage><\/pre>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/xamarin.forms.dualscreen.twopaneview?view=xamarin-forms\"><code>TwoPaneView<\/code><\/a> is extremely configurable for a variety of layouts for single- and dual-screen devices (including tablets). Refer to the Xamarin.Forms dual-screen docs for instructions on the complete API.  <\/p>\n<p>Using Xamarin.Forms provides the additional benefit of creating apps that can be deployed on the Surface Neo running Windows 10X. Check out the Xamarin blog for <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/app-fundamentals\/dual-screen\/\">more details<\/a> on how you can create a single codebase for both platforms. Here\u2019s a screenshot of the app spanned across two screens: <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/03\/xamarin-emulator.png\" alt=\"Surface Duo showing XamarinTV sample\" width=\"800\" height=\"534\" class=\"alignnone size-full wp-image-263\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/03\/xamarin-emulator.png 800w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/03\/xamarin-emulator-300x200.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/03\/xamarin-emulator-768x513.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/>\nFigure 2: Surface Duo running XamarinTV sample<\/p>\n<p>The <a href=\"https:\/\/github.com\/xamarin\/app-xamarintv\">XamarinTV sample<\/a> runs on both the Surface Duo and Surface Neo \u2013 try it on both emulators today! <\/p>\n<h2>Feedback<\/h2>\n<p>We would love to hear from you about your experiences using the SDK, emulator, and your first thoughts on how you can utilize these in your Android apps.<\/p>\n<p>Please reach out using our <a href=\"https:\/\/aka.ms\/SurfaceDuoSDK-Feedback\">feedback forum<\/a> or direct message me on <a href=\"https:\/\/twitter.com\/conceptdev\">Twitter<\/a> or <a href=\"https:\/\/github.com\/conceptdev\">GitHub<\/a>. <\/p>\n<p>And if you haven\u2019t seen those already, we started a <a href=\"https:\/\/www.youtube.com\/playlist?reload=9&#038;list=PLlrxD0HtieHg0F4sBsc4c29ReSHQqpsqs\">series of 1 minute videos<\/a> to answer the most common questions <\/p>\n<p>Finally, don\u2019t forget about <a href=\"https:\/\/msft.it\/6013TjukZ\">.NET Conf: Focus on Xamarin<\/a> \u2013 tune in on <b>Monday March 23rd<\/b> to learn more about Xamarin and its dual-screen support.<\/p>\n<p><a href=\"https:\/\/msft.it\/6013TjukZ\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2020\/03\/dotnetconf-xamarin-800.png\" alt=\".NET Conf Focus on Xamarin March 23, 2020\" width=\"800\" height=\"205\" class=\"alignnone size-full\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Xamarin developers can build and test dual-screen apps for the Surface Duo, using the available emulator and SDKs available via NuGet. The hinge angle and display mask can be queried to adapt your screen layouts, and there is a TwoPaneView Xamarin.Forms control that helps build responsive app designs that work on all device types.<\/p>\n","protected":false},"author":570,"featured_media":263,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,46,45,181,182],"class_list":["post-162","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-surface-duo-sdk","tag-dual-screen-development","tag-surface-duo","tag-surface-duo-sdk","tag-xamarin","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>Xamarin developers can build and test dual-screen apps for the Surface Duo, using the available emulator and SDKs available via NuGet. The hinge angle and display mask can be queried to adapt your screen layouts, and there is a TwoPaneView Xamarin.Forms control that helps build responsive app designs that work on all device types.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/users\/570"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media\/263"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}