{"id":31901,"date":"2017-06-02T11:38:59","date_gmt":"2017-06-02T18:38:59","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=31901"},"modified":"2019-04-04T08:32:53","modified_gmt":"2019-04-04T15:32:53","slug":"unleashed-embedding-xamarin-forms-xamarin-native","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/unleashed-embedding-xamarin-forms-xamarin-native\/","title":{"rendered":"Unleashed: Embedding Xamarin.Forms in Xamarin Native"},"content":{"rendered":"<p>\t\t\t\tIf you&#8217;re anything like me, when you&#8217;re ready to start coding a new application you face the question, &#8220;Xamarin Native or Xamarin.Forms?&#8221; The only correct answer should be \u201cyes\u201d. In fact, I want to show you how this shouldn&#8217;t be a choice at all; you can just start coding with total confidence that you\u2019re on the right path.<\/p>\n<h2>Introducing Xamarin.Forms Embedding<\/h2>\n<p>At Microsoft Build 2017 <a href=\"https:\/\/channel9.msdn.com\/events\/Build\/2017\/B8099\" target=\"_blank\">we announced Xamarin.Forms Embedding<\/a>, the ability to take any <code>ContentPage<\/code> and add it to your native applications.<\/p>\n<p>At its core, Xamarin.Forms is a shared UI toolkit that gives you the ability to write a single UI to target Android, iOS, and UWP. It also provides services to empower rapid application development such as navigation, dependency management, and messaging.\u00a0With that, you may write your entire application in Xamarin.Forms and never need to touch the underlying native OS layers. But there <em>is<\/em> an underlying layer!<\/p>\n<p>Xamarin.Forms is built upon the foundation of Xamarin.iOS and Xamarin.Android, our top of the line solution for producing highly performant native applications with 100% access to platform APIs.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-31904\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/2017-06-02_08-12-00-2.gif\" alt=\"\" width=\"1432\" height=\"532\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Let\u2019s see how this looks on iOS.<\/p>\n<h2>Adding A Xamarin.Forms Page to Xamarin.iOS<\/h2>\n<p>The weather has been a big story in my hometown of St. Louis this past month, with severe rain and flooding, so naturally, I\u2019m obsessed with what\u2019s going on outdoors. Looking at the Xamarin <a href=\"https:\/\/github.com\/davidortinau\/build2017-new-in-xamarin-forms\" target=\"_blank\">Weather sample app<\/a>, you\u2019ll see we have Android, iOS, and UWP native applications.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-31907\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/2017-06-02_07-47-14-3.gif\" alt=\"\" width=\"640\" height=\"480\" \/><\/p>\n<p>While it&#8217;s fine for searching ad hoc, I wanted to store search history and compare locations. I\u2019ll show you how I did this on iOS and shared this across all three platforms using Xamarin.Forms.<\/p>\n<p>I added a new Shared Project (or PCL) to host the Xamarin.Forms page. As a side note, you <em>could<\/em> put a ContentPage directly into your native project and write your entire Xamarin native application using XAML for your UI.<\/p>\n<p>The Xamarin.Forms project and each platform project needs a reference to the <a href=\"#preview_source\">early pre-release Xamarin.Forms 3.0 package<\/a>. In the new shared project I have my <code>HistoryPage.xaml<\/code> and (for demo simplicity) I\u2019ve set my <code>BindingContext<\/code> to the page. An MVVM pattern would work perfectly fine here as well.<\/p>\n<p>As you review this XAML, you\u2019ll note there\u2019s nothing special going on here; nothing different from if this were a Xamarin.Forms application from top to bottom. This XAML now works everywhere in Xamarin. That\u2019s powerful!<\/p>\n<pre class=\"height-set:true lang:xml decode:true \">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;\n&lt;ContentPage xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\"\n             xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\"\n             x:Class=\"Weather.Forms.HistoryPage\"&gt;\n    &lt;Grid BackgroundColor=\"#002050\"&gt;\n        &lt;Image Source=\"buildheader.jpg\" VerticalOptions=\"Start\" \/&gt;\n        &lt;StackLayout VerticalOptions=\"Fill\"&gt;\n            &lt;StackLayout.Margin&gt;\n                &lt;OnIdiom x:TypeArguments=\"Thickness\"&gt;\n                    &lt;OnIdiom.Phone&gt;\n                        &lt;OnPlatform x:TypeArguments=\"Thickness\"&gt;\n                            &lt;On Platform=\"iOS\" Value=\"10,80,10,10\"&gt;&lt;\/On&gt;\n                            &lt;On Platform=\"WinRT, UWP, Android\" Value=\"10\"&gt;&lt;\/On&gt;\n                        &lt;\/OnPlatform&gt;\n                    &lt;\/OnIdiom.Phone&gt;\n                    &lt;OnIdiom.Tablet&gt;\n                        &lt;OnPlatform x:TypeArguments=\"Thickness\"&gt;\n                            &lt;On Platform=\"iOS\" Value=\"10,80,10,10\"&gt;&lt;\/On&gt;\n                            &lt;On Platform=\"WinRT, UWP, Android\" Value=\"10\"&gt;&lt;\/On&gt;\n                        &lt;\/OnPlatform&gt;\n                    &lt;\/OnIdiom.Tablet&gt;\n                    &lt;OnIdiom.Desktop&gt;\n                        &lt;OnPlatform x:TypeArguments=\"Thickness\"&gt;\n                            &lt;On Platform=\"WinRT, UWP\" Value=\"20\"&gt;&lt;\/On&gt;\n                        &lt;\/OnPlatform&gt;\n                    &lt;\/OnIdiom.Desktop&gt;\n                &lt;\/OnIdiom&gt;\n            &lt;\/StackLayout.Margin&gt;\n            &lt;BoxView HeightRequest=\"120\"\/&gt;\n            &lt;Label Text=\"Your Places\"&gt;\n                &lt;Label.TextColor&gt;\n                        &lt;OnPlatform x:TypeArguments=\"Color\"&gt;\n                            &lt;On Platform=\"Android\" Value=\"GhostWhite\"&gt;&lt;\/On&gt;\n                            &lt;On Platform=\"UWP, iOS\" Value=\"White\"&gt;&lt;\/On&gt;\n                        &lt;\/OnPlatform&gt;\n                &lt;\/Label.TextColor&gt;\n                &lt;Label.FontSize&gt;\n                    &lt;OnPlatform x:TypeArguments=\"x:Double\"&gt;\n                        &lt;On Platform=\"iOS\" Value=\"24\"&gt;&lt;\/On&gt;\n                        &lt;On Platform=\"WinRT, UWP, Android\" Value=\"18\"&gt;&lt;\/On&gt;\n                    &lt;\/OnPlatform&gt;\n                &lt;\/Label.FontSize&gt;\n            &lt;\/Label&gt;\n            &lt;ListView x:Name=\"HistoryItems\" VerticalOptions=\"Fill\" BackgroundColor=\"Transparent\"&gt;\n                &lt;ListView.ItemTemplate&gt;\n                    &lt;DataTemplate&gt;\n                        &lt;ViewCell&gt;\n                            &lt;Grid&gt;\n                                &lt;Grid.ColumnDefinitions&gt;\n                                    &lt;ColumnDefinition Width=\"50\"&gt;&lt;\/ColumnDefinition&gt;\n                                    &lt;ColumnDefinition&gt;&lt;\/ColumnDefinition&gt;\n                                    &lt;ColumnDefinition Width=\"100\"&gt;&lt;\/ColumnDefinition&gt;\n                                &lt;\/Grid.ColumnDefinitions&gt;\n                                &lt;Label Text=\"{Binding WeatherIcon}\" FontSize=\"18\" Grid.Column=\"0\" VerticalTextAlignment=\"Center\" TextColor=\"White\"&gt;\n                                    &lt;Label.FontFamily&gt;\n                                        &lt;OnPlatform x:TypeArguments=\"x:String\"&gt;\n                                            &lt;On Platform=\"UWP\" Value=\"\/Assets\/WeatherIcons.ttf#Weather Icons\"&gt;&lt;\/On&gt;\n                                            &lt;On Platform=\"iOS\" Value=\"Weather Icons\"&gt;&lt;\/On&gt;\n                                            &lt;On Platform=\"Android\" Value=\"WeatherIcons.ttf#Weather Icons\"&gt;&lt;\/On&gt;\n                                        &lt;\/OnPlatform&gt;\n                                    &lt;\/Label.FontFamily&gt;\n                                &lt;\/Label&gt;\n                                &lt;Label Text=\"{Binding LocationName}\" Grid.Column=\"1\" VerticalOptions=\"Center\" TextColor=\"White\"&gt;\n                                    &lt;Label.FontSize&gt;\n                                        &lt;OnPlatform x:TypeArguments=\"x:Double\"&gt;\n                                            &lt;On Platform=\"iOS\" Value=\"18\"&gt;&lt;\/On&gt;\n                                            &lt;On Platform=\"WinRT, UWP, Android\" Value=\"14\"&gt;&lt;\/On&gt;\n                                        &lt;\/OnPlatform&gt;\n                                    &lt;\/Label.FontSize&gt;\n                                &lt;\/Label&gt;\n                                &lt;Label Text=\"{Binding PostalCode}\" VerticalOptions=\"Center\" TextColor=\"White\" Grid.Column=\"2\" VerticalTextAlignment=\"Center\"&gt;\n                                    &lt;Label.FontSize&gt;\n                                        &lt;OnPlatform x:TypeArguments=\"x:Double\"&gt;\n                                            &lt;On Platform=\"iOS\" Value=\"18\"&gt;&lt;\/On&gt;\n                                            &lt;On Platform=\"WinRT, UWP, Android\" Value=\"14\"&gt;&lt;\/On&gt;\n                                        &lt;\/OnPlatform&gt;\n                                    &lt;\/Label.FontSize&gt;\n                                &lt;\/Label&gt;\n                            \n                            &lt;\/Grid&gt;\n                        &lt;\/ViewCell&gt;\n                    &lt;\/DataTemplate&gt;\n                &lt;\/ListView.ItemTemplate&gt;\n            &lt;\/ListView&gt;\n            &lt;StackLayout Orientation=\"Horizontal\" HorizontalOptions=\"Center\"&gt;\n                &lt;Label Text=\"{Binding PlatformName}\" TextColor=\"White\" VerticalTextAlignment=\"Center\"&gt;\n                    &lt;Label.FontSize&gt;\n                        &lt;OnPlatform x:TypeArguments=\"x:Double\"&gt;\n                            &lt;On Platform=\"iOS\" Value=\"24\"&gt;&lt;\/On&gt;\n                            &lt;On Platform=\"WinRT, UWP, Android\" Value=\"18\"&gt;&lt;\/On&gt;\n                        &lt;\/OnPlatform&gt;\n                    &lt;\/Label.FontSize&gt;\n                &lt;\/Label&gt;\n                &lt;Label TextColor=\"Red\" VerticalTextAlignment=\"Center\"&gt;\n                    &lt;Label.Text&gt;\n                        &lt;OnPlatform x:TypeArguments=\"x:String\"&gt;\n                            &lt;On Platform=\"UWP\" Value=\"&#xEB52;\"&gt;&lt;\/On&gt;\n                            &lt;On Platform=\"iOS\" Value=\"&#xf004;\"&gt;&lt;\/On&gt;\n                            &lt;On Platform=\"Android\" Value=\"&#xf004;\"&gt;&lt;\/On&gt;\n                        &lt;\/OnPlatform&gt;\n                    &lt;\/Label.Text&gt;\n                    &lt;Label.FontSize&gt;\n                        &lt;OnPlatform x:TypeArguments=\"x:Double\"&gt;\n                            &lt;On Platform=\"iOS\" Value=\"24\"&gt;&lt;\/On&gt;\n                            &lt;On Platform=\"WinRT, UWP, Android\" Value=\"18\"&gt;&lt;\/On&gt;\n                        &lt;\/OnPlatform&gt;\n                    &lt;\/Label.FontSize&gt;\n                    &lt;Label.FontFamily&gt;\n                        &lt;OnPlatform x:TypeArguments=\"x:String\"&gt;\n                            &lt;On Platform=\"UWP\" Value=\"Segoe MDL2 Assets\"&gt;&lt;\/On&gt;\n                            &lt;On Platform=\"iOS\" Value=\"FontAwesome\"&gt;&lt;\/On&gt;\n                            &lt;On Platform=\"Android\" Value=\"FontAwesome.otf#FontAwesome\"&gt;&lt;\/On&gt;\n                        &lt;\/OnPlatform&gt;\n                    &lt;\/Label.FontFamily&gt;\n                &lt;\/Label&gt;\n                &lt;Label Text=\" Xamarin.Forms\" VerticalTextAlignment=\"Center\" TextColor=\"White\"&gt;\n                    &lt;Label.FontSize&gt;\n                        &lt;OnPlatform x:TypeArguments=\"x:Double\"&gt;\n                            &lt;On Platform=\"iOS\" Value=\"24\"&gt;&lt;\/On&gt;\n                            &lt;On Platform=\"WinRT, UWP, Android\" Value=\"18\"&gt;&lt;\/On&gt;\n                        &lt;\/OnPlatform&gt;\n                    &lt;\/Label.FontSize&gt;\n                &lt;\/Label&gt;\n            &lt;\/StackLayout&gt;\n        \n        \n        &lt;\/StackLayout&gt;\n    &lt;\/Grid&gt;\n&lt;\/ContentPage&gt;<\/pre>\n<p>You get full use of <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/images\/\" target=\"_blank\" rel=\"noopener noreferrer\">images<\/a> and <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/text\/fonts\/\" target=\"_blank\" rel=\"noopener noreferrer\">custom fonts<\/a> here, as you would expect. Below in the code you\u2019ll see you can use <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/xaml\/xamlc\/\" target=\"_blank\" rel=\"noopener noreferrer\">XAML Compilation<\/a> to pre-compile the XAML, <code>MessagingCenter<\/code> for <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/application-fundamentals\/messaging-center\/\" target=\"_blank\" rel=\"noopener noreferrer\">messaging<\/a>, and all your <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/xaml\/xaml-basics\/data_binding_basics\/\" target=\"_blank\" rel=\"noopener noreferrer\">data bindings<\/a> work.<\/p>\n<pre class=\"height-set:true lang:c# decode:true \">[XamlCompilation(XamlCompilationOptions.Compile)]\npublic partial class HistoryPage : ContentPage\n{\n\tpublic const string HistoryItemSelected = \"HistoryItemSelected\";\n\n\tpublic HistoryPage()\n\t{\n\t\tInitializeComponent();\n\n\t\tHistoryItems.ItemsSource = HistoryRecorder.LocationHistory;\n\t\tHistoryItems.ItemTapped += HistoryItemsOnItemTapped;\n\n\t\tBindingContext = this;\n\t}\n\n\tpublic string PlatformName =&gt; $\"{Device.RuntimePlatform} \";\n\n\tprivate void HistoryItemsOnItemTapped(object sender, ItemTappedEventArgs itemTappedEventArgs)\n\t{\n\t\tvar historyItem = itemTappedEventArgs.Item as HistoryItem;\n\n\t\tif (historyItem == null)\n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\tMessagingCenter.Send(this, HistoryItemSelected, historyItem.PostalCode);\n\t}\n}<\/pre>\n<p>To use <code>HistoryPage<\/code> in the Xamarin.iOS application, I now only need to do two things:<\/p>\n<ol>\n<li>Initialize Forms with <code>Forms.Init()<\/code>.<\/li>\n<li>Add the page to my view.<\/li>\n<\/ol>\n<pre class=\"lang:c# decode:true \">namespace WeatherApp.iOS\n{\n    [Register (\"AppDelegate\")]\n    public class AppDelegate : UIApplicationDelegate\n    {\n        UINavigationController _navigation;\n        UIViewController _historyViewController;\n        \n        ...\n        \n        public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)\n        {\n            \/\/ #1 INITIALIZE\n            Forms.Init();\n            ...\n        }\n\n        public void ShowHistory()\n        {\n            if (_historyViewController == null)\n            {\n                \/\/ #2 ADD\n                _historyViewController = new HistoryPage().CreateViewController();\n            }\n\n            _navigation.PushViewController(_historyViewController, true);\n        }\n    }\n}<\/pre>\n<p>Note the returned value of the method <code>CreateViewController()<\/code> is <code>UIViewController<\/code>. How you interact with that native control from this point follows the same rules as if you had made it a UIViewController from the start&mdash;because you did! On Android <code>CreateFragment(*Context*)<\/code> returns the native <code>Fragment<\/code>, and on UWP <code>CreateFrameworkElement()<\/code> returns the native <code>FrameworkElement<\/code>.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-31915\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/weather_ios_framed_embedded.gif\" alt=\"\" width=\"335\" height=\"697\" \/><\/p>\n<h2>What\u2019s Coming Next<\/h2>\n<p>With your help, we&#8217;ll continue to validate our implementation and make sure it\u2019s all working as needed. Of the non-UI parts of Xamarin.Forms, the <code>NavigationService<\/code> is not supported outside of a Xamarin.Forms application, whereas <code>DependencyService<\/code> and <code>MessagingCenter<\/code> are fully operational.<\/p>\n<p>There is work yet to be done here. Right now the <code>ContentPage<\/code> is fully supported, but we also think we can do this at the control (and custom control) level. We need to look closely at <code>Application.Resources<\/code> and shared styling. We&#8217;re also looking at our templates and IDE support for making it easy and elegant to use Xamarin.Forms everywhere.<\/p>\n<h2>Start Embedding Today!<\/h2>\n<p>The Xamarin.Forms 3.0 preview of Embedding is published to a custom NuGet feed. To get it:<\/p>\n<ul>\n<li>Add a new source to your NuGet Manager: <code>https:\/\/www.myget.org\/F\/xamarinforms-dev\/api\/v3\/index.json<\/code><\/li>\n<li>Check Pre-Release<\/li>\n<li>Select and install the package with the feature name &#8220;Embedding&#8221; &#8211; <code>3.0.0.100-embeddingpreview<\/code><\/li>\n<\/ul>\n<p>The source for this Weather demo application code is available on GitHub: <a href=\"https:\/\/github.com\/davidortinau\/build2017-new-in-xamarin-forms\" target=\"_blank\">https:\/\/github.com\/davidortinau\/build2017-new-in-xamarin-forms<\/a><\/p>\n<p>As you can see, you can start with Xamarin.iOS, Xamarin.Android, and UWP and bring in Xamarin.Forms anywhere it benefits you. Or you can start with Xamarin.Forms and eventually migrate to Xamarin.iOS, Xamarin.Android, UWP, and everywhere else Xamarin.Forms take you in the future. Now you can do more and use your code in even more fantastic ways!<\/p>\n<p>This is a <em>Preview<\/em> capability, so we need your feedback! Join us in the forums and <a href=\"https:\/\/forums.xamarin.com\/96889\/preview-xamarin-forms-embedding\/p1\" target=\"_blank\"> please share your experience with us<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re anything like me, when you&#8217;re ready to start coding a new application you face the question, &#8220;Xamarin Native or Xamarin.Forms?&#8221; The only correct answer should be \u201cyes\u201d. In fact, I want to show you how this shouldn&#8217;t be a choice at all; you can just start coding with total confidence that you\u2019re on [&hellip;]<\/p>\n","protected":false},"author":553,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[16],"class_list":["post-31901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>If you&#8217;re anything like me, when you&#8217;re ready to start coding a new application you face the question, &#8220;Xamarin Native or Xamarin.Forms?&#8221; The only correct answer should be \u201cyes\u201d. In fact, I want to show you how this shouldn&#8217;t be a choice at all; you can just start coding with total confidence that you\u2019re on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/31901","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\/553"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=31901"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/31901\/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=31901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=31901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=31901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}