시작시 MVVM 방식으로 viewmodel 함수를 호출하려고합니다. 나는 그것이 맞다고 생각했지만, 코드는 절대로 함수 호출을 치지 않습니다.viewmodel에서 함수 호출 시작
이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;
}
}
}
명중되지 않는 문제의 기능 LoadData()
입니다 :
<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>
여기 내 뷰 모델 (필요한 부분)입니다 :
여기 내 XAML입니다. 내 모델 클래스에서 GetData()
을 호출합니다. 이유는 표시 할 수 없습니다. 그 밖의 무엇을 시도해야하는지 잘 모릅니다. 내가하는 일과 똑같은 일을하는 것에 대한 다른 질문을 보았다. 내 다른 기능 ShowTimeWindow
은 똑같은 방식으로 설정되어 공격을받습니다.
데이터 바인딩이 어떻게 작동하는지 실제로 알지 못하는 것 같습니다. WPF 데이터 바인딩 엔진에 익숙해지기를 제안합니다. 당신은 작동하지 않는 방법에 묶으려고합니다. – dymanoid
더 많은 조사를 해보고 싶습니다.이 사이트의 다른 질문을 한 번 보았습니다. http://stackoverflow.com/a/17933290/2480598 메서드에 명령을 바인딩 할 수 있다고 생각했습니다. – pfinferno
Binding LoadData 대신 Binding LoadDataCmd를 사용하면 안됩니까? – Steve