private void SwitchRoomInfoGrid(bool Enter) { if (Enter) { var FadeOutAnimation = new DoubleAnimation() { From = InProgressGrid.Opacity, To = 0, Duration = TimeSpan.FromSeconds(0.3), EasingFunction = new ExponentialEase { EasingMode = EasingMode.EaseIn } }; FadeOutAnimation.Completed += delegate { InProgressGrid.Visibility = Visibility.Hidden; RoomInfoGrid.Visibility = Visibility.Visible; RoomNameLabel.Content = CurrentRoom.Name; BattleTypeLabel.Content = BattleTypeDictionary[CurrentRoom.BattleType]; if (CurrentRoom.Host.Avator != null) { using (MemoryStream Stream = new MemoryStream()) { CurrentRoom.Host.Avator.Save(Stream, ImageFormat.Png); BitmapImage Temp = new BitmapImage(); Temp.BeginInit(); Temp.CacheOption = BitmapCacheOption.OnLoad; Temp.StreamSource = Stream; Temp.EndInit(); HostImg.ImageSource = Temp; } } HostNameBlock.Text = CurrentRoom.Host.Name; HostIntroBlock.Text = CurrentRoom.Host.Introduction; RoomDescriptionBlock.Text = CurrentRoom.Description; ParticipantStack.Children.Clear(); foreach (var Participant in CurrentRoom.Members) { var Item = new ParticipantDisplaySmall() { Height = 35, Width = 325 }; if (Participant.Avator != null) { using (MemoryStream Stream = new MemoryStream()) { Participant.Avator.Save(Stream, ImageFormat.Png); BitmapImage Temp = new BitmapImage(); Temp.BeginInit(); Temp.CacheOption = BitmapCacheOption.OnLoad; Temp.StreamSource = Stream; Temp.EndInit(); Item.AvatorImage.ImageSource = Temp; } } Item.UserName.Text = Participant.Name; ParticipantStack.Children.Add(Item); } RoomInfoGrid.BeginAnimation(OpacityProperty, new DoubleAnimation() { From = RoomInfoGrid.Opacity, To = 1, Duration = TimeSpan.FromSeconds(0.3), EasingFunction = new ExponentialEase { EasingMode = EasingMode.EaseOut } }); }; InProgressGrid.BeginAnimation(OpacityProperty, FadeOutAnimation); } else { var FadeOutAnimation = new DoubleAnimation() { From = RoomInfoGrid.Opacity, To = 0, Duration = TimeSpan.FromSeconds(0.3), EasingFunction = new ExponentialEase { EasingMode = EasingMode.EaseIn } }; FadeOutAnimation.Completed += delegate { RoomInfoGrid.Visibility = Visibility.Hidden; InProgressGrid.Visibility = Visibility.Visible; SetRoomInfoBox("链接房间中...", Colors.Orange); ResetInProgress(); InProgressGrid.BeginAnimation(OpacityProperty, new DoubleAnimation() { From = InProgressGrid.Opacity, To = 1, Duration = TimeSpan.FromSeconds(0.3), EasingFunction = new ExponentialEase { EasingMode = EasingMode.EaseOut } }); }; RoomInfoGrid.BeginAnimation(OpacityProperty, FadeOutAnimation); } }
private void Link_Click(object Sender, RoutedEventArgs E) { if (Linking) { return; } Linking = true; if (string.IsNullOrWhiteSpace(InvitationBox.Text)) { SetStatus("邀请码不可为空。", Colors.OrangeRed); Linking = false; return; } if (RoomInfoBox.Visibility == Visibility.Hidden) { RoomInfoBox.Visibility = Visibility.Visible; RoomInfoBox.BeginAnimation(OpacityProperty, new DoubleAnimation() { From = 0, To = 1, Duration = TimeSpan.FromSeconds(0.75), EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseOut } }); RoomInfoBox.BeginAnimation(MarginProperty, new ThicknessAnimation() { From = new Thickness( RoomInfoBox.Margin.Left - 50, RoomInfoBox.Margin.Top, RoomInfoBox.Margin.Right + 50, RoomInfoBox.Margin.Bottom), To = RoomInfoBox.Margin, Duration = TimeSpan.FromSeconds(0.75), EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseOut } }); } SetRoomInfoBox("链接房间中...", Colors.Orange); ResetInProgress(); if (RoomInfoGrid.Visibility == Visibility.Visible) { DoubleAnimation RoomInfoGridOpacityAnimation = new DoubleAnimation() { From = 1, To = 0, Duration = TimeSpan.FromSeconds(0.5), EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseIn } }; RoomInfoGridOpacityAnimation.Completed += delegate { RoomInfoGrid.Visibility = Visibility.Hidden; InProgressGrid.Visibility = Visibility.Visible; InProgressGrid.BeginAnimation(OpacityProperty, new DoubleAnimation() { From = 0, To = 1, Duration = TimeSpan.FromSeconds(0.5), EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseOut } }); }; RoomInfoGrid.BeginAnimation(OpacityProperty, RoomInfoGridOpacityAnimation); } Thread ConnectionThread = new Thread(() => { string Text = ""; this.Dispatcher.Invoke(() => { Text = InvitationBox.Text; SetStatus("正在解析邀请码...", Colors.Orange, false); }); try { if (Text.Length == 1) { throw new FormatException("邀请码长度有误。"); } Text = Encryption.Decrypt(Text); } catch (Exception Ex1) { this.Dispatcher.Invoke(() => { SetRoomInfoBox("房间链接失败!", Colors.Red); SetStatus("邀请码解码失败。", Colors.Red); SetFailedText("无法解析的邀请码格式。\n" + Ex1.Message); }); Linking = false; return; } string[] Fragments = Text.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (Fragments.Length != 4) { this.Dispatcher.Invoke(() => { SetRoomInfoBox("房间链接失败!", Colors.Red); SetStatus("邀请码解码失败。", Colors.Red); SetFailedText("邀请码长度有误,信息缺失。"); }); Linking = false; return; } IPAddress Address; try { Address = IPAddress.Parse(Fragments[0]); } catch (Exception Ex2) { this.Dispatcher.Invoke(() => { SetRoomInfoBox("房间链接失败!", Colors.Red); SetStatus("IP地址解析失败。", Colors.Red); SetFailedText(Ex2.Message); }); Linking = false; return; } RoomName = Fragments[2]; RoomHost = Fragments[3]; this.Dispatcher.Invoke(() => { SetStatus("已确定IP地址,正在链接" + RoomHost + "的房间:" + RoomName + "...", Colors.Orange, false); }); App.Client = new TcpClient(); App.Client.ConnectFailed += ServerConnectFailed; App.Client.ConnectCompleted += ServerConnectCompleted; App.Client.ConnectAsync(new IPEndPoint(Address, App.Port)); }) { Name = "房间链接线程" }; ConnectionThread.Start(); }