我创建了一个新的 ASP.NET Core 3.0 Web 应用程序并选择了模型-视图-控制器选项。我想添加 Blazor 服务器端,所以我将以下内容添加到 Startup.cs 文件中。
services.AddServerSideBlazor();
endpoints.MapBlazorHub();
Run Code Online (Sandbox Code Playgroud)
并添加了脚本文件
<script src="_framework/blazor.server.js"></script>
Run Code Online (Sandbox Code Playgroud)
在我的布局文件中。
我创建了一个简单的组件来显示我在文本框中输入的内容,并将该组件添加到我的Index.cshtml视图中。它在 Visual Studio 中工作,但是当我将它推送到我的内部 2016 服务器时,该组件已呈现,但文本未显示。该应用程序似乎无法找到该blazor.server.js文件。
我是否缺少其他一些部署步骤来推送 JS 文件?
在 Blazor .razor文件中,您可以@typeparam MyType使用泛型参数。例如:
MyComponent.razor
@typeparam MyType
<SomeHtml />
@code
{
[Parameter]
public List<MyType> MyList{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
所以你可以调用:
<MyComponent MyType="MyTypeABC" MyList="@MyData.MyList" />
Run Code Online (Sandbox Code Playgroud)
但我更喜欢后面的代码(razor.cs),我如何使用像@typeparam MyTyperazor.cs 文件中的类型的参数?
我目前的解决方法是:
MyComponent.razor
@inherits MyComponentCode<MyType>
@typeparam MyType
Run Code Online (Sandbox Code Playgroud)
MyComponent.razor.cs
public class MyComponentCode<MyType> : ComponentBase
{
[Parameter]
public List<MyType> MyList{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想念类似的东西[TypeParameter],但也许有更好的解决方案,有什么想法吗?或者也许这是一个关于“如何在后面的代码中使用剃刀@statements”的一般问题。
2020-02-27 更新:
根据Roger Wolf 的建议(见下文),一个更好的方法:
MyComponent.razor
@typeparam MyType
Run Code Online (Sandbox Code Playgroud)
MyComponent.razor.cs
public partial class MyComponent<MyType>
{
[Parameter]
public List<MyType> MyList{ …Run Code Online (Sandbox Code Playgroud) 我正在学习 Blazor 技术。我在 VS 2019 中启动了一个默认的增量项目,并且我已经使用 confirm() 和 alert 修改了 Decrement 的代码,但它不起作用。
@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Increment</button>
<button class="btn btn-primary btn-danger" onclick="if (confirm('Are you sure to Decrement')) { @DecrementCount() }">Decrement</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
private void DecrementCount()
{
currentCount--;
// alert('Operation Successfully executed')
}
}
Run Code Online (Sandbox Code Playgroud)
在我的代码片段中,confirm() 函数运行良好,但我想调用 Decrement 函数不起作用构建失败。我想在我的函数中添加一条成功消息。请提供任何选项,而不是使用 confirm(),alert() 函数。
我有:
<input onkeypress="@Foo" />
Run Code Online (Sandbox Code Playgroud)
和:
@functions
{
void Foo(UIKeyboardEventArgs e)
{
}
}
Run Code Online (Sandbox Code Playgroud)
如何传递或以其他方式检索处理程序方法中的输入值?
我正在尝试使用blazor.Net,这是一个实验框架。
我已经在这个Framework及其出色的工具中开发了一个小项目。
但是在11月14日Blazor语言服务最近更新之后,我在模板选择中看到了owo选项。
首先是Blazor(托管ASP.NET Core)
其次是Blazor(ASP.NET Core中的服务器端)
没有关于它们之间差异的信息,
谁能告诉我这两个模板之间的区别是什么,何时应选择哪个?
我尝试在 Blazor 上编写一个简单的蛇游戏,但找不到任何方法将我的事件绑定到文档上。
有机会在不同元素上绑定事件,例如div或input。
例如像:<input onkeypress="@KeyPressInDiv"/>
在哪里public void KeyPressInDiv(UIKeyboardEventArgs ev)
{....}
,我想,应该有一定的类比JavaScript方法document.onkeydown = function (evt) {}
我已经找到了几种方法解决此问题的工作:
1)使用JS绑定和调用blazor代码(摘自https://github.com/ aesalazar/AsteroidsWasm )
document.onkeydown = function (evt) {
evt = evt || window.event;
DotNet.invokeMethodAsync('Test.ClientSide', 'JsKeyDown', evt.keyCode);
//Prevent all but F5 and F12
if (evt.keyCode !== 116 && evt.keyCode !== 123)
evt.preventDefault();
};
document.onkeyup = function (evt) {
evt = evt || window.event;
DotNet.invokeMethodAsync('Test.ClientSide', 'JsKeyUp', evt.keyCode);
//Prevent all but F5 and F12
if (evt.keyCode !== 116 …Run Code Online (Sandbox Code Playgroud) 有什么区别
@(await Html.RenderComponentAsync<Todo>(RenderMode.ServerPrerendered))
Run Code Online (Sandbox Code Playgroud)
和
@(await Html.RenderComponentAsync<Todo>(RenderMode.Server))
Run Code Online (Sandbox Code Playgroud)
我正在查看文档,但无法真正找到解释差异的内容。并且也不太明白枚举上的代码注释:
// Summary:
// Renders a marker for a Blazor server-side application. This doesn't include any
// output from the component. When the user-agent starts, it uses this marker to
// bootstrap a blazor application.
Server = 2,
//
// Summary:
// Renders the component into static HTML and includes a marker for a Blazor server-side
// application. When the user-agent starts, it uses this marker to bootstrap a blazor
// application.
ServerPrerendered = …Run Code Online (Sandbox Code Playgroud) Razor Pages用于服务器端 Web 应用程序,就像过去一样。
Blazor旨在提供流行的 JavaScript 框架(如 Angular 或 React)的替代方案,以创建(主要)在客户端浏览器中运行的单页应用程序 (SPA)。
但是,我也听说过服务器端 Blazor,这让我很困惑。根据这个答案,服务器端 Blazor 只是在服务器上运行的 Razor 组件。但是 Razor Pages 和 Razor Components 之间有什么区别?
注意:我不是想弄清楚哪个更好或“正确的选择”。我只是想弄清楚存在哪些技术差异。
Blazor 服务器是一项出色的技术,但它经常因 SignalR 无法重新连接到服务器而出现故障。
如何在生产中解决这个问题?我让人们将笔记本电脑置于睡眠状态或将手机与网站连接 5 秒钟,然后“尝试重新连接”。
并且总是失败。用户等待只是为了看到“重新加载”按钮。
即使网站在移动浏览器或休眠电脑的浏览器中未处于活动状态,是否仍需要克服此问题并强制重新连接 SignalR?
我有两个组件。第一个组件包含模型列表,第二个组件包含模态表单我想在第一个组件中点击模型在第二个组件中,打开模态并编辑模型如何从父组件调用子组件中的显示功能
<ChildComponent />
<button onClick="@ShowModal">show modal</button>
@code{
ChildComponent child;
void ShowModal(){
child.Show();
}
}
Run Code Online (Sandbox Code Playgroud)
我用过@using,但这段代码有错误:
找不到类型或命名空间名称 ChildComponent
blazor ×10
c# ×5
asp.net-core ×2
.net ×1
.net-core ×1
asp.net ×1
asp.net-mvc ×1
razor ×1
webassembly ×1