private void LoadConnections()
 {
     ThreadHelper.JoinableTaskFactory.Run(async delegate
     {
         await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
         var devKitConnections = VsixHelper.GetDevKitConnections();
         comboBoxSavedConnection.ItemsSource = devKitConnections.CrmConnections;
         if (devKitConnections.DefaultCrmConnection != null)
         {
             comboBoxSavedConnection.SelectedItem = devKitConnections.CrmConnections.FirstOrDefault(x => x.Name == devKitConnections.DefaultCrmConnection);
         }
         buttonOK.IsEnabled = comboBoxSavedConnection.Items.Count > 0;
     });
 }
        private void ButtonCheckConnection_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!IsValid())
            {
                return;
            }
            var crmConnection = new CrmConnection
            {
                Name     = textboxName.Text,
                Password = comboBoxType.Text == "ClientSecret" ? textboxPassword.Password : EncryptDecrypt.EncryptString(textboxPassword.Password),
                Type     = comboBoxType.Text,
                Url      = textboxUrl.Text,
                UserName = textboxUser.Text
            };

            stackPanelForm.IsEnabled = false;
            progressBar.Visibility   = System.Windows.Visibility.Visible;
            _ = Task.Factory.StartNew(() =>
            {
                var crmServiceClient = XrmHelper.IsConnected(crmConnection);
                ThreadHelper.JoinableTaskFactory.Run(async delegate
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    stackPanelForm.IsEnabled = true;
                    progressBar.Visibility   = System.Windows.Visibility.Hidden;
                });
                if (crmServiceClient != null)
                {
                    var devKitConnections = VsixHelper.GetDevKitConnections();
                    devKitConnections.DefaultCrmConnection = crmConnection.Name;
                    devKitConnections.CrmConnections.Add(crmConnection);
                    VsixHelper.SaveDevKitConnections(devKitConnections);
                    LoadConnections();
                    ClearData();
                }
                else
                {
                    ThreadHelper.JoinableTaskFactory.Run(async delegate
                    {
                        await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                        await VS.MessageBox.ShowErrorAsync(@"Something wrong with your connection. Please try it again");
                    });
                }
            }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
        }
 private bool IsValid()
 {
     if (comboBoxType.Text.Length == 0)
     {
         VS.MessageBox.ShowError($"Please select Type");
         comboBoxType.Focus();
         return(false);
     }
     if (textboxName.Text.Length == 0)
     {
         VS.MessageBox.ShowError($"Please enter {Const.CrmString} Name");
         textboxName.Focus();
         return(false);
     }
     if (textboxUrl.Text.Length == 0)
     {
         VS.MessageBox.ShowError("Please enter Url");
         textboxUrl.Focus();
         return(false);
     }
     if (textboxUser.Text.Length == 0)
     {
         VS.MessageBox.ShowError($"Please enter {labelUser.Content}");
         textboxUser.Focus();
         return(false);
     }
     if (textboxPassword.Password.Length == 0)
     {
         VS.MessageBox.ShowError($"Please enter {labelPassword.Content}");
         textboxPassword.Focus();
         return(false);
     }
     if (VsixHelper.GetDevKitConnections().CrmConnections.Any(x => x.Name == textboxName.Text))
     {
         VS.MessageBox.ShowError($"Name already used");
         textboxName.Focus();
         return(false);
     }
     return(true);
 }