2014-10-04 55 views
0

我正在用Catel開發一個簡單的應用程序。我之前使用過ReactiveUI,並且在開始使用Catel時遇到了一些麻煩。我有一個基本的MainWindow。在那裏我有一個帶有一些按鈕的工具欄。當選擇一個按鈕時,我想在應用程序的底部窗格中顯示一個用戶控件(根據他們選擇的內容)。到目前爲止,我有一個基本視圖,其中有一個列表視圖,然後是一個視圖模型。在選擇按鈕時,我需要幫助弄清楚如何顯示該視圖。感謝您的幫助。這是我到目前爲止。正如你所看到的,當mainviewmodel中的'ExecutePlayersButtonCommand'運行時,我想讓它顯示玩家的視圖。我不知道如何得到這個。我可以把它彈出來,但這不是我想要的。在reactiveui中,我可以使用Router.Navigate函數來完成。不知道如何在這裏做。如何使用Catel顯示視圖

<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 

    } 
} 

回答

0

有在Catel導航的幾種方法:

  • IUIVisualizerService =>顯示其它對話框
  • INavigationService =>轉到頁/關閉應用程序的/ etc

也許是一個好主意讓你閱讀getting started guide of Catel

相關問題