{"id":3960,"date":"2016-05-11T20:20:39","date_gmt":"2016-05-11T20:20:39","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3960"},"modified":"2016-05-11T20:51:20","modified_gmt":"2016-05-11T20:51:20","slug":"prism-and-xamarin-forms","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/prism-and-xamarin-forms\/","title":{"rendered":"Prism and Xamarin Forms"},"content":{"rendered":"<p>Here&#8217;s the steps for getting Prism and Xamarin Forms up and running together. For this I&#8217;m using Visual Studio 2015. Refer to a <a href=\"http:\/\/putridparrot.com\/blog\/xamarin-and-visual-studio-2015\/\">previous post<\/a> on how to get a Xamarin Forms application up and running in Visual Studio 2015 if you need to.<\/p>\n<p><em>Note: I&#8217;m using the Unity container hence I&#8217;ll be demonstrating the code using Prism.Unity.Forms, there is support for alternate IoC containers such as NInject, Mef and Autofac. Prism.Unity.Forms will automatically include Prism.Core and Prism.Forms.<\/em><\/p>\n<ul>\n<li>Create a Cross-Platform, Blank App (Xamarin.Forms Portable)<\/li>\n<li>Using NuGet, install Prism.Unity.Forms (at the time of writing &#8211; you&#8217;ll need to click on the Include prerelease option), I&#8217;ve installed v6.2.0-pe5<\/li>\n<li>For each of the projects you&#8217;ve created you&#8217;ll now need to go to each References section and add the Prism.Unity.Forms package from NuGet<\/li>\n<li>Prism has a convention based approach to loading view model&#8217;s, so let&#8217;s create the two folders, Views and ViewModels off of the Portable project &#8211; in fact it&#8217;s the namespace that matters, so the expectation is for views to be in the Views namespace and likewise the view models should be in the ViewModels namespace.<\/li>\n<li>Now add a MainPage.xaml file to the Views folder, so in the Portable project right mouse click, select Add | New Item and then from Cross-Platform select Forms Xaml Page, I&#8217;ve named mine MainPage<\/li>\n<li>In the MainPage.xaml file you&#8217;ll see the Label is bound to a MainText property, so let&#8217;s now create a MainPageViewModel.cs file in the ViewModels folder to act as our view model for this page (the default convention is for the view model for a view to be named after the view with the suffix ViewModel, i.e. MyView by default maps to MyViewModel).<\/li>\n<li>To allow Prism to auto wire up view models we need to add the following to the XAML file (to the ContentPage)\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nxmlns:mvvm=&quot;clr-namespace:Prism.Mvvm;assembly=Prism.Forms&quot;\r\nmvvm:ViewModelLocator.AutowireViewModel=&quot;True&quot;\r\n<\/pre>\n<\/li>\n<li>Derive the view model from BindableBase and implement the following code to simply get things up and running\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MainPageViewModel : BindableBase\r\n{\r\n   public string MainText { get; } = &quot;Hello Prism World&quot;;\r\n}\r\n<\/pre>\n<p>You&#8217;ll need to add <em>using Prism.Mvvm;<\/em> to resolve the BindableBase object.\n<\/li>\n<li>Remove all the code from the App constructor as the MainPage will be supplied by Prism going forward.<\/li>\n<li>Derive your App from PrismApplication instead of Application and implement the abstract methods as below\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class App : PrismApplication\r\n{\r\n   \/\/ add these methods\r\n   protected override void OnInitialized()\r\n   {\r\n      NavigationService.NavigateAsync(&quot;MainPage&quot;);\r\n   }\r\n\r\n   protected override void RegisterTypes()\r\n   {\r\n      Container.RegisterTypeForNavigation&lt;MainPage&gt;();\r\n   }\r\n}\r\n<\/pre>\n<p>You&#8217;ll need to add <em>using Prism.Unity;<\/em> to resolve PrismApplication.<\/p>\n<p>OnInitialized will automatically navigate to the &#8220;MainPage&#8221; view, the magic string maps to the type name MainPage as I&#8217;m sure you can see. But in the RegisterTypeForNavigation we can change this string mapping if preferred.\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s the steps for getting Prism and Xamarin Forms up and running together. For this I&#8217;m using Visual Studio 2015. Refer to a previous post on how to get a Xamarin Forms application up and running in Visual Studio 2015 if you need to. Note: I&#8217;m using the Unity container hence I&#8217;ll be demonstrating the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[73,122],"tags":[],"class_list":["post-3960","post","type-post","status-publish","format-standard","hentry","category-prism","category-xamarin-forms"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3960"}],"version-history":[{"count":26,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3960\/revisions"}],"predecessor-version":[{"id":3986,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3960\/revisions\/3986"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}