{"id":8456,"date":"2020-07-14T21:34:40","date_gmt":"2020-07-14T21:34:40","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8456"},"modified":"2020-07-14T21:48:39","modified_gmt":"2020-07-14T21:48:39","slug":"blazor-components","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/blazor-components\/","title":{"rendered":"Blazor Components"},"content":{"rendered":"<p>We can create Blazor components within our Blazor application by simply right mouse clicking on a folder or project in Visual Studio and select Add | Razor Component. <\/p>\n<p>A component derives from Microsoft.AspNetCore.Components.ComponentBase, whether implicitly, such as within a .razor file or explicitly, such as in a C# code file. <\/p>\n<p><strong>Razor component<\/strong><\/p>\n<p>If we create a .razor file we are already implementing a ComponentBase, but usually without an explicit inherit such as the following <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@inherits ComponentBase\r\n<\/pre>\n<p>With or without this @inherit, we will get a generated file (written to obj\\Debug\\netstandard2.1\\Razor) that is a class which implements the ComponentBase.<\/p>\n<p>We can add code to our .razor file in a @code block, thus combining markup and code within the same file, as per this example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@namespace BlazorApp\r\n\r\n&lt;h3&gt;@Text&lt;\/h3&gt;\r\n\r\n@code {\r\n    &#x5B;Parameter]\r\n    public string Text { get; set; }\r\n}\r\n<\/pre>\n<p><strong>Separate code and razor files<\/strong><\/p>\n<p>As alternative to a single .razor file with both markup and code, we can create a .cs file for our code. For example if our .razor file is MyComponent.razor we could create a MyComponent.cs and put our code into a C# class. The class should derive from ComponentBase and also be partial<\/p>\n<p>Hence now we have a MyComponent.razor file that looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@namespace BlazorApp\r\n\r\n&lt;h3&gt;@Text&lt;\/h3&gt;\r\n<\/pre>\n<p>and a C# file, MyComponent.cs that looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing Microsoft.AspNetCore.Components;\r\n\r\nnamespace BlazorApp\r\n{\r\n    public partial class MyComponent : ComponentBase\r\n    {\r\n        &#x5B;Parameter]\r\n        public string Text { get; set; }\r\n    }\r\n}\r\n<\/pre>\n<p>Ofcourse the use of the <em>partial<\/em> keyword is the key to this working. If we name the file MyComponent.cs all will be fine, but if we name the file <strong>MyComponent.razor.cs<\/strong> the file will appear like a code-behind file in other C# scenarios.<\/p>\n<p><strong>Code only component<\/strong><\/p>\n<p>We could also write a code only component, so let&#8217;s assume we have only a MyComponent.cs file<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing Microsoft.AspNetCore.Components;\r\nusing Microsoft.AspNetCore.Components.Rendering;\r\n\r\nnamespace BlazorApp\r\n{\r\n    public class MyComponent : ComponentBase\r\n    {\r\n        protected override void BuildRenderTree(RenderTreeBuilder builder)\r\n        {\r\n            builder.OpenElement(1, &quot;h3&quot;);\r\n            builder.AddContent(2, Text);\r\n            builder.CloseElement();\r\n        }\r\n\r\n        &#x5B;Parameter]\r\n        public string Text { get; set; }\r\n    }\r\n}\r\n<\/pre>\n<p>Obviously we have to put a little bit of effort into constructing the elements in the BuildRenderTree method, but this might suit some scenarios.<\/p>\n<p><strong>Pages and Layouts<\/strong><\/p>\n<p>Pages and Layouts are just components, just like those discussed above. <\/p>\n<p>The only difference of between a Page and component is that our class is annotated with a PageAttribute for Page. Obviously in a .razor file this happens automatically when we add a page declaration as below<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@page &quot;\/&quot;\r\n<\/pre>\n<p>As for the layout, we inherit from LayoutComponentBase, in a .razor file this looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@inherits LayoutComponentBase\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We can create Blazor components within our Blazor application by simply right mouse clicking on a folder or project in Visual Studio and select Add | Razor Component. A component derives from Microsoft.AspNetCore.Components.ComponentBase, whether implicitly, such as within a .razor file or explicitly, such as in a C# code file. Razor component If we create [&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":[305],"tags":[],"class_list":["post-8456","post","type-post","status-publish","format-standard","hentry","category-blazor"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8456","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=8456"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8456\/revisions"}],"predecessor-version":[{"id":8464,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8456\/revisions\/8464"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}