예제 #1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            var deferral = taskInstance?.GetDeferral();

            try
            {
                var result = await _bingWallpaperService.GetAsync(0, 1, _bingoWallpaperSettings.SelectedArea);

                var image = result?.Images.FirstOrDefault();
                if (image != null)
                {
                    var copyright = image.Copyright;
                    var text      = Regex.Replace(copyright, @"\(©.*", string.Empty).Trim();
                    _tileService.UpdatePrimaryTile(image, text);

                    if (_bingoWallpaperSettings.IsAutoUpdateWallpaper || _bingoWallpaperSettings.IsAutoUpdateLockScreen)
                    {
                        using (var client = new HttpClient())
                        {
                            var bytes = await client.GetByteArrayAsync(_bingWallpaperService.GetUrl(image, _bingoWallpaperSettings.SelectedWallpaperSize));

                            if (bytes != null && bytes.Length > 0)
                            {
                                if (_bingoWallpaperSettings.IsAutoUpdateWallpaper)
                                {
                                    await _systemSettingService.SetWallpaperAsync(bytes);
                                }
                                if (_bingoWallpaperSettings.IsAutoUpdateLockScreen)
                                {
                                    await _systemSettingService.SetLockScreenAsync(bytes);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                // ignored
            }
            finally
            {
                deferral?.Complete();
            }
        }