Ich versuche eine Funktion im Viewmodel beim Start der MVVM Weg. Ich dachte, ich hätte es richtig, aber der Code trifft nie den Funktionsaufruf.Aufruf Startfunktion in Viewmodel
Hier ist meine XAML:
<Window x:Class="TestWin.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:l="clr-namespace:Timeserver"
xmlns:viewmodel="clr-namespace:Timeserver.ViewModels"
Title="MainWindow"
Width="893"
Height="Auto"
Background="LightGray"
ResizeMode="NoResize"
SizeToContent="Height">
<Window.DataContext>
<viewmodel:MainWindowViewModel />
</Window.DataContext>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadData}" />
</i:EventTrigger>
</i:Interaction.Triggers>
Hier ist meine Viewmodel (die notwendigen Teile):
namespace TestWin.ViewModels
{
class MainWindowViewModel
{
private StructsModel model; // my model class
private ICommand loadDataCmd;
private ICommand showTimeWindowCmd;
private ICommand toggleExecuteCommand { get; set; }
private bool canExecute = true;
public bool CanExecute
{
get
{
return this.canExecute;
}
set
{
if (this.canExecute == value)
{
return;
}
this.canExecute = value;
}
}
public ICommand ToggleExecuteCommand
{
get
{
return toggleExecuteCommand;
}
set
{
toggleExecuteCommand = value;
}
}
public ICommand ShowTimeWindowCmd
{
//code here
}
public ICommand LoadDataCmd
{
get
{
return loadDataCmd;
}
set
{
loadDataCmd = value;
}
}
public void LoadData(object parameter)
{
model.GetData();
}
public MainWindowViewModel()
{
this.model = new StructsModel();
LoadDataCmd = new RelayCommand(LoadData, param => this.canExecute);
ShowTimeWindowCmd = new RelayCommand(ShowTimeWindow, param => this.canExecute);
toggleExecuteCommand = new RelayCommand(ChangeCanExecute);
}
public void ShowTimeWindow(object parameter)
{
//code here
}
public void ChangeCanExecute(object obj)
{
canExecute = !canExecute;
}
}
}
Die Funktion in Frage, die nicht getroffen wird, ist LoadData()
. Es ruft GetData()
in meiner Modellklasse an, die ich aus Gründen nicht anzeigen kann. Ich bin mir nicht sicher, was ich sonst noch versuchen sollte. Ich habe andere Fragen zu SO gesehen, die dasselbe machen wie ich. Meine andere Funktion ShowTimeWindow
ist genau so eingerichtet und wird getroffen.
Es scheint, dass Sie nicht wirklich erkennen, wie die Datenbindung funktioniert. Ich empfehle Ihnen, sich mit der WPF-Datenbindungs-Engine vertraut zu machen. Sie versuchen, an eine Methode zu binden, die nicht funktioniert. – dymanoid
Ich werde mehr Nachforschungen anstellen, ich habe nur andere Fragen auf dieser Seite wie dieser gesehen: http://Stackoverflow.com/a/17933290/2480598 Ich dachte, ich könnte einen Befehl an eine Methode binden. – pfinferno
sollte es nicht Binding LoadDataCmd statt Bindung LoadData? – Steve