請幫我解決一個問題。爲什麼帶簽名的構造函數不起作用。以及如何確定用於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
這裏?我的意思是,這有什麼好方法?
POST UPDATE。不,我的'MainWindowViewModel'中沒有這個屬性。我沒有什麼。我應該怎麼做? –