Prism 6中的无参数视图构造函数

时间:2019-03-01 14:19:27

标签: c# wpf mvvm prism

这就是我想要做的(可能会有更好的方法):     我的应用程序旨在跟踪游戏中的玩家。当玩家加入游戏时,将发送事件(使用事件汇总器)以创建用户控件,并将其添加到连接到ItemControl的列表中。当他们离开游戏时,将发送事件以从列表中删除用户控件。

实际的ItemContol看起来像这样-玩家绑定到列表,以便它可以显示各个玩家用户控件:

<UserControl>
        <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <ItemsControl ItemsSource="{Binding Players}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <local:PlayerListView />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
</UserControl>

只需使用以下命令即可将其插入到主窗口视图的网格行/列中:

<views:PlayerListView />

我遇到的问题是最后一行<views:PlayerListView />,因为它说“没有可访问的PlayerListView构造函数”。这是构造函数后面的代码,我相信我收到此错误的原因是因为构造函数不是无参数的-这是背后的代码。

public partial class PlayerListView : UserControl
{
    public PlayerListView(IEventAggregator eventAggregator)
    {
        InitializeComponent();
        DataContext = new PlayerListViewModel(eventAggregator);
    }
}

我需要PlayerListViewModel在其构造函数中具有IEventAggregator,但不确定执行此操作的适当方法。我已经查看了这篇Stack Overflow帖子:Prism MVVM - How to pass an IEventAggregator to my ViewModel,但不确定这是否是实现我正在尝试做的最好的方法,并且对如何合并它有些困惑。

添加PlayerListViewModel

namespace PlayerTools.ViewModels
{
    class PlayerListViewModel
    {
        #region Private Fields
        IEventAggregator _eventAggregator;
        #endregion

        #region Public Commands
        ICommand AddPlayerToListCommand { get; set; }
        ICommand RemovePlayerFromListCommand { get; set; }
        #endregion

        public List<PlayerStackPanelViewModel> Players = new List<PlayerStackPanelViewModel>();

        #region Constructor
        public PlayerListViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;

            Players.Add(new PlayerStackPanelViewModel()
            {
                PlayerName = "Steven",
                PlayerClass = "Tennis",
                CurrentLevel = 10,
                NumberOfDeaths = 0
            });
            Players.Add( new PlayerStackPanelViewModel()
            {
                PlayerName = "Steven",
                PlayerClass = "Soccer",
                CurrentLevel = 17,
                NumberOfDeaths = 1
            });

            //Commands
            AddPlayerToListCommand = new DelegateCommand<PlayerStackPanelViewModel>(AddPlayerToList);
            RemovePlayerFromListCommand = new DelegateCommand<string>(RemovePlayerFromList);
        }
        #endregion

        public void AddPlayerToList(PlayerStackPanelViewModel player)
        {
            Players.Add(player);
        }

        public void RemovePlayerFromList(string playerName)
        {
            Players.Remove(Players.Single(s => s.PlayerName == playerName));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用ViewModelLocator将视图的DataContext连接到视图模型的实例:

PlayerListView.xaml.cs:

public partial class PlayerListView : UserControl
{
    public PlayerListView()
    {
        InitializeComponent();
    }
}

PlayerListView.xaml:

<UserControl ... prism:ViewModelLocator.AutoWireViewModel="True" />

您应在视图模型中而不是在视图中实现对事件聚合器的使用。