Пример #1
0
        public async Task EnableServiceAsync(IService service)
        {
            if (service is null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            PlaySpaceElement space       = SelectedElement;
            string           serviceName = service.GetType().GetSimpleTypeName();

            if (service is IPairedService pairedService)
            {
                var msg = new PairServiceMessage(pairedService);
                Messenger.Default.Send(msg);
                if (msg.Result == null)
                {
                    throw new OperationCanceledException();
                }

                var servicePairings = space.Pairings?.ToDictionary(kvp => kvp.Key, kvp => (IReadOnlyList <PairedDevice>) new List <PairedDevice> (kvp.Value))
                                      ?? new Dictionary <string, IReadOnlyList <PairedDevice> >();
                if (!servicePairings.TryGetValue(serviceName, out var pairings))
                {
                    servicePairings[serviceName] = pairings = new List <PairedDevice> ();
                }

                var pairing = await msg.Result;
                ((List <PairedDevice>)pairings).Add(new PairedDevice {
                    Id   = pairing.Item1.Id,
                    Pair = pairing.Item2
                });

                space = space with {
                    Pairings = servicePairings
                };
            }

            // Paired services may ask user to enable if their only paired devices aren't present, but we don't need to re-add the service
            if (!space.Services.Contains(serviceName))
            {
                var services = new List <string> (space.Services.Count + 1);
                services.AddRange(space.Services);
                services.Add(serviceName);

                space = space with {
                    Services = services
                };
            }

            await SyncService.SaveElementAsync(space);

            await Loading;
        }
Пример #2
0
        private async void OnPairService(PairServiceMessage msg)
        {
            var vm = new PairServiceViewModel(msg.PairedService);

            msg.Result = vm.PairResult;

            await this.rootFrame.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, async() => {
                var dialog = new PairDialog {
                    DataContext = vm
                };
                await dialog.ShowAsync();
            });
        }