Пример #1
0
        private void CustomSnowFallFromAppServiceAsync(
            IBackgroundTaskInstance instance,
            AppServiceTriggerDetails details)
        {
            var def = instance.GetDeferral();

            instance.Canceled += (_, __) => { def.Complete(); };
            details.AppServiceConnection.RequestReceived += (_, message) =>
            {
                var messageDef = message.GetDeferral();
                try
                {
                    try
                    {
                        // lecture du message
                        var targetUri = message.Request.Message.FirstOrDefault(kVp => kVp.Key == "targetUri").Value?.ToString();
                        if (!string.IsNullOrEmpty(targetUri))
                        {
                            // Uri locale = pas de téléchargement
                            if (targetUri.StartsWith("ms-appx"))
                            {
                                DispatcherHelper.CheckBeginInvokeOnUIAsync(() => SnowFallAsync(true, new BitmapImage(new Uri(targetUri))));
                                return;
                            }

                            DispatcherHelper.CheckBeginInvokeOnUIAsync(
                                async() =>
                            {
                                // téléchargement local de l'image
                                var image = await Microsoft.Toolkit.Uwp.UI.ImageCache.GetFromCacheAsync(new Uri(targetUri));

                                SnowFallAsync(true, image);
                            });
                        }
                    }
                    finally
                    {
                        // fermeture du message
                        messageDef.Complete();
                    }
                }
                finally
                {
                    // On ne fera pas d'autres communications
                    def.Complete();
                }
            };
        }