Sharing Code and Resources (Part 5)
Making The Blog Look Like
A Blog
- In the MyBlog.Shared project, open the Pages/index.razor file.
- We no longer need fake blog posts, so let's remove the button from the top of the page, as well as the <br /> tag.
- Remove the AddSomePosts method. Now, when we have an admin, we can create our posts.
- Inside the Virtualize component, change the content (RenderFragment). Also remove the <ul> tag.
- Add a using
statement for Markdig at the top of the file- @using Markdig;
- Add an OnInitializedAsync method that will handle the instantiation of the Markdig pipeline.
- Now, run the project using Ctrl + F5 and take a look at our new front page.
Sharing Problems
- In the MyBlog.Shared project, open Pages/Admin/BlogPostEdit.razor. Always perform null checks.
- Add a null check around the category loop.
- Add a null check around the tag loop.
Note- It's
good to keep in mind to always check for nulls, but sometimes these things
sneak past us. When sharing code, it is always good to go through the code one
more time.
CSS Isolation
In .NET 5, Microsoft
introduced a feature known as isolated CSS. This functionality is similar to
offerings in many other frameworks. The concept revolves around creating CSS
that is tailored for a single component.
The template for Blazor
uses isolated CSS for Shared/MainLayout.razor and NavMenu.Razor.
If you expand Shared/MainLayout.razor in the MyBlogWebAssebly.Client
project, you will see a file called MainLayout.razor. css.
You can also use SASS
here by adding a file called MainLayout.razor.scss. It is important that
the file we add should generate a file called MainLayout. razor.css in
order for the compiler to pick it up. This is a naming
convention that will make sure to rewrite CSS and the HTML output. CSS has the
following naming convention-
.main {
flex: 1;
}
For all of this to
happen, we also need to have a link to the CSS (which is provided by the
template). This way, our users won't have to add a reference to the CSS and
there is no risk of the CSS breaking something in the user's app (since it's
isolated).
Conclusion
we have successfully moved
components into a shared library and used that library with both the Blazor
Server and Blazor WebAssembly projects. We have also learned how we can use
dependency injection to use different ways of accessing data depending on the
platform including the use of both regular CSS and isolated CSS.
Comments
Post a Comment