public ComPortControlViewModel(ISharedAppAbilities appAbilities, ILogger logger, IThreadNotifier notifier, IWindowSystem windowSystem, string channelName)
        {
            _appAbilities            = appAbilities;
            _logger                  = logger;
            _notifier                = notifier;
            _windowSystem            = windowSystem;
            _openPortCommand         = new RelayCommand(OpenPort, () => !_isPortOpened);
            _closePortCommand        = new RelayCommand(ClosePort, () => _isPortOpened);
            GetPortsAvailableCommand = new RelayCommand(GetPortsAvailable);
            RecordVm                 = new RecordViewModel(_notifier, _windowSystem);


            //TODO: unsubscribe
            _channel = _appAbilities.CreateChannel(channelName);
            _channel.Channel.CommandHeared += SerialChannelOnCommandHeared;
            _channel.Channel.CommandHearedWithReplyPossibility   += SerialChannelOnCommandHearedWithReplyPossibility;
            _channel.TimeoutMonitor.SomeCommandWasHeared         += CommandHearedTimeoutMonitorOnSomeCommandWasHeared;
            _channel.TimeoutMonitor.NoAnyCommandWasHearedTooLong += CommandHearedTimeoutMonitorOnNoAnyCommandWasHearedTooLong;

            _channelWithIoProgress = _channel.Channel as ISerialChannelWithIoProgress;
            if (_channelWithIoProgress != null)
            {
                _channelWithIoProgress.ProgressChanged += ChannelWithIoProgressOnProgressChanged;
            }


            GetPortsAvailable();

            _logger.Log("Канал обмена добавлен");
        }
Exemple #2
0
        /// <summary>
        /// Shows main window and calls back function in window thread with window view model as argument
        /// </summary>
        /// <param name="windowTitle">Window title</param>
        /// <param name="appAbilities">Window need for some app abilities, use AppFactory.Abilities property to take them</param>
        /// <param name="callback">Callback is needed to complete window creation with tabs</param>
        public void ShowMainWindowInOwnThread(string windowTitle, ISharedAppAbilities appAbilities, Action <ISharedMainViewModel> callback)
        {
            var appThreadNotifier = new WpfUiNotifierAsync(System.Windows.Threading.Dispatcher.CurrentDispatcher);

            var mainWindowThread = new Thread(() =>
            {
                try
                {
                    //Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " > Main window thread started");
                    var mainWindowNotifier = new WpfUiNotifierAsync(System.Windows.Threading.Dispatcher.CurrentDispatcher);
                    var windowSystem       = new WpfWindowSystem();
                    //Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " > uiNotifier and WpfWindowSystem created, line before window and WM were created");
                    var mainViewModel = new SharedMainViewModel(mainWindowNotifier, windowSystem, windowTitle, appAbilities);
                    //Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " > windowVM was created");
                    var mainWindow = new SharedMainView(appThreadNotifier, () =>
                    {
                        foreach (var closingAction in _closeChildWindowsActions)
                        {
                            closingAction.Invoke();
                        }

                        _closeChildWindowsActions.Clear();
                    })
                    {
                        DataContext = mainViewModel
                    };
                    //Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " > mainWindow was created, let's call it's .Show() method");

                    mainWindow.Show();
                    //mainWindow.ShowDialog();
                    //Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " > mainWindow.Show() was called");

                    callback(mainViewModel);
                    //Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " > Callback was fired");

                    _mainWindowCreationCompleteWaiter.Set();
                    System.Windows.Threading.Dispatcher.Run();
                }
                catch (Exception e)
                {
                    //Console.WriteLine(e);
                }
            });

            mainWindowThread.SetApartmentState(ApartmentState.STA);
            mainWindowThread.Priority     = ThreadPriority.Normal;
            mainWindowThread.IsBackground = false;
            mainWindowThread.Start();
            _mainWindowCreationCompleteWaiter.WaitOne();
        }