英文:
SignalR causing null reference exception while adding custom dialog services
问题
我们正在尝试使用SignalR添加自定义服务,但在调用自定义服务时会抛出空异常。如果我们在mainLayout.razor中指定组件标记为<BlazorApp3.DialogForms.Test/>,则项目无法运行。
示例链接: https://uploadnow.io/f/Y59l8d9
附带示例中是否缺少任何配置?
需要在没有任何异常的情况下运行具有自定义对话服务的SignalR示例。
英文:
We are trying to add custom services with SignalR, but it throws a null exception when we invoke the custom services. If we specify the component tag in mainLayout.razor like <BlazorApp3.DialogForms.Test/>, it is unable to run the project.
Sample: https://uploadnow.io/f/Y59l8d9
enter image description here
enter image description here
Are we missing any configuration in the attached sample?
Need to run the signalR with custom dialog service sample without any exception
答案1
得分: 1
将您的 DialogService
类更改如下,问题可能会得到解决。
using System;
using Microsoft.AspNetCore.Components;
namespace BlazorApp3.DialogForms
{
public class DialogService
{
private event Action<DialogOptions> dialogInstance;
public event Action<DialogOptions> DialogInstance
{
add => dialogInstance += value;
remove => dialogInstance -= value;
}
public async Task Open(DialogOptions options)
{
// 调用 Test 以更新并显示具有选项的对话框
this.dialogInstance?.Invoke(options);
}
}
}
英文:
Change your DialogService
class like below and the issue could be fixed.
using System;
using Microsoft.AspNetCore.Components;
namespace BlazorApp3.DialogForms
{
public class DialogService
{
private event Action<DialogOptions> dialogInstance;
public event Action<DialogOptions> DialogInstance
{
add => dialogInstance += value;
remove => dialogInstance -= value;
}
public async Task Open(DialogOptions options)
{
// Invoke Test to update and show the dialog with options
this.dialogInstance?.Invoke(options);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论