我有一个与此问题类似的问题,因为我无法将 Blazor EditForm 绑定到简单的列表。
为了将列表绑定到 EditForm,我是否遗漏了某些内容?
人物.cs
public class Person {
public List<string>? Names { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
EditForm1.razor 会产生编译时错误:Cannot assign to 'item' because it is a 'foreach iteration variable'。我明白了 - 迭代器是只读的,所以我可以理解这一点。
<EditForm Model="@person">
@if (person is not null) {
@if (person.Names is not null) {
@foreach (var item in person.Names) {
<InputText @bind-Value="@item" />
}
}
}
</EditForm>
Run Code Online (Sandbox Code Playgroud)
因此,根据引用的 Microsoft 文档,我对其进行了重构。
EditForm2.razor 编译并运行...直到 person.Names 实际上具有值。然后它抛出ArgumentException: The provided expression contains a InstanceMethodCallExpression1 …
我的 ef 核心有问题。我有两个从数据库读取数据的服务。在一个页面上调用第一个服务,在第二个页面上调用第二个服务。当我单击按钮创建新程序时,出现错误。我通常从带有注入服务的页面调用它。有人可以帮我吗?
builder.Services.AddDbContextPool<Context>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("Connection"));
});
Run Code Online (Sandbox Code Playgroud)
测试服务1:
public class TestService1 : ITestService1
{
private readonly Context _context;
private readonly IMapper _mapper;
public TestService1(Context context, IMapper mapper)
{
_kreativgangContext = kreativgangContext;
_mapper = mapper;
}
public virtual async Task<AllProgramViewModel> HandleAsync(AllProgramFilterViewModel filter)
{
var model = new AllProgramViewModel();
var data = _context.Programs.Where(x => (EF.Functions.Like(x.Name ?? "", "%" + filter.Name + "%") || string.IsNullOrEmpty(filter.Name)))
.Select(x => new Core.Models.Program() { ID = x.ID, Name = x.Name, Order = x.Order });
result.Model.TotalCount …Run Code Online (Sandbox Code Playgroud) 我有一个 .net cor 3.1 Blazor 服务器项目。我在这个项目中使用默认的身份系统。我想在不登录的情况下访问特定的剃须刀页面。我怎么做?任何想法?
我尝试添加@attribute [AllowAnonymous]到剃刀页面。但这对我不起作用
_Host.cshtml
page "/"
@using Microsoft.AspNetCore.Authorization
@namespace has.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@attribute [Authorize]
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>has</title>
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<link href="css/custom_styles.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="_content/AntDesign/css/ant-design-blazor.css" rel="stylesheet" />
<link href="_content/Blazor.ContextMenu/blazorContextMenu.min.css" rel="stylesheet" />
<script src="_content/BlazorInputFile/inputfile.js"></script>
<script src="~/js/site.js"></script>
</head>
<body>
<app>
<component type="typeof(App)" …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 blazor 服务器应用程序的后台服务中访问存储在 appsettings.json 文件中的数据,但大多数有关如何在线执行此操作的插图都是基于 Blazor 的 Webassemble 配置。我想我应该使用 Microsoft.Extensions.Configuration 包,但我不太清楚如何实现它。
下面是后台服务及其基本代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Timers;
using log4net;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
namespace BlazorServerApp
{
public class JobExecutedEventArgs : EventArgs { }
public class PeriodicExecutor : IDisposable
{
Timer _Timer;
bool _Running;
public void StartExecuting()
{
if (!_Running)
{
// Initiate a Timer
_Timer = new Timer();
_Timer.Interval = 5000;
_Timer.Elapsed += HandleTimer;
_Timer.AutoReset = true; …Run Code Online (Sandbox Code Playgroud) 我将一篇文章中的以下代码放置在 .razor 文件中。如何将 wig-pig 代码部分转换为 .razor.cs 文件后面的代码?
[Parameter] public RenderFragment<TItem> ItemTemplate { get; set; }
protected override void OnParametersSet()
{
if (ItemTemplate == null)
{
ItemTemplate = (item) => @:@{
<li @key=item>@item.ToString()</li>}
;
}
}
Run Code Online (Sandbox Code Playgroud) 我遇到了与此问题中概述的问题类似的问题。
在本地运行时使用自定义环境时,某些文件不会加载(导致 404 等)。
.UseStaticWebAssets()使用进行呼叫WebApplicationBuilder?在我的program.cs中,我尝试添加一些代码来调用,.UseStaticWebAssets()如下所示,但我看到了错误ConfigureWebHost() is not supported by WebApplicationBuilder.Host. Use the WebApplication returned by WebApplicationBuilder.Build() instead。
// program.cs
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureWebHostDefaults(builder =>
{
builder.UseStaticWebAssets();
});
// etc...
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试WebHost在上述构建器上使用 prop,我会在尝试调用该UseStaticWebAssets()方法时看到编译器错误。
我有一个 Blazor 服务器应用程序 (.net 6) 页面,其中有一个选择下拉列表,该列表设置为允许多个选定项目。当页面加载时,我需要设置默认值。如果我只想设置一个选定的值,则此代码可以正常工作。但是,如果我想设置多个值,它就不起作用。当我想要设置多个值时,如何设置选择列表默认值?这是我所拥有的:
<select value ="@SelectedCities" @onchange="@((ev) => ListChangeEvent(ev, row_id ))" class="form-select" aria-label="Default select example" multiple rows="4">
@foreach (var i in item.values.ToList())
{
<option id="@i.Key">@i.Value</option>
}
</select>
public string[] SelectedCities { get; set; } = new[] { "New York", "Pittsburgh" };
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用。没有错误,它只是没有选择这两个项目。
但是,如果 SelectedCities 不是数组,如下所示:
public string SelectedCities { get; set; } = "New York";
Run Code Online (Sandbox Code Playgroud)
效果很好。
谢谢
听说Blazer服务器是SSR(服务器端渲染)。顺便说一句,我知道 Blazer 服务器是单页应用程序 (SPA)。Blazer服务器是否同时具有SSR和SPA?那么Blazer服务器是不是先通过SSR方式接收数据,再通过CSR方式接收数据来实现SPA呢?
c# single-page-application server-side-rendering blazor blazor-server-side
我正在尝试从 Blazor InputFile 组件上传多个图像:
<InputFile OnChange="@LoadFiles" multiple accept=".png, .jpg, .jpeg, .gif" />
Run Code Online (Sandbox Code Playgroud)
一旦进入内存,我需要使用 SkiaSharp 将它们的大小调整为最大宽/高 1000 像素。我为此创建了一个辅助方法,但它似乎在任何超过大约 4MB 的图像上失败。我通过搜索以及任何可用的(相当缺乏的)Microsoft 文档将以下逻辑拼凑在一起,因此我的方法可能完全错误:
using SkiaSharp;
public static byte[] Resize(byte[] fileContents, int maxWidth, int maxHeight)
{
using MemoryStream ms = new(fileContents);
using SKBitmap sourceBitmap = SKBitmap.Decode(ms); // <-- EXCEPTION HERE ON LARGER FILES
int height = Math.Min(maxHeight, sourceBitmap.Height);
int width = Math.Min(maxWidth, sourceBitmap.Width);
var quality = SKFilterQuality.High;
using SKBitmap scaledBitmap = sourceBitmap.Resize(new SKImageInfo(width, height), quality);
using SKImage scaledImage = SKImage.FromBitmap(scaledBitmap);
using SKData data = …Run Code Online (Sandbox Code Playgroud) 我以前的编程是 Windows 窗体,其中有一个 UI 线程,任何涉及 UI 的代码都必须在该线程中调用。一旦你掌握了它的窍门,这不是一个大问题,但你必须小心地始终调用该线程。
Blazor(服务器端)有相同的概念吗?我猜不会,因为无论线程如何,所有东西都将某些东西推入电路,然后该电路在客户端成为单个线程。
但我觉得还是问一下比较好。从哪些线程调用哪些代码有任何限制吗?
以及附加的后续问题。我接到的每个电话都OnInitializedAsync在该 UI 线程中 - 对吗?我从 Blazor 和 DevExpress 组件实现了所有这些异步事件。虽然它们都是异步的,但它们都在主 UI 线程中 - 对吗?
这意味着如果我创建后台线程或计时器,我只需关心这一点。(在 WinForms 的情况下,当您收到事件/回调时,您经常处于非 UI 线程中。)
c# ×9
blazor ×7
.net-6.0 ×2
.net ×1
appsettings ×1
asp.net ×1
asp.net-core ×1
memorystream ×1
skiasharp ×1