예제 #1
0
        /// <summary>
        /// Show toast notification.
        /// </summary>
        /// <param name="arguments">Notification arguments object.</param>
        /// <returns>Toast notification object.</returns>
        public static ToastNotification ShowToast(NotificationArguments arguments)
        {
            var notifier  = ToastNotificationManager.CreateToastNotifier(arguments.ApplicationId);
            var scheduled = notifier.GetScheduledToastNotifications();

            for (var i = 0; i < scheduled.Count; i++)
            {
                // The itemId value is the unique ScheduledTileNotification.Id assigned to the notification when it was created.
                if (scheduled[i].Id == "")
                {
                    notifier.RemoveFromSchedule(scheduled[i]);
                }
            }

            //Set the toast visual
            var visual = new ToastVisual()
            {
                BindingGeneric = new ToastBindingGeneric()
                {
                    Children =
                    {
                        new AdaptiveText()
                        {
                            Text = arguments.Title
                        },
                        new AdaptiveText()
                        {
                            Text = arguments.Message
                        }
                    }
                }
            };

            //Set the image
            var imagePath = Globals.GetImageOrDefault(arguments.PicturePath);

            visual.BindingGeneric.AppLogoOverride = new ToastGenericAppLogo()
            {
                Source   = imagePath,
                HintCrop = ToastGenericAppLogoCrop.Circle
            };

            // Construct the actions for the toast (inputs and buttons)

            var actions = new ToastActionsCustom();

            // Add any inputs
            foreach (var input in arguments.Inputs)
            {
                actions.Inputs.Add(
                    new ToastTextBox(input.Id)
                {
                    Title = input.Title,
                    PlaceholderContent = input.PlaceHolderText
                });
            }

            // Add any buttons
            foreach (var button in arguments.Buttons)
            {
                actions.Buttons.Add(
                    new ToastButton(button.Text, new QueryString()
                {
                    { "Notification ID", arguments.NotificationId },
                    { "Button ID", button.Id },
                    { "Arguments", button.Arguments }
                }.ToString())
                {
                    ActivationType = ToastActivationType.Background
                });
            }

            //Set the audio
            ToastAudio audio = null;

            if (!string.IsNullOrWhiteSpace(arguments.WindowsSound) || !string.IsNullOrWhiteSpace(arguments.SoundPath))
            {
                string sound;
                if (string.IsNullOrWhiteSpace(arguments.WindowsSound))
                {
                    sound = "file:///" + arguments.SoundPath;
                }
                else
                {
                    sound = $"ms-winsoundevent:{arguments.WindowsSound}";
                }

                audio = new ToastAudio()
                {
                    Src    = new Uri(sound),
                    Loop   = bool.Parse(arguments.Loop),
                    Silent = arguments.Silent
                };
            }

            // Construct the toast content
            var toastContent = new ToastContent()
            {
                Visual  = visual,
                Actions = actions,
                Audio   = audio
            };

            // Create notification
            var toast = new ToastNotification(toastContent.GetXml());

            // Set the expiration time
            if (!string.IsNullOrWhiteSpace(arguments.Duration))
            {
                switch (arguments.Duration)
                {
                case "short":
                    toast.ExpirationTime = DateTime.Now.AddSeconds(5);
                    break;

                case "long":
                    toast.ExpirationTime = DateTime.Now.AddSeconds(25);
                    break;
                }
            }

            //Add event handlers
            var events = new NotificationEvents();

            toast.Activated += events.Activated;
            toast.Dismissed += events.Dismissed;
            toast.Failed    += events.Failed;

            //Show notification
            ToastNotificationManager.CreateToastNotifier().Show(toast);

            return(toast);
        }
예제 #2
0
        /// <summary>
        /// Show toast notification.
        /// </summary>
        /// <param name="arguments">Notification arguments object.</param>
        /// <returns>Toast notification object.</returns>
        public static async Task <ToastNotification> ShowToast(NotificationArguments arguments)
        {
            //Set the toast visual
            var visual = new ToastVisual()
            {
                BindingGeneric = new ToastBindingGeneric()
                {
                    Children =
                    {
                        new AdaptiveText()
                        {
                            Text = arguments.Title
                        },
                        new AdaptiveText()
                        {
                            Text = arguments.Message
                        }
                    }
                }
            };

            //Set the attribution text
            if (!string.IsNullOrWhiteSpace(arguments.AttributionText))
            {
                visual.BindingGeneric.Attribution = new ToastGenericAttributionText()
                {
                    Text = arguments.AttributionText
                };
            }

            //Set the logo override
            var imagePath       = Globals.GetImageOrDefault(arguments.PicturePath);
            var isInternetImage = IsInternetImage(imagePath);
            var imageSource     = isInternetImage ? await DownloadImageToDisk(imagePath) : imagePath;

            visual.BindingGeneric.AppLogoOverride = new ToastGenericAppLogo()
            {
                Source   = imageSource,
                HintCrop = ToastGenericAppLogoCrop.Circle
            };

            //Set a background image
            if (!string.IsNullOrWhiteSpace(arguments.Image))
            {
                isInternetImage = IsInternetImage(arguments.Image);
                imageSource     = isInternetImage ? await DownloadImageToDisk(arguments.Image) : arguments.Image;

                visual.BindingGeneric.Children.Add(new AdaptiveImage()
                {
                    Source = imageSource
                });
            }

            // Construct the actions for the toast (inputs and buttons)
            var actions = new ToastActionsCustom();

            // Add any inputs
            if (arguments.Inputs != null)
            {
                foreach (var input in arguments.Inputs)
                {
                    var textBox = new ToastTextBox(input.Id)
                    {
                        PlaceholderContent = input.PlaceHolderText
                    };

                    if (!string.IsNullOrWhiteSpace(input.Title))
                    {
                        textBox.Title = input.Title;
                    }
                    actions.Inputs.Add(textBox);
                }
            }

            // Add any buttons
            if (arguments.Buttons != null)
            {
                foreach (var button in arguments.Buttons)
                {
                    actions.Buttons.Add(new ToastButton(button.Text, button.Arguments));

                    //Background activation is not needed the COM activator decides whether
                    //to process in background or launch foreground window
                    //actions.Buttons.Add(new ToastButton(button.Text, button.Arguments)
                    //{
                    //	ActivationType = ToastActivationType.Background
                    //});
                }
            }

            //Set the audio
            ToastAudio audio = null;

            if (!string.IsNullOrWhiteSpace(arguments.WindowsSound) || !string.IsNullOrWhiteSpace(arguments.SoundPath))
            {
                string sound;
                if (string.IsNullOrWhiteSpace(arguments.WindowsSound))
                {
                    sound = "file:///" + arguments.SoundPath;
                }
                else
                {
                    sound = $"ms-winsoundevent:{arguments.WindowsSound}";
                }

                audio = new ToastAudio()
                {
                    Src    = new Uri(sound),
                    Loop   = bool.Parse(arguments.Loop),
                    Silent = arguments.Silent
                };
            }

            // Construct the toast content
            var toastContent = new ToastContent()
            {
                Visual  = visual,
                Actions = actions,
                Audio   = audio
            };

            // Create notification
            var xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(toastContent.GetContent());

            var toast = new ToastNotification(xmlDocument);

            // Set the expiration time
            if (!string.IsNullOrWhiteSpace(arguments.Duration))
            {
                switch (arguments.Duration)
                {
                case "short":
                    toast.ExpirationTime = DateTime.Now.AddSeconds(5);
                    break;

                case "long":
                    toast.ExpirationTime = DateTime.Now.AddSeconds(25);
                    break;
                }
            }

            //Add event handlers
            var events = new NotificationEvents();

            toast.Activated += events.Activated;
            toast.Dismissed += events.Dismissed;
            toast.Failed    += events.Failed;

            //Show notification
            DesktopNotificationManagerCompat.CreateToastNotifier().Show(toast);

            return(toast);
        }
예제 #3
0
        /// <summary>
        /// Show toast notification.
        /// </summary>
        /// <param name="arguments">Notification arguments object.</param>
        /// <returns>Toast notification object.</returns>
        public static ToastNotification ShowToast(NotificationArguments arguments)
        {
            var notifier  = ToastNotificationManager.CreateToastNotifier(arguments.ApplicationId);
            var scheduled = notifier.GetScheduledToastNotifications();

            for (var i = 0; i < scheduled.Count; i++)
            {
                // The itemId value is the unique ScheduledTileNotification.Id assigned to the notification when it was created.
                if (scheduled[i].Id == "")
                {
                    notifier.RemoveFromSchedule(scheduled[i]);
                }
            }

            XmlDocument toastXml;

            //Set the image
            //if (string.IsNullOrWhiteSpace(arguments.PicturePath))
            //{
            //	if (string.IsNullOrWhiteSpace(arguments.Title))
            //	{
            //		toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
            //		var stringElements = toastXml.GetElementsByTagName("text");
            //		stringElements[0].AppendChild(toastXml.CreateTextNode(arguments.Message));
            //	}
            //	else
            //	{
            //		toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
            //		var stringElements = toastXml.GetElementsByTagName("text");
            //		stringElements[0].AppendChild(toastXml.CreateTextNode(arguments.Title));
            //		stringElements[1].AppendChild(toastXml.CreateTextNode(arguments.Message));
            //	}
            //}
            //else
            //{
            if (string.IsNullOrWhiteSpace(arguments.Title))
            {
                toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01);
                var stringElements = toastXml.GetElementsByTagName("text");
                stringElements[0].AppendChild(toastXml.CreateTextNode(arguments.Message));
            }
            else
            {
                toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText02);
                var stringElements = toastXml.GetElementsByTagName("text");
                stringElements[0].AppendChild(toastXml.CreateTextNode(arguments.Title));
                stringElements[1].AppendChild(toastXml.CreateTextNode(arguments.Message));
            }
            //}

            var imagePath = Globals.GetImageOrDefault(arguments.PicturePath);

            var imageElements = toastXml.GetElementsByTagName("image");

            imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;

            if (arguments.Silent)
            {
                SetSilentAttribute(toastXml);
            }

            if (!string.IsNullOrWhiteSpace(arguments.WindowsSound) || !string.IsNullOrWhiteSpace(arguments.SoundPath))
            {
                SetSoundAttribute(arguments, toastXml);
            }

            SetBrandingAttribute(toastXml);

            var toast  = new ToastNotification(toastXml);
            var events = new NotificationEvents();

            toast.Activated += events.Activated;
            toast.Dismissed += events.Dismissed;
            toast.Failed    += events.Failed;

            //SetCommandsAttribute is commented out as is not used at this moment.
            //SetCommandsAttribute(toastXml);

            SetVisualAttribute(toastXml);

            if (!string.IsNullOrWhiteSpace(arguments.Duration))
            {
                SetDurationAttribute(toastXml, arguments.Duration);
            }

            Debug.WriteLine(toastXml.GetXml());

            ToastNotificationManager.CreateToastNotifier(arguments.ApplicationId).Show(toast);

            return(toast);
        }