使用事件聚集器在UserControls之间导航 [英] Navigate between UserControls with Event Aggegator

查看:91
本文介绍了使用事件聚集器在UserControls之间导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MainWindow,通过单击菜单可以在UserControl之间导航,

I have a MainWindow where I navigate between UserControls by clicking on a menu and it works fine.

我正在使用以下模式:

https://rachel53461.wordpress.com/2011/05/08/simplemvvmexample/

在这些用户控件之一中,有一个按钮.通过单击此按钮,我想导航到另一个用户控件.

In one of those usercontrol there is a button. By clicking on this button I want to navigate to another usercontrol.

我该怎么做?

<UserControl.Resources>
    <DataTemplate DataType="{x:Type cvm:FirstViewModel}">
        <cv:FirstView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type cvm:SecondViewModel}">
        <cv:SecondView/>
    </DataTemplate>
    <cvm:MainViewModel x:Key="main"/>
</UserControl.Resources>

<Grid DataContext="{Binding Source={StaticResource main}}">
    <Border Grid.Row="0">
        <Menu Height="58">
             <ItemsControl ItemsSource="{Binding PageViewModels}" Width="289" Height="58">
                 <ItemsControl.ItemTemplate>
                     <DataTemplate>
                         <TextBlock>
                             <Hyperlink Command="{Binding ChangePageCommand, Mode=OneWay, Source={StaticResource main}}" CommandParameter="{Binding}" TextDecorations="{x:Null}">
                                 <InlineUIContainer>
                                     <TextBlock Text="{Binding Name}"/>
                                 </InlineUIContainer>
                             </Hyperlink>    
                         </TextBlock>
                      </DataTemplate>
                 </ItemsControl.ItemTemplate>
             </ItemsControl>
        </Menu>
    </Border>       
    <Border Grid.Row="1" >
        <ContentControl Content="{Binding CurrentUserControl}"/>
    </Border>
</Grid>

MainViewModel

public class MainViewModel : ViewModelBase
{
        public MainViewModel()
        {
            PageViewModels.Add(new FirstViewModel());
            PageViewModels.Add(new SecondViewModel());

            // Set starting page
            CurrentUserControl = PageViewModels[0];
        }

        #region Fields

        private List<IUserContentViewModel> _pageViewModels;
        public List<IUserContentViewModel> PageViewModels
        {
            get
            {
                if (_pageViewModels == null)
                    _pageViewModels = new List<IUserContentViewModel>();

                return _pageViewModels;
            }
        }

        private IUserContentViewModel _currentUserControl;
        public IUserContentViewModel CurrentUserControl
        {
            get { return _currentUserControl; }
            set
            {
                if (value != _currentUserControl)
                {
                    _currentUserControl = value;
                    OnPropertyChanged("CurrentUserControl");
                }
            }
        }

        #region Methods

        private void ChangeViewModel(IUserContentViewModel viewModel)
        {
            if (!PageViewModels.Contains(viewModel))
                PageViewModels.Add(viewModel);

            CurrentUserControl = PageViewModels
                .FirstOrDefault(vm => vm == viewModel);


        }

        #endregion

        private ICommand _changePageCommand;
        #endregion
        public ICommand ChangePageCommand
        {
            get
            {
                if (_changePageCommand == null)
                {
                    _changePageCommand = new RelayCommand(
                        p => ChangeViewModel((IUserContentViewModel)p),
                        p => p is IUserContentViewModel);
                }

                return _changePageCommand;
            }
        }
    }

SecondView

<Grid Background="Blue">
    <Button /> <!-- Going to ThirdView?????????-->
</Grid>

推荐答案

我终于有了解决方案.

我将Event Aggregator与Prism 6一起使用.

I use Event Aggregator with Prism 6.

首先,我创建一个Singleton.

First I create a Singleton.

internal sealed class ApplicationService
    {
        private ApplicationService() { }

        private static readonly ApplicationService _instance = new ApplicationService();

        internal static ApplicationService Instance { get { return _instance; } }

        private IEventAggregator _eventAggregator;
        internal IEventAggregator EventAggregator
        {
            get
            {
                if (_eventAggregator == null)
                    _eventAggregator = new EventAggregator();

                return _eventAggregator;
            }
        }
    }

然后public class GoToThird : PubSubEvent<TEvent> { }

MainViewModel中,我订阅了该事件并添加了我的ThirdViewModel().

In MainViewModel I subscribe to the event and add my ThirdViewModel().

public class MainViewModel : ViewModelBase
{
        protected readonly IEventAggregator _eventAggregator;

        public MainViewModel(IEventAggregator eventAggregator)
        {
            PageViewModels.Add(new FirstViewModel());
            PageViewModels.Add(new SecondViewModel(ApplicationService.Instance.EventAggregator)));

            PageViewModels.Add(new ThirdViewModel());

            // Set starting page
            CurrentUserControl = PageViewModels[0];

            this._eventAggregator = eventAggregator;
        }

        private void GoToThird()
        {
            CurrentUserControl = PageViewModels[2];
        } 
}

最后,我将该事件发布在SecondViewModel()

At the end I publish the event in SecondViewModel()

public class SecondViewModel
    {

        protected readonly IEventAggregator _eventAggregator;

        public SecondViewModel(IEventAggregator eventAggregator)
        {
            this._eventAggregator = eventAggregator;

        }



        private void Go()
        {
            _eventAggregator.GetEvent<GoToThird>().Publish();
        }


        private ICommand goToThirdCommand;

        public ICommand GoToThirdCommand
        {
            get
            {
                return goToThirdCommand ?? (goToThirdCommand = new RelayCommand(p => this.Go(), p => this.CanGo()));
            }
        }

        private bool CanGo()
        {
            return true;
        }
}

非常感谢Rachel和Kirenenko

Big Thanks to Rachel and Kirenenko

这篇关于使用事件聚集器在UserControls之间导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