Interaction logic for MyDialog.xaml
Inheritance: DialogWindow, IDisposable
Example #1
0
        private SelectedDevice GetSelectedDevice(ConnectedServiceHandlerContext context, string ioTHubUri, string primaryKey)
        {
            var connectionString = string.Format(CultureInfo.InvariantCulture,
                                                 "HostName={0};SharedAccessKeyName=iothubowner;SharedAccessKey={1}",
                                                 ioTHubUri, primaryKey);

            var registryManager = RegistryManager.CreateFromConnectionString(connectionString);
            var devicesTask     = registryManager.GetDevicesAsync(1000);

            SelectedDevice deviceId = null;

            Func <string, Task <Device> > newDeviceCreator = (string deviceId2) => CreateNewDevice(context, registryManager, deviceId2);

            using (var dlg = new DeviceSelectionDialog(devicesTask, newDeviceCreator))
            {
                var dlgResult = dlg.ShowModal();
                if (dlgResult.HasValue && dlgResult.Value)
                {
                    var id  = dlg.SelectedDeviceID;
                    var key = dlg.Devices.First(_ => _.Id == id).Authentication.SymmetricKey;
                    deviceId = new SelectedDevice {
                        Id = id, Key = key.PrimaryKey
                    };
                }
            }
            return(deviceId);
        }