public static void RegisterAumidAndComServer <T>(string aumid)
            where T : NotificationActivator
        {
            if (string.IsNullOrWhiteSpace(aumid))
            {
                throw new ArgumentException("You must provide an AUMID.", nameof(aumid));
            }

            // If running as Desktop Bridge
            if (DesktopBridgeHelpers.IsRunningAsUwp())
            {
                _aumid = null;
                _registeredAumidAndComServer = true;
                return;
            }

            _aumid = aumid;

            string exename   = Assembly.GetExecutingAssembly().GetName().Name;
            string shortpath = "\\Microsoft\\Windows\\Start Menu\\A" + exename;
            var    shortcut  = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
                               + shortpath + $"\\{_aumid}.lnk";

            if (!File.Exists(shortcut))
            {
                Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + shortpath);
                // 不需要从通知打开程序则不需要这项操作
                RegisterComServer <T>(Process.GetCurrentProcess().MainModule.FileName);
                CreatShortcut <T>(shortcut);
            }

            _registeredAumidAndComServer = true;
        }
        private static void EnsureRegistered()
        {
            // If not registered AUMID yet
            if (!_registeredAumidAndComServer)
            {
                // Check if Desktop Bridge
                if (DesktopBridgeHelpers.IsRunningAsUwp())
                {
                    // Implicitly registered, all good!
                    _registeredAumidAndComServer = true;
                }

                else
                {
                    // Otherwise, incorrect usage
                    throw new Exception("You must call RegisterAumidAndComServer first.");
                }
            }

            // If not registered activator yet
            if (!_registeredActivator)
            {
                // Incorrect usage
                throw new Exception("You must call RegisterActivator first.");
            }
        }
        /// <summary>
        /// If not running under the Desktop Bridge, you must call this method to register your AUMID with the Compat library and to
        /// register your COM CLSID and EXE in LocalServer32 registry. Feel free to call this regardless, and we will no-op if running
        /// under Desktop Bridge. Call this upon application startup, before calling any other APIs.
        /// </summary>
        /// <param name="aumid">An AUMID that uniquely identifies your application.</param>
        public static void RegisterAumidAndComServer <T>(string aumid)
            where T : NotificationActivator
        {
            if (string.IsNullOrWhiteSpace(aumid))
            {
                throw new ArgumentException("You must provide an AUMID.", nameof(aumid));
            }

            // If running as Desktop Bridge
            if (DesktopBridgeHelpers.IsRunningAsUwp())
            {
                // Clear the AUMID since Desktop Bridge doesn't use it, and then we're done.
                // Desktop Bridge apps are registered with platform through their manifest.
                // Their LocalServer32 key is also registered through their manifest.
                _aumid = null;
                _registeredAumidAndComServer = true;
                return;
            }

            _aumid = aumid;

            String exePath = Process.GetCurrentProcess().MainModule.FileName;

            RegisterComServer <T>(exePath);

            _registeredAumidAndComServer = true;
        }
Пример #4
0
        /// <summary>
        /// If not running under the Desktop Bridge, you must call this method to register your app information with the notification platform.
        /// Feel free to call this regardless, and we will no-op if running under Desktop Bridge. Call this upon application startup, before sending toasts.
        /// </summary>
        /// <typeparam name="T">Your implementation of the notification activator.</typeparam>
        /// <param name="aumid">The AUMID to use if not running under Desktop Bridge. This should be unique and different from your Desktop Bridge app's AUMID.</param>
        /// <param name="displayName">The display name to use if not running under Desktop Bridge.</param>
        /// <param name="logo">The app logo to use if not running under Desktop Bridge.</param>
        /// <param name="logoBackgroundColor">The background color to use with your logo if not running under Desktop Bridge. This should be in hex #AARRGGBB format, like "#FF0063B1", or "transparent" if you want the system accent color to be used.</param>
        /// <returns></returns>
        public static Task RegisterWithPlatformAsync <T>(string aumid, string displayName, string logo, string logoBackgroundColor)
            where T : NotificationActivator
        {
            if (typeof(T) == typeof(NotificationActivator))
            {
                throw new ArgumentException("You must provide an implementation of your NotificationActivator.");
            }

            if (string.IsNullOrWhiteSpace(aumid))
            {
                throw new ArgumentException("You must provide an AUMID.", nameof(aumid));
            }

            if (string.IsNullOrWhiteSpace(displayName))
            {
                throw new ArgumentException("You must provide a display name.", nameof(displayName));
            }

            if (string.IsNullOrWhiteSpace(logo))
            {
                throw new ArgumentException("You must provide an app logo.", nameof(logo));
            }

            if (string.IsNullOrWhiteSpace(logoBackgroundColor))
            {
                throw new ArgumentException("You must provide a logo background color.", nameof(logoBackgroundColor));
            }

            // If running as Desktop Bridge
            if (DesktopBridgeHelpers.IsRunningAsUwp())
            {
                // Clear the AUMID since Desktop Bridge doesn't use it, and then we're done.
                // Desktop Bridge apps are registered with platform through their manifest.
                _aumid      = null;
                _registered = true;
                return(Task.CompletedTask);
            }

            // Cache their AUMID
            _aumid = aumid;

            // In the future, there'll be a new platform API that we can call to register, which will likely be async,
            // which is why this method is flagged async.
            String shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + $"\\Microsoft\\Windows\\Start Menu\\Programs\\{displayName}.lnk";

            // Find the path to the current executable
            String exePath = Process.GetCurrentProcess().MainModule.FileName;

            InstallShortcut <T>(shortcutPath, exePath, aumid);

            _registered = true;
            return(Task.CompletedTask);
        }
Пример #5
0
        private static void EnsureRegistered()
        {
            // If not registered
            if (!_registered)
            {
                // Check if Desktop Bridge
                if (DesktopBridgeHelpers.IsRunningAsUwp())
                {
                    // Implicitly registered, all good!
                    _registered = true;
                }

                else
                {
                    // Otherwise, incorrect usage
                    throw new Exception("You must call RegisterWithPlatformAsync first.");
                }
            }
        }
Пример #6
0
            public static bool IsRunningAsUwp()
            {
                if (DesktopBridgeHelpers._isRunningAsUwp == null)
                {
                    if (DesktopBridgeHelpers.IsWindows7OrLower)
                    {
                        DesktopBridgeHelpers._isRunningAsUwp = false;
                    }
                    else
                    {
                        int           length = 0;
                        StringBuilder sb     = new StringBuilder(0);
                        int           result = DesktopBridgeHelpers.GetCurrentPackageFullName(ref length, sb);

                        sb     = new StringBuilder(length);
                        result = DesktopBridgeHelpers.GetCurrentPackageFullName(ref length, sb);

                        DesktopBridgeHelpers._isRunningAsUwp = result != DesktopBridgeHelpers.APPMODEL_ERROR_NO_PACKAGE;
                    }
                }

                return(DesktopBridgeHelpers._isRunningAsUwp.Value);
            }