2016-09-21 146 views
0

請幫我解決一個問題。爲什麼帶簽名的構造函數不起作用。以及如何確定用於ServerTabView的DataContext?據我所知,現在的DataTable for ServerTabView繼承自父 - MainWindowView,因爲ServerTabView嵌套在它中。那麼如何獲得接收ServerTabView DataContext的權利?Catel爲嵌套視圖設置DataContext

MainWindowView

<catel:DataWindow x:Class="ServerUI.Views.MainWindowView" 
       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:ServerUI.Views" 
       Width="640" 
       Height="480" 
       ResizeMode="CanResize" 
       ShowInTaskbar="True" 
       SizeToContent="Manual" 
       WindowStartupLocation="Manual" 
       WindowState="Normal"> 

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

<catel:TabControl TabStripPlacement="Left"> 
    <TabItem Header="__Server__"> 
     <views:ServerTabView DataContext="{Binding}" /> 
    </TabItem> 
    <TabItem Header="__Clients_"> 
     <views:ClientsTabView DataContext="{Binding}" /> 
    </TabItem> 
</catel:TabControl> 

</catel:DataWindow> 

ServerTabView

<catel:UserControl x:Class="ServerUI.Views.ServerTabView" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:catel="http://catel.codeplex.com"> 

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

    <catel:StackGrid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition Width="Auto" /> 
    </catel:StackGrid.ColumnDefinitions> 

    <Label Content="IP Address" /> 
    <TextBox Text="{Binding ServerIpAddress}" /> 

    <Label Content="Port" /> 
    <TextBox Text="{Binding ServerPort}" /> 

    <Label Content="Directory" /> 
    <TextBox Text="{Binding Catalogue}" /> 

    <catel:EmptyRow /> 

    <Button Grid.ColumnSpan="2" 
      Command="{Binding StartServer}" 
      Content="Start" /> 

    <Button Grid.ColumnSpan="2" 
      Command="{Binding StopServer}" 
      Content="Stop" /> 
</catel:StackGrid> 

</catel:UserControl> 

ServerTabViewModel

public class ServerTabViewModel : ViewModelBase 
    { 
     private readonly IServerServices _serverServices; 

     public ServerTabViewModel(Server server, IServerServices serverServices) 
     { 
      Argument.IsNotNull(() => serverServices); 
      Argument.IsNotNull(() => server); 

      _serverServices = serverServices; 
      Server = server; 

      StartServer = new TaskCommand(OnStartServerExecuteAsync); 
      StopServer = new TaskCommand(OnStopServerExecuteAsync); 
     } 

     #region Properties 
     /// <summary> 
     /// Gets or sets the property value. 
     /// </summary> 
     [Model] 
     public Server Server 
     { 
      get { return GetValue<Server>(ServerProperty); } 
      set { SetValue(ServerProperty, value); } 
     } 
     // ModelToViewModel Properties 
     //... 
    } 

而且一些調試inormation:

11:57:00:464 => [DEBUG] [Catel.MVVM.Providers.LogicBase] [10] DataContext of TargetView 'ServerTabView' has changed to 'MainWindowViewModel' 
11:57:00:466 => [DEBUG] [Catel.MVVM.Providers.LogicBase] [10] Using IViewModelFactory 'Catel.MVVM.ViewModelFactory' to instantiate the view model 
11:57:00:466 => [DEBUG] [Catel.IoC.TypeFactory] [10] Creating instance of type 'ServerUI.ViewModels.ServerTabViewModel' using specific parameters. No constructor found in the cache, so searching for the right one 
11:57:00:467 => [DEBUG] [Catel.IoC.TypeFactory] [10] Checking if constructor 'public ctor(Server server, IServerServices serverServices)' can be used 
11:57:00:468 => [DEBUG] [Catel.IoC.TypeFactory] [10] Constructor is not valid because value 'ServerUI.ViewModels.MainWindowViewModel (ID = 1)' cannot be used for parameter 'ServerUI.ViewModels.MainWindowViewModel (ID = 1)' 
11:57:00:468 => [DEBUG] [Catel.IoC.TypeFactory] [10] The constructor is valid and can be used 
11:57:00:469 => [DEBUG] [Catel.IoC.TypeFactory] [10] No constructor could be used, cannot construct type 'ServerUI.ViewModels.ServerTabViewModel' with the specified parameters 
11:57:00:469 => [DEBUG] [Catel.IoC.TypeFactory] [10] Creating instance of type 'ServerUI.ViewModels.ServerTabViewModel' using specific parameters. No constructor found in the cache, so searching for the right one 
11:57:00:469 => [DEBUG] [Catel.IoC.TypeFactory] [10] Checking if constructor 'public ctor(Server server, IServerServices serverServices)' can be used 
11:57:00:470 => [DEBUG] [Catel.IoC.TypeFactory] [10] Constructor is not valid because parameter 'server' cannot be resolved from the dependency resolver 
11:57:00:470 => [DEBUG] [Catel.IoC.TypeFactory] [10] The constructor is valid and can be used 
11:57:00:470 => [DEBUG] [Catel.IoC.TypeFactory] [10] No constructor could be used, cannot construct type 'ServerUI.ViewModels.ServerTabViewModel' with the specified parameters 
11:57:00:471 => [DEBUG] [Catel.MVVM.ViewModelFactory] [10] Could not construct view model 'ServerUI.ViewModels.ServerTabViewModel' using injection of data context 'MainWindowViewModel' 
11:57:00:471 => [DEBUG] [Catel.MVVM.Providers.LogicBase] [10] Used IViewModelFactory to instantiate view model, the factory did NOT return a valid view model 
System.Windows.Data Error: 3 : Cannot find element that provides DataContext. BindingExpression:(no path); DataItem=null; target element is 'ClientsTabView' (Name=''); target property is 'DataContext' (type 'Object') 

結果如何 - ServerTabViewModel(Server server, IServerServices serverServices)不要觸發。

更新 _____________________________________________________________________

MainWindowViewModel

public class MainWindowViewModel : ViewModelBase 
{ 
    public MainWindowViewModel() 
    { 
    } 

    public override string Title => "WatcherServerUI"; 
} 

我應該如何實例Server這裏?我的意思是,這有什麼好方法?

回答

0

正如您在日誌記錄中看到的那樣,嵌套視圖的當前DataContext(在此例中爲ServerTabViewClientsTabView)爲MainWindowViewModel。在Catel

構建視圖模型可以有3種方式發生:

  1. 有了明確的模型注入(那麼你需要設置的DataContext)
  2. 自動從IoC容器
  3. 1的組合解決和2

由於您在視圖模型中指定了Server參數,並在視圖模型中添加了額外的注入服務,因此您可以選擇選項3.這意味着y您需要將DataContext設置爲有效的Server對象。您尚未公佈MainWindowViewModel的代碼,但讓我們假設該虛擬機上有一個名爲Server的公共屬性。

<TabItem Header="__Server__"> 
    <views:ServerTabView DataContext="{Binding Server}" /> 
</TabItem> 

正如你所看到的,而不是通過{Binding}(導致MainWindowViewModel),你應該使用{Binding Server}這會導致服務器實例,將被用於實例嵌套視圖模型。

+0

POST UPDATE。不,我的'MainWindowViewModel'中沒有這個屬性。我沒有什麼。我應該怎麼做? –