简体   繁体   English

如何从 Blazor WebAssembly 中的 Razor Page 访问 body 标签或其他标签?

[英]How can I reach body tag or another tag from Razor Page in Blazor WebAssembly?

I'm trying to reach to body from a Razor page.我正在尝试从 Razor 页面到达正文。 I have just started to learn Blazor.我刚刚开始学习 Blazor。 So I don't know if it is possible or not.所以我不知道这是否可能。 Basically I'm trying to change the body class from the C# side and from different pages.基本上,我试图从 C# 端和不同页面更改主体类。 I can do it with JavaScript but I want to do it with Blazor WebAssembly.我可以用 JavaScript 来做,但我想用 Blazor WebAssembly 来做。 It can work if the body is inside of the Razor page.如果正文位于 Razor 页面内,则它可以工作。 But I don't want to use it that way.但我不想那样使用它。

index.html :索引.html

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="utf-8" />
</head>
<body class="theme-dark">
    <app>Loading...</app>
    <script src="_framework/blazor.webassembly.js"></script>
</body>
</html>

MainLayout.razor : MainLayout.razor

@inherits LayoutComponentBase
<div class="grid-wrapper sidebar-bg bg1">
    <div id="theme-tab">
        <div class="theme-tab-item switch-theme bg-white" @onclick="ToggleTheme" data-theme="theme-default" title="Light"></div>
        <div class="theme-tab-item switch-theme bg-dark" @onclick="ToggleTheme" data-theme="theme-dark" title="Dark"></div>
    </div>
    <NavMenu />
    <SideBar />
    <div class="main">
        @Body
    </div>
    <Footer />
</div>

@code {
    private bool isDark = true;
    private string currentTheme => isDark ? "theme-dark" : "theme-default"; //I want to use this variable in other pages.

    private void ToggleTheme()
    {
        isDark = !isDark;
    }
}

If you want to change out the <body> class如果你想改变<body>

Add a JS file.添加一个JS文件。

site.js网站.js


window.SetBodyCss = function (elementId, classname) {
    var link = document.getElementById(elementId);
    if (link !== undefined) {
        link.className = classname;
    }
    return true;
}

Reference it in Host.cshtml .Host.cshtml 中引用它。
Create an id on <body> .<body>上创建一个id

<body id="BlazorMainBody">

    <script src="/site.js"></script>
    <script src="_framework/blazor.server.js"></script>
</body>

Add a helper class添加辅助类

using Microsoft.JSInterop;
using System.Threading.Tasks;

namespace Blazor.Starter
{
    public class AppJsInterop
    {
        protected IJSRuntime JSRuntime { get; }

        public AppJsInterop(IJSRuntime jsRuntime)
        {
            JSRuntime = jsRuntime;
        }

        public ValueTask<bool> SetBodyCss(string elementId, string cssClass)
          => JSRuntime.InvokeAsync<bool>("SetBodyCss", elementId, cssClass);
    }
}

This shows toggling it in a page这显示在页面中切换它

<button type="button" @onclick="SetCss">SetCss</button>

@code {
    [Inject] private IJSRuntime _js { get; set; }

    private bool _isbodyCss;

    private string _bodyCss => _isbodyCss ? "Theme-Dark" : "Theme-Light";

    private async Task SetCss()
    {
        var appJsInterop = new AppJsInterop(_js);
        await appJsInterop.SetBodyCss("BlazorMainBody", _bodyCss);
        _isbodyCss = !_isbodyCss;
    }
}

You can change out the whole stylesheet in a similar way - see this note of mine您可以以类似的方式更改整个样式表 - 请参阅我的这篇笔记

wrap your Layout HTML in body tag.将您的布​​局 HTML 包装在 body 标签中。

I needed the navbar-collapsed class in the body in some layouts and navbar-expand in some layouts.我需要在某些布局中的主体中使用导航栏折叠类,在某些布局中使用导航栏扩展。 By default it was navbar-expand so in the other layout where I needed collapsed I just wrapped my layout HTML in BODY tag with "class=navbar-collapsed" and it worked.默认情况下,它是 navbar-expand,所以在我需要折叠的其他布局中,我只是将布局 HTML 包裹在带有“class=navbar-collapsed”的 BODY 标签中,并且它起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将“查询参数”从 a.razor 页面传递到 ASP.NET 核心 Blazor 中的 .cshtml 页面 - How can I pass 'Query Params' from a .razor page to .cshtml page in ASP.NET Core Blazor Blazor 基本标签和@page 指令 in.razor 文件 - Blazor base-tag and @page instruction in .razor files 如何在 Blazor 中创建 Razor 页面列表 - How can I create a List of Razor Page in Blazor 如何从C#到达href标签的内容以插入链接? - How can i reach my content of href tag from c# to insert a link? 在 Blazor 中,如何动态更改 HTML 标记? - In Blazor, how can I dynamically change an HTML tag? 如何将属性从剃刀页面模型传递给 asp-route-id 标签助手 - How do I pass a property from a razor page model to asp-route-id tag helper Blazor WebAssembly 与服务器端 Razor 页面授权问题 - Blazor WebAssembly with server side Razor page authorize problem 我如何从 <seealso> 标签? - How can I reference a Members page from a <seealso> tag? 如何在特定的另一个 aspx 页面中加载一个 aspx 页面<div>标签</div><div id="text_translate"><p>我想用来自另一个 *.aspx 页面的内容更新页面的某个部分,从而避免只有一个(非常)长的代码页面。</p><p> 所以:如何在特定标签中的另一个 aspx 页面中加载一个 aspx 页面,而不必重新加载整个页面。</p></div> - How can I load an aspx page in another aspx page in a specific <div> tag 我如何导航到另一个 blazor 页面但没有 state 重置 - How can i navigate to another blazor page but without state reset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM