[英]Javascript Interop Dispose Problem with blazor serverside
When ever refresh or manually navigate to a Blazor page that is using javascript interop, it errors out because the dispose function no longer exists in javascript.当刷新或手动导航到使用 javascript 互操作的 Blazor 页面时,它会出错,因为 javascript 中不再存在 dispose 函数。
Is there a way to not run "dispose" on a component that implements IDisposable when its a refresh or a navigation?有没有办法在刷新或导航时不在实现 IDisposable 的组件上运行“处理”? Is there where the "ElementReference" type would help? “ElementReference”类型有什么帮助吗?
Here's some code for context:这是上下文的一些代码:
My blazor component implements IDisposable:我的 blazor 组件实现了 IDisposable:
@implements IDisposable
this runs my Dispose function, which calls on my interop file:这将运行我的 Dispose 函数,该函数调用我的互操作文件:
public void Dispose()
{
JqxWindowJsInterop.Dispose();
}
my JsInterop runs this call to javascript:我的 JsInterop 运行这个对 javascript 的调用:
public void Dispose()
{
jsRuntime.InvokeAsync<string>(
"jqxWindowComponent.dispose",
InstanceId);
_JqxWindowJsInteropReference?.Dispose();
}
which finally runs this in javascript:它最终在 javascript 中运行:
window.jqxWindowComponent = {
dispose: function (instanceId) {
console.log('jqxWindowComponent.dispose : ' + instanceId);
if ($('#' + instanceId).length) {
$('#' + instanceId).jqxWindow('destroy');
}
delete jqxWindowList[instanceId];
}};
when I refresh or navigate to/from to this page through the browser I get this error当我通过浏览器刷新或导航到/从该页面导航到此页面时,我收到此错误
System.NullReferenceException: 'Object reference not set to an instance of an object.'MyNameSpace.Components.JqxWindowComponent.JqxWindowJsInterop.get returned null. System.NullReferenceException: '未将对象引用设置为对象的实例。'MyNameSpace.Components.JqxWindowComponent.JqxWindowJsInterop.get 返回 null。
Any help appreciated.任何帮助表示赞赏。
I was able to solve this by adding a render check property.我能够通过添加渲染检查属性来解决这个问题。
private bool firstRenderComplete;
I set it here:我在这里设置:
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
firstRenderComplete = true;
DayPilotJsInterop = new DayPilotJsInterop(JavascriptRunTime, InstanceId);
await DayPilotJsInterop.Initialize();
}
}
and finally test it here:最后在这里测试一下:
public void Dispose()
{
if(firstRenderComplete == true)
{
DayPilotJsInterop.Dispose();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.