{"id":8075,"date":"2020-04-03T21:14:14","date_gmt":"2020-04-03T21:14:14","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8075"},"modified":"2020-04-03T21:14:14","modified_gmt":"2020-04-03T21:14:14","slug":"razor-templates-in-your-xamarin-application","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/razor-templates-in-your-xamarin-application\/","title":{"rendered":"Razor templates in your Xamarin application"},"content":{"rendered":"<p>I had a situation where I wanted to have a template file made up of HTML along with data supplied by my Xamarin application. There are several options to do this but a Razor template file would be a perfect fit, but how to embed one in my (non-Web) application?<\/p>\n<p><em>Note: Whilst this post talks about a Xamarin application this should be usable in other types of application.<\/em><\/p>\n<ul>\n<li>I created a Templates folder (not required for this to work) in my solution.<\/li>\n<li>Within the Templates folder I add a new text file but saved with a .cshtml extension, for example EmployeeTemplate.cshtml. Visual Studio 2019 doesn&#8217;t have a specific template for Xamarin application, but just naming it with the correct extension works.<\/li>\n<li>Select the file in the solution explorer and open it&#8217;s properties, set the Custom Tool to <strong>RazorTemplatePreprocessor<\/strong>.<\/li>\n<\/ul>\n<p>Here&#8217;s a snippet of the contents within the Razor template file<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&lt;div class=&quot;row&quot;&gt;\r\n  &lt;div&gt;Date: @Model.DateEntered&lt;\/div&gt;\r\n    &lt;div&gt;\r\n      &lt;div&gt;Employee Number:&lt;\/div&gt;\r\n      &lt;div&gt;@Model.OrderDetails.EmployeeNumber&lt;\/div&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>Visual Studio will (due the use of the RazorTemplatePreprocessor Custom Tool) generate a .cs file that is a class that we can pass our model into and will process the template file and output a string representing the combination of the template with our model. <\/p>\n<p>To generate our output string we simply use the following code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar template = new EmployeeTemplate() \r\n{ \r\n  Model = viewModel \r\n};\r\n\r\nvar result = template.GenerateString();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I had a situation where I wanted to have a template file made up of HTML along with data supplied by my Xamarin application. There are several options to do this but a Razor template file would be a perfect fit, but how to embed one in my (non-Web) application? Note: Whilst this post talks [&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":[3,292],"tags":[],"class_list":["post-8075","post","type-post","status-publish","format-standard","hentry","category-c","category-razor"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8075","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=8075"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8075\/revisions"}],"predecessor-version":[{"id":8079,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8075\/revisions\/8079"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}