首页 > 解决方案 > 如何订阅 Blazor.Radzen DialogService onClose 事件

问题描述

我正在使用blazor.radzen中非常有用的对话框。在我为模型创建的服务 (ModelService) 中,我有一个取消函数,它可以回滚对模型所做的任何更改。这可以使用对话框内的按钮触发,如下所示:

<RadzenButton ButtonStyle="ButtonStyle.Light" Icon="cancel" Text="Cancel" Click="@(args => Cancel(CurrentItem))" />

但是我需要在对话框关闭时触发此功能,以便取消对模型所做的更改。

DialogService(Radzen) 确实提供了 OnClose 事件。它似乎是这样声明的:

public event Action<dynamic> OnClose;

在 Visual Studio 中闲逛,我在 Dialog Service 的元数据中发现了这一点

public class DialogService : IDisposable
    {
        protected List<TaskCompletionSource<dynamic>> tasks;
        protected List<object> dialogs;

        public DialogService(NavigationManager uriHelper);

        public event Action<dynamic> OnClose;
        public event Action OnRefresh;
        public event Action<string, Type, Dictionary<string, object>, DialogOptions> OnOpen;

        public void Close(dynamic result = null);
        [AsyncStateMachine(typeof(<Confirm>d__25))]
        public Task<bool?> Confirm(string message = "Confirm?", string title = "Confirm", ConfirmOptions options = null);
        public void Dispose();
        public void Open<T>(string title, Dictionary<string, object> parameters = null, DialogOptions options = null) where T : ComponentBase;
        public void Open(string title, RenderFragment<DialogService> childContent, DialogOptions options = null);
        public Task<dynamic> OpenAsync<T>(string title, Dictionary<string, object> parameters = null, DialogOptions options = null) where T : ComponentBase;
        public Task<dynamic> OpenAsync(string title, RenderFragment<DialogService> childContent, DialogOptions options = null);
        public void Refresh();
    }

我找不到处理程序的签名,所以我不知道如何订阅该事件。我也想知道订阅活动的最佳地点在哪里。我应该在 OnInitialized 上做吗?

标签: c#blazorblazor-server-side

解决方案


[Inject]
protected DialogService dialogService { get; set; }    

protected override void OnInitialized()
{
    dialogService.OnOpen += Open;
    dialogService.OnClose += Close;
}

private void Close(dynamic obj)
{
    //throw new NotImplementedException();
}

private void Open(string arg1, Type arg2, Dictionary<string, object> arg3, DialogOptions arg4)
{
    //throw new NotImplementedException();
}

推荐阅读