public MainWindow() { InitializeComponent(); DataContext = this; Client = new Client(); var registered = false; do { var input = new UserNameInput(); input.ShowDialog(); var name = input.NicknameBox.Text.Trim(); try { registered = Client.Register(name); } catch (EndpointNotFoundException) { MessageBox.Show("Cannot connect to server, are you sure it is running?", "No server found", MessageBoxButton.OK, MessageBoxImage.Error); Close(); return; } if (!registered) { MessageBox.Show("There is already a user with selected username, please choose another one!", "Username already exist", MessageBoxButton.OK, MessageBoxImage.Information); } } while (!registered); Client.SendConnectionMessage(); Client.MessageIncomeEvent += ClientOnMessageIncomeEvent; Client.NewUserJoinedEvent += ClientOnNewUserJoinedEvent; Client.UserDisconnectEvent += ClientOnUserDisconnectEvent; Client.UserIsOnlineEvent += ClientOnUserIsOnlineEvent; Client.ImageMessageIncomeEvent += ClientOnImageMessageIncomeEvent; ChatBox.IsReadOnly = true; ReceiverComboBox.Items.Add("All"); ReceiverComboBox.SelectedIndex = 0; Title += $": {Client.Name}"; FocusManager.SetFocusedElement(this, InputBox); ChatBox.Document = new FlowDocument(); }