Example #1
0
        private BackgroundTaskDeferral _deferral; // Used to keep task alive
        async public void Run(IBackgroundTaskInstance taskInstance)
        {
            //cost
            var cost = BackgroundWorkCost.CurrentBackgroundWorkCost;

            if (cost == BackgroundWorkCostValue.High)
            {
                return;
            }

            //handle cancelation if needed
            var cancel = new CancellationTokenSource();

            taskInstance.Canceled += (s, e) =>
            {
                cancel.Cancel();
                cancel.Dispose();
            };

            //get deferral
            _deferral = taskInstance.GetDeferral();


            try
            {
                //progress
                var result = await UserSettings.ChangeWallpaperAsync();

                taskInstance.Progress = result;
            }
            catch (Exception ex)
            {
                _deferral.Complete();
            }
        }
        private async Task HandleChangeWallpaper(VoiceCommandServiceConnection connection, VoiceCommandUserMessage user_message)
        {
            //copy images to appdata
            var local_folder = ApplicationData.Current.LocalFolder;
            var install_path = Package.Current.InstalledLocation;
            var media_path   = await install_path.GetFolderAsync("media\\images");

            var images = await media_path.GetFilesAsync();

            foreach (var image in images)
            {
                try
                {
                    await local_folder.GetFileAsync(image.Name);

                    continue;
                }
                catch { }
                await image.CopyAsync(local_folder, image.Name, ReplaceExisting);
            }

            //change wallpaper and prepare response back to user

            var result = await UserSettings.ChangeWallpaperAsync();

            user_message.SpokenMessage = "Your wallpaper was modified, do you want me to change the lock screen as well?";

            var backup_message = new VoiceCommandUserMessage
            {
                SpokenMessage = "Change your lock screen",
            };

            var response       = VoiceCommandResponse.CreateResponseForPrompt(user_message, backup_message);
            var confirm_result = await connection.RequestConfirmationAsync(response);

            if (confirm_result.Confirmed)
            {
                await UserSettings.ChangeLockScreenAsync();

                user_message.SpokenMessage = "Your lock screen was also modified.";
                response = VoiceCommandResponse.CreateResponse(user_message);
                await connection.ReportSuccessAsync(response);
            }
            else
            {
                user_message.SpokenMessage = "okay, you're all set then";
                response = VoiceCommandResponse.CreateResponse(user_message);
                await connection.ReportSuccessAsync(response);
            }
        }
        public LandingPage()
        {
            this.InitializeComponent();
            this.Loaded += LandingPage_Loaded;


            if (ApplicationHostPage.Host != null)
            {
                ApplicationHostPage.Host.GeoLocationChanged += Host_GeoLocationChanged;
                ApplicationHostPage.Host.AttendaceChanged   += Host_AttendaceChanged;
            }



            img_banner.SetImage("ms-appx:///media/images/club1.jpg");
            img_banner.PointerPressed += Img_banner_PointerPressed;
            Feature = App.State.NextEvent.Feature;

            image_banner_menu = new PopupMenu();

            var task_name          = "should-have-been-there";
            var previous_task_list = BackgroundTaskRegistration.AllTasks.Values;
            var reg_task           = previous_task_list.Where(i => i.Name == task_name).FirstOrDefault();

            image_banner_menu.Commands.Add(new UICommand($"{(reg_task == null ? "Start" : "Stop")} wallpaper switcher", async(ui_command) =>
            {
                previous_task_list = BackgroundTaskRegistration.AllTasks.Values;
                var registered     = previous_task_list.Where(i => i.Name == task_name).FirstOrDefault();
                if (registered == null)
                {
                    var local_folder = Windows.Storage.ApplicationData.Current.LocalFolder;
                    var file         = await local_folder.CreateFileAsync("marker.txt", OpenIfExists);
                    await file.WriteTextAsync("1");

                    await ApplicationHostPage.Host.StartBackgroundTaskAsync(() =>
                    {
                        var task = new BackgroundTaskBuilder();

                        task.Name           = task_name;
                        task.TaskEntryPoint = typeof(BMX.BackgroundTasks.WallpaperSwitcherTask).ToString();
                        TimeTrigger timer   = new TimeTrigger(20, false);
                        task.SetTrigger(timer);
                        registered           = task.Register();
                        registered.Progress += async(s, args) =>
                        {
                            await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                                                           ApplicationView.GetForCurrentView().Title = $"Showing Image {args.Progress}");
                        };

                        ui_command.Label = "Stop wallpaper switcher";
                    }, "Cant do this now");
                }
                else
                {
                    //unregister if button is clicked again
                    registered.Unregister(true);
                    ui_command.Label = "Start wallpaper switcher";
                }
            }));

            image_banner_menu.Commands.Add(new UICommand("Next Wallpaper", async(ui_command) =>
            {
                await UserSettings.ChangeWallpaperAsync();
            }));
            image_banner_menu.Commands.Add(new UICommand("Next Lock Screen", async(ui_command) =>
            {
                await UserSettings.ChangeLockScreenAsync();
            }));

            //initializes the background audio task
            BackgroundMediaPlayer.MessageReceivedFromBackground += BackgroundMessageRecieved;
            _media_control = SystemMediaTransportControls.GetForCurrentView();
            _media_control.ButtonPressed += _media_control_ButtonPressed;

            _media_control.IsPreviousEnabled = true;
            _media_control.IsNextEnabled     = true;
        }