如何使用Catel显示视图

时间:2014-10-04 13:20:50

标签: c# wpf mvvm catel

我正在使用Catel开发一个简单的应用程序。我之前使用过ReactiveUI,并且在使用Catel时遇到了一些麻烦。我有一个基本的MainWindow。在那里,我有一个带有一些按钮的工具栏。当选择一个按钮时,我想在应用程序的底部窗格中显示一个用户控件(基于他们选择的内容)。到目前为止,我有一个基本视图,其中包含listview,然后是一个视图模型。在选择按钮时,我需要帮助确定如何显示该视图。谢谢您的帮助。这是我到目前为止所拥有的。正如你所看到的,当'ExecutePlayersButtonCommand'在mainviewmodel中运行,我想让它显示玩家视图。我不知道怎么做到这一点。我可以让它出现在弹出窗口中,但这不是我想要的。在reactui中,我可以使用Router.Navigate功能。不知道如何在这里做。

    <catel:DataWindow xmlns:Controls="clr-namespace:FootballSim.Controls;assembly=FootballSim.Controls"  
                  xmlns:RedfoxSoftwareCustomControls="clr-namespace:RedfoxSoftwareCustomControls;assembly=RedfoxSoftwareCustomControls" 
                  x:Class="FootballSim.Views.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:catel="http://catel.codeplex.com"
                  xmlns:views="clr-namespace:FootballSim.Views"
                  Style="{StaticResource {x:Type Window}}"
                  ShowInTaskbar="True" ResizeMode="CanResize" SizeToContent="Manual" 
                  WindowStartupLocation="Manual" WindowState="Maximized" Icon="/FootballSim;component/redfox_software_48x48.ico">

    <!-- Resources -->
    <catel:DataWindow.Resources>
    </catel:DataWindow.Resources>

    <!-- Content -->
    <catel:StackGrid x:Name="LayoutRoot">
        <catel:StackGrid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </catel:StackGrid.RowDefinitions>
        <DockPanel>
            <StackPanel DockPanel.Dock="Top">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="50" />
                        <RowDefinition Height="100*" />
                    </Grid.RowDefinitions>
                    <RedfoxSoftwareCustomControls:WpfCustomApplicationMenuBar x:Name="CustomMenuBar" Grid.Row="0" />
                    <StackPanel Grid.Row="1">
                        <Button HorizontalAlignment="Left" Command="{Binding PlayersButtonCommand}" Background="Transparent">
                            <StackPanel>
                                <Image Source="/FootballSim;component/Resources/People_white_48.png" Width="30"></Image>
                                <TextBlock Text="Players" Foreground="White" HorizontalAlignment="Center"/>
                            </StackPanel>
                        </Button>
                    </StackPanel>
                    <DockPanel LastChildFill="True" Grid.Row="2">
                        <ContentControl Content="{Binding contentObject}" />
                        <!--<views:PlayersView DataContext="{Binding PlayerProviders}" />-->
                    </DockPanel>
                </Grid>
            </StackPanel>
        </DockPanel>
    </catel:StackGrid>
</catel:DataWindow>


    using Catel.Windows;
using FootballSim.Scripts;
using FootballSim.Services;
using FootballSim.ViewModels;
using System;
using System.ComponentModel;
using Catel.MVVM.Views;
using Catel.Windows.Data;
using Catel.MVVM;


namespace FootballSim.Views
{
    public partial class MainWindow : DataWindow
    {
        private RedfoxMessage logger = new RedfoxMessage();
        private PopulateDatabase database = new PopulateDatabase();
        public MainWindow() : base(DataWindowMode.Custom)
        {
            try
            {
                InitializeComponent();

                logger.LogMessage("Starting application.");

                CustomMenuBar.AboutButtonClickEvent += CustomMenuBar_AboutButtonClickEvent;
            }
            catch (Exception e)
            {
                RedfoxMessage.LogMessage(e, NLog.LogLevel.Error);
            }
        }

        private void CustomMenuBar_AboutButtonClickEvent(object sender, System.EventArgs args)
        {
            (DataContext as IMainWindowViewModel).AboutButtonCommand.Execute(null);
        }
    }
}

    using Catel.Data;
using Catel.IoC;
using Catel.MVVM;
using Catel.MVVM.Services;
using FootballSim.Models;
using FootballSim.Scripts;
using FootballSim.Views;
using System.Collections.Generic;
using System.Windows;


namespace FootballSim.ViewModels
{
    public interface IMainWindowViewModel
    {
        Command PlayersButtonCommand { get; }
        Command AboutButtonCommand { get; }

