Completed
Last Updated: 20 Apr 2022 11:34 by ADMIN
Release 3.3.0
Matthias
Created on: 09 Dec 2020 11:52
Category: Notification
Type: Feature Request
27
Method for closing/hiding the notification
I would like to have a method for the reference of the component which will allow me to close the notifications, for example - Close().
2 comments
ADMIN
Marin Bratanov
Posted on: 25 Jan 2021 10:09

Hello,

I must note that we do not support inheriting our components and we cannot guarantee such hacks will not cause issues.

 

Regards,
Marin Bratanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

jura
Posted on: 25 Jan 2021 07:58

Here is my take before it gets implemented:

 

using HaSaM.Systems;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Telerik.Blazor.Components;

namespace HaSaM.Web.Components.Interactions
{
    public class TelerikNotification :
        Telerik.Blazor.Components.TelerikNotification
    {
        public TelerikNotification()
        {
            Type type = typeof(Telerik.Blazor.Components.TelerikNotification);
            notifications = type.CreatePropertyGetMethodDelegate<List<NotificationModel>>(nameof(Notifications), BindingFlags.NonPublic, this);
            hideNotification = type.CreateMethodDelegate<Action<NotificationModel>>(nameof(HideNotification), BindingFlags.NonPublic, this);
        }

        public IEnumerable<NotificationModel> Notifications => notifications();

        public bool Hide(NotificationModel model)
        {
            bool found = Notifications.Contains(model);
            if (found)
                HideNotification(model);
            return found;
        }

        private void HideNotification(NotificationModel notification) => hideNotification(notification);

        private readonly Func<List<NotificationModel>> notifications;
        private readonly Action<NotificationModel> hideNotification;
    }
}

 

public static Func<T> CreatePropertyGetMethodDelegate<T>(this Type type, string name, BindingFlags bindingAttr = BindingFlags.Public, object instance = null)
        {
            Validate(type);
            ValidateName(name);
            bindingAttr = AddStaticOrInstanceFlag(bindingAttr, instance);
            return (Func<T>)type.
                GetProperty(name, bindingAttr)?.
                GetGetMethod(nonPublic: bindingAttr.HasFlag(BindingFlags.NonPublic))?.
                CreateDelegate(typeof(Func<T>), instance);
        }

        public static T CreateMethodDelegate<T>(this Type type, string name, BindingFlags bindingAttr = BindingFlags.Public, object instance = null)
            where T : Delegate
        {
            Validate(type);
            ValidateName(name);
            bindingAttr = AddStaticOrInstanceFlag(bindingAttr, instance);
            return (T)type.
                GetMethod(name, bindingAttr)?.
                CreateDelegate(typeof(T), instance);
        }

        private static BindingFlags AddStaticOrInstanceFlag(BindingFlags bindingAttr, object instance) => bindingAttr | (instance is null ?
            BindingFlags.Static :
            BindingFlags.Instance);

        private static void Validate(Type type) => Argument.NonNull(type, nameof(type));
        private static void ValidateName(string name) => Argument.NonNullOrWhiteSpace(name, nameof(name));