我们在所有 Blazor 页面中都有如下类似的代码,用于简单地停止渲染直到加载完成。它似乎工作正常,但该网站尚未进行太多测试。
我有点担心这个,意味着回归;在页面中间,会/可能会以一种或另一种方式扰乱 Blazors 流程,导致内存泄漏或其他问题。
我看到的每个示例都使用 if/else 代替,但如果下面的示例有效,那么最好减少页面的嵌套和复杂性。
那么,这个方法好用吗,还是会给我们带来麻烦呢?
一个简化的例子:
@usings/injects here
@if (IsLoading)
{
@:Loading content..
return;
}
<p>This will not be rendered until page is initialized and Model populated</p>
<p>@Foo.Bar</p>
@code {
public bool IsLoading { get; set; } = true;
public FooModel Foo { get; set;}
protected override async Task OnInitializedAsync()
{
try
{
Foo = await GetFooFromRepo();
}
catch (Exception ex)
{
Toaster.Add("Something went wrong when loading foo.",
MatToastType.Danger, "Error");
}
finally
{ …Run Code Online (Sandbox Code Playgroud) 我dotnet new blazorserver在多台电脑上使用 vs code 创建了多个新的 BlazorServer 项目,但总是收到此错误。
The type or namespace name 'Shared' does not exist in the namespace 'BlazorServerTutorial' (are you missing an assembly reference?) [BlazorServerTutorial]
Run Code Online (Sandbox Code Playgroud)
编译和运行没有任何问题,但不断显示错误是很烦人的。
还有其他人能够解决这个问题吗?
我正在尝试在 blazor 网页中设置实时日期时间。我尝试使用 Datetime.now 。它没有更新实时时间。我想在不使用 javascript 的情况下做到这一点。
关于这个问题有什么帮助吗?
我正在 Blazor Server (.NET 5) 中制作一个应用程序,我想隐藏特定页面的侧边栏和顶部栏。我尝试搜索,但找不到任何有效的解决方案。下面的屏幕截图显示了我想要隐藏的内容(在红色矩形中)。

显然,我希望内容/正文部分本身占据整个页面。
有没有办法在 Blazor 服务器端项目中模拟 ProtectedSessionStorage?
我尝试了下面的代码,但收到错误:“要模拟的类型 (ProtectedSessionStorage) 必须是接口、委托或非密封、非静态类。”
private readonly Mock<ProtectedSessionStorage> _sessionStorage = new();
private readonly Mock<IDataProtector> _mockDataProtector = new();
private readonly Mock<IDataProtectionProvider> _mockDataProtectionProvider = new();
//in ctor()
Services.AddSingleton(_sessionStorage.Object);
//mock IDataProtector
_mockDataProtector = new Mock<IDataProtector>();
_mockDataProtector.Setup(sut => sut.Protect(It.IsAny<byte[]>())).Returns(Encoding.UTF8.GetBytes("protectedText"));
_mockDataProtector.Setup(sut => sut.Unprotect(It.IsAny<byte[]>())).Returns(Encoding.UTF8.GetBytes("originalText"));
Services.AddSingleton(_mockDataProtector.Object);
//mock IDataProtectionProvider
_mockDataProtectionProvider = new Mock<IDataProtectionProvider>();
_mockDataProtectionProvider.Setup(s => s.CreateProtector(It.IsAny<string>())).Returns(_mockDataProtector.Object);
Services.AddSingleton(_mockDataProtectionProvider.Object);
//in testMethod()
EquipmentSearchFilterDto filter = new();
filter.HospitalID = 1;
var result = new ProtectedBrowserStorageResult<EquipmentSearchFilterDto>();
_sessionStorage.Setup(x => x.GetAsync<EquipmentSearchFilterDto>(It.IsAny<string>()))
.ReturnsAsync(new ProtectedBrowserStorageResult<EquipmentSearchFilterDto>());
Run Code Online (Sandbox Code Playgroud)
我想过将 ProtectedSessionStorage 实现隐藏在接口后面,不幸的是我无法想出一个。有任何想法吗?
我尝试在 Blazor 服务器应用程序中配置页面标题,但不幸的是,该标记<PageTitle>不起作用。这个项目是在 Microsoft 发布 .NET Core 6 之前开始的,之后我将我的应用程序迁移到了这个版本。
编译或运行都没有错误。
示例页面:
@page "/page"
<PageTitle>My page title</PageTitle>
...
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何使用 MudBlazor 表组件将“键入时搜索”功能应用到 MudTextField 组件。截至目前,它仅在按下 Enter 键时进行搜索,如此处所示 - MudBlazor 表示例
在尝试使用 ClosedXML 构建 Excel 文件时,我遇到了一些有关内存流的意外行为。在调试器中,我可以看到 MemoryStream ms 正在由该wb.SaveAs()函数填充。但是,当直接将其传递到 时DotNetStreamReference,该文件将作为 0 字节的 blob 下载。
奇怪的是,当我将流转换为字节数组并返回流时,该函数按预期工作。
我在这里错过了一些基本的东西吗?
这是使用MS 示例得出的
技术:
C# 代码
async Task ExportToExcel()
{
string fileName = $"DocumentsExport{DateTime.UtcNow.Ticks}.xlsx";
var wb = new ClosedXML.Excel.XLWorkbook();
var ws = wb.AddWorksheet("Documents");
// construct headers
ws.Cell(1, 1).SetValue<string>("Document Name");
ws.Cell(1, 2).SetValue<string>("Description");
ws.Cell(1, 3).SetValue<string>("Sort Order");
ws.Cell(1, 4).SetValue<string>("Category Name");
ws.Cell(1, 5).SetValue<string>("Group Name");
ws.Cell(1, 6).SetValue<string>("Date Modified");
// construct worksheet contents
for (int i = 0; i < documents.Count; …Run Code Online (Sandbox Code Playgroud) 我将如何向使用嵌套对象的 MudBlazor DataGrid 添加列?
例如,我有一个名为 Foo 的基本实体,它具有以下组成:
public class Foo
{
public int Id {get; set;}
public Bar Bar {get; set;}
}
public class Bar
{
public string Name {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
如何将 Bar.Name 添加到MudBlazor 的 DataGrid中的列?
<MudDataGrid Items="@someIEnumerableOfTypeFoo">
<Columns>
<Column T="Foo" Field="Id" Title="Id" />
<Column T="Foo" Field="Bar.Name" /> <!-- This Fails -->
</Columns>
</MudDataGrid>
Run Code Online (Sandbox Code Playgroud) 我使用的是 mudblazor 6.10.0,我遇到了对话框未显示的问题,即使我使用开箱即用的对话框也无法打开。我在控制台上没有收到错误,代码中也没有发生错误,但对话框根本不显示。单击按钮会调用 ToggleOpen 代码,但不显示对话框。
我的组件
@using Microsoft.AspNetCore.Components
@using MudBlazor
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">
<MudIcon Icon="@Icons.Material.Outlined.Edit" Class="mr-3 mb-n1"/>
Changes made by @User.Name
</MudText>
</TitleContent>
<DialogContent>
<MudTextField Disabled="true" Label="Before" @bind-Value="User.Change" Multiline="true" />
</DialogContent>
<DialogActions>
<MudButton Color="Color.Primary" OnClick="Close">Ok</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter] MudDialogInstance MudDialog { get; set; }
[Parameter]
public UserChange User { get; set; }
private void Close() => MudDialog.Close(DialogResult.Ok(true));
}
Run Code Online (Sandbox Code Playgroud)
与调用父组件
@page "/"
@using MudBlazor
@inject UserChangesService UserChangesService
@inject IDialogService DialogService
<MudText Typo="Typo.h2" Color="Color.Info">Welcome</MudText>
<MudGrid>
<MudItem xs="12" sm="6">
<MudCard> …Run Code Online (Sandbox Code Playgroud) blazor ×7
c# ×7
mudblazor ×3
.net-6.0 ×1
.net-core ×1
asp.net-core ×1
bunit ×1
closedxml ×1
datetime ×1
head ×1
live-update ×1
moq ×1
namespaces ×1
xunit ×1