        List<Player> PlayerProviders { get; }
        Player SelectedPlayerProvider { get; }
        object ContentObject { get; }
    }

    /// <summary>
    /// MainWindow view model.
    /// </summary>
    public class MainWindowViewModel : ViewModelBase, IMainWindowViewModel
    {
        //private readonly IUIVisualizerService _uiVisualizerService;
        private PopulateDatabase _populateDatabase;

        public static readonly PropertyData PlayerProvidersProperty = RegisterProperty("PlayerProviders", typeof(List<Player>));
        public static readonly PropertyData SelectedPlayerProviderProperty = RegisterProperty("SelectedPlayerProvider", typeof(Player));

        public Command PlayersButtonCommand { get; private set; }
        public Command AboutButtonCommand { get; private set; }
        public object ContentObject { get; set; }

        public MainWindowViewModel() : base()
        {
            //var dependencyResolver = this.GetDependencyResolver();
            //_uiVisualizerService = dependencyResolver.Resolve<IUIVisualizerService>();
            _populateDatabase = new PopulateDatabase();
            PlayerProviders = _populateDatabase.Players;

            var pv = new PlayersView();
            pv.DataContext = PlayerProviders;
            ContentObject = pv;

            PlayersButtonCommand = new Command(ExecutePlayersButtonCommand);
            AboutButtonCommand = new Command(ExecuteAboutButtonCommand);
        }

        private void ExecutePlayersButtonCommand()
        {
            PlayerProviders = _populateDatabase.Players;
            MessageBox.Show("Players");
        }

        private void ExecuteAboutButtonCommand()
        {
            var aboutView = new AboutView();
            aboutView.ShowDialog();
        }

        public List<Player> PlayerProviders
        {
            get
            {
                return GetValue<List<Player>>(PlayerProvidersProperty);
            }
            set
            {
                SetValue(PlayerProvidersProperty, value);
            }
        }

        public Player SelectedPlayerProvider
        {
            get
            {
                return GetValue<Player>(SelectedPlayerProviderProperty);
            }
            set
            {
                SetValue(SelectedPlayerProviderProperty, value);
            }
        }

        //protected override void Initialize()
        //{
        //    SelectedPlayerProvider = PlayerProviders[0];
        //}



        public override string Title { get { return "FootballSim"; } }
    }
}

    <catel:UserControl x:Class="FootballSim.Views.PlayersView"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:catel="http://catel.codeplex.com"
                   xmlns:views="clr-namespace:FootballSim.Views"
                   xmlns:viewmodels="clr-namespace:FootballSim.ViewModels"
                   xmlns:models="clr-namespace:FootballSim.Models;assembly=FootballSim.Core">

    <!-- Resources -->
    <UserControl.Resources>
    </UserControl.Resources>

    <!-- Content -->
    <catel:StackGrid>
        <catel:StackGrid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </catel:StackGrid.RowDefinitions>

        <Label Content="{Binding Title}" Foreground="White" Grid.Row="0" />
        <Label Content="Here goes your real content" Foreground="White" Grid.Row="1"/>

        <ListBox Grid.Row="2" ItemsSource="{Binding PlayersCollection}" SelectedItem="{Binding SelectedPlayer}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label Content="{Binding ColumnValue}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>


        <!--<views:PlayersView Grid.Column="1" DataContext="{Binding SelectedPlayer}" />-->

    </catel:StackGrid>
</catel:UserControl>

    namespace FootballSim.Views
{
    using Catel.Windows.Controls;
    using FootballSim.ViewModels;

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

    namespace FootballSim.ViewModels
{
    using Catel.MVVM;
    using FootballSim.Models;
    using System.Collections.Generic;

    /// <summary>
    /// UserControl view model.
    /// </summary>
    public class PlayersViewModel : ViewModelBase, IPlayersViewModel
    {
        public List<Player> Players { get; protected set; }

        public PlayersViewModel(List<Player> players) : base()
        {
            Players = players;
        }

        public override string Title { get { return "View model title"; } }

        // TODO: Register models with the vmpropmodel codesnippet
        // TODO: Register view model properties with the vmprop or vmpropviewmodeltomodel codesnippets
        // TODO: Register commands with the vmcommand or vmcommandwithcanexecute codesnippets

    }
}

1 个答案:

答案 0 :(得分:0)

在卡特尔有几种导航方式:

  • IUIVisualizerService =&gt;显示其他对话框
  • INavigationService =&gt;导航到页面/关闭应用程序/ etc

也许您最好阅读getting started guide of Catel