Files
BeWoPlaner/BeWoLauncher/View/Components/MessageDialog.xaml.cs
2022-09-14 10:57:51 +02:00

123 lines
3.8 KiB
C#

using BeWoLauncher.ServiceProxy;
using BS.SharedLauncher;
using BS.SharedLauncher.Enums;
using BS.SharedLauncher.Information;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Threading;
using System.Xaml;
using XamlReader = System.Windows.Markup.XamlReader;
namespace BeWoLauncher.Components
{
public partial class MessageDialog
{
public MessageDialog()
{
InitializeComponent();
DataContext = this;
OkayButton.Visibility = Visibility.Collapsed;
}
public MessageDialog(Action pAfterPasswordConfirmationCallBack)
{
IsInConfirmationWithPasswordMode = true;
AfterPasswordConfirmationCallBack = pAfterPasswordConfirmationCallBack;
InitializeComponent();
DataContext = this;
OkayButton.IsDefault = true;
CountButton.IsDefault = false;
}
public Action AfterPasswordConfirmationCallBack { get; set; }
public bool IsInConfirmationWithPasswordMode { get; set; }
private ICommand _OpenBrowserCommand;
public ICommand OpenBrowserCommand => _OpenBrowserCommand ?? (_OpenBrowserCommand = new OpenBrowserCommandImpl());
public class OpenBrowserCommandImpl : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (parameter != null)
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(parameter.ToString()));
}
}
}
public void SetXaml(string xamlCode)
{
try
{
if(XamlReader.Parse(xamlCode) is UIElement element)
{
txtMessage.Visibility = Visibility.Collapsed;
rootGrid.Children.Add(element);
}
}
catch(Exception e)
{
//ignore
}
}
public string Message
{
get => txtMessage.Text;
set => txtMessage.Text = value;
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
//DialogResult = true;
Close();
}
private void OpenBrowserCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (e.OriginalSource is Hyperlink)
{
System.Diagnostics.Process.Start(
new System.Diagnostics.ProcessStartInfo((e.OriginalSource as Hyperlink).NavigateUri.ToString()));
}
}
private void OpenBrowserCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}
private void OkayButton_OnClick(object sender, RoutedEventArgs e)
{
LauncherServiceFacade.DoLauncherServiceAsync(u => u.IsUserValid(ConnectionInformation.Username, PasswordBox.Password, null, null, false), result =>
{
if(result == UserValidationResult.UserValid)
{
AfterPasswordConfirmationCallBack?.Invoke();
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)Close);
}
else
{
MessageBox.Show("Anmeldeinformationen nicht bekannt. Bitte überprüfen Sie den Benutzernamen und das Passwort!", "Fehler bei der Anmeldung", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}, true);
}
}
}