Пример #1
0
        public void Connect()
        {
            if (!IsConnected)
            {
                if (_hostRdp == null)
                {
                    _hostRdp            = new RdpHost();
                    _hostRdp.Rdp.Server = ConnectionSettings.Server;

                    if (int.TryParse(ConnectionSettings.Port, out var port))
                    {
                        _hostRdp.Rdp.Port = port;
                    }

                    _hostRdp.Rdp.OnDisconnected += Rdp_OnDisconnected;
                }

                var credentials = ConnectionSettings.GetCredentials();
                if (credentials != null)
                {
                    if (!string.IsNullOrEmpty(credentials.Domain))
                    {
                        _hostRdp.Rdp.Domain = credentials.Domain;
                    }
                    if (!string.IsNullOrEmpty(credentials.Username) && !string.IsNullOrEmpty(credentials.Password))
                    {
                        _hostRdp.Rdp.UserName = credentials.Username;
                        _hostRdp.Rdp.Password = credentials.GetPassword();
                    }
                }

                // Connection settings.
                _hostRdp.Rdp.ConnectingText   = Resources.Connecting + " " + ConnectionSettings.Server;
                _hostRdp.Rdp.DisconnectedText = Resources.Disconnected + " " + ConnectionSettings.Server;

                if (_hostGrid == null)
                {
                    _hostGrid = new Grid();
                    _hostGrid.Children.Add(new WindowsFormsHost {
                        Child = _hostRdp
                    });
                    _hostGrid.SizeChanged += Host_SizeChanged_Initial;

                    Observable
                    .FromEventPattern <SizeChangedEventArgs>(_hostGrid, "SizeChanged")
                    .Throttle(TimeSpan.FromSeconds(1))
                    .Subscribe(x => _hostGrid?.Dispatcher.Invoke(UpdateSessionDisplaySettings));
                }
                else
                {
                    PrepareSessionDisplaSettingsAndConnect();
                }

                UI = _hostGrid;

                IsConnected = true;
            }
        }
Пример #2
0
        public void Destroy()
        {
            if (_hostGrid != null)
            {
                _hostGrid.SizeChanged -= Host_SizeChanged_Initial;
                _hostGrid.Dispatcher.Invoke(() => _hostGrid.Children.Clear());
                _hostGrid = null;
            }

            if (_hostRdp != null)
            {
                _hostRdp.Rdp.OnDisconnected -= Rdp_OnDisconnected;
                if (_hostRdp.Rdp.Connected && IsConnected)
                {
                    _hostRdp.Rdp.Disconnect();
                }
                _hostRdp.Invoke((MethodInvoker) delegate { _hostRdp.Dispose(); });
                _hostRdp = null;
            }

            UI = null;
        }