Ejemplo n.º 1
0
        /// <summary>
        ///     跳转到指定的 <paramref name="viewModelPage" /> 添加消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="viewModelPage"></param>
        /// <param name="parameter"></param>
        public void Navigate(INavigateable sender, ViewModelPage viewModelPage, object parameter = null)
        {
            if (viewModelPage == null)
            {
                throw new ArgumentNullException(nameof(viewModelPage));
            }

            if (sender == null)
            {
                throw new ArgumentNullException(nameof(sender));
            }

            var receiveMessage = sender as IReceiveMessage;


            var lastViewModel = _lastViewModelPage?.ViewModel?.GetViewModel();

            if (lastViewModel is INavigable lastNavigableViewModel)
            {
                lastNavigableViewModel.NavigatedFrom(sender, parameter);
            }

            if (receiveMessage != null && lastViewModel is ISendMessage lastSendMessage)
            {
                lastSendMessage.Send -= receiveMessage.ReceiveMessage;
            }


            // 开始跳转
            var viewModel = viewModelPage.ViewModel.GetViewModel();

            if (viewModel == null)
            {
                throw new ArgumentException("无法从ViewModel.GetViewModel拿到ViewModel值");
            }

            if (receiveMessage != null)
            {
                if (viewModel is ISendMessage sendMessage)
                {
                    sendMessage.Send -= receiveMessage.ReceiveMessage;
                    sendMessage.Send += receiveMessage.ReceiveMessage;
                }
            }

            if (viewModel is INavigable navigable)
            {
                navigable.NavigatedTo(sender, parameter);
            }

            Frame.Navigate(viewModelPage.Page, viewModel);

            _lastViewModelPage = viewModelPage;
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public override void ReceiveMessage(object sender, IMessage message)
        {
            ViewModelBase viewModel = this;
            var           composite = message as ICombinationComposite;

            composite?.Run(viewModel, message);

            var run = MVVM.Framework.ViewModel.Composite.Run(viewModel, message, Composite);

            if (run)
            {
                return;
            }

            // 所有在这个 ViewModel 的 ViewModel 判断是否需要
            // 解决 A B 两个通信
            foreach (var temp in ViewModelPage.Where(/*如果 ViewModel 没有使用,就不收消息*/ temp => temp.ViewModel.IsLoaded)
                     .Select(temp => temp.ViewModel.GetViewModel()))
            {
                ViewModel.Composite.Run(temp, message, Composite);
            }
        }