这个问题可能很容易解决,但是我遇到了一些严重的问题,将命令从viewModel绑定到xaml视图文件。您可能已经猜到我们的应用程序基于MVVM设计模式,目前有2个窗口(2个XAML文件)和2个viewModels(cs文件)。
MainWindow.xaml绑定到MainViewModel,其中所有命令都存储为ICommand对象。一切正常,所有按钮都可以工作,并在viewModel中达到它们绑定的命令。然后,一个按钮创建一个名为MultipleQuizWindow.xaml的新窗口,该窗口绑定到名为MultipleQuizViewModel.cs的新ViewModel,但这里是有趣的开始。按钮能够找到用作标签的内容,看起来它也可以找到命令(运行时输出菜单中没有错误)但是当我点击按钮时没有任何反应?已经尝试了几乎所有的东西,并开始用尽为什么它可以找到ICommand而不执行它。这是创建新窗口的按钮的代码:
(MainViewModel.cs)
public void StartQuiz() {
MultipleQuizWindow newWindow = new MultipleQuizWindow(this);
newWindow.Show();
}
然后调用新窗口,特定的viewModel将MainViewModel作为构造函数的参数。
MultipleQuizWindow.xaml.cs
public partial class MultipleQuizWindow : Window {
public MultipleQuizViewModel vmQuiz;
public MultipleQuizWindow(MainViewModel mvm) {
InitializeComponent();
vmQuiz = new MultipleQuizViewModel(mvm);
this.DataContext = vmQuiz;
}
}
然后是用于设计窗口的xaml代码:
MultipleQuizWindow.xaml
<Window x:Class="VocApp.View.MultipleQuizWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:VocApp.View"
xmlns:viewmodel="clr-namespace:VocApp.ViewModel"
Title="MultipleQuizWindow" Height="300" Width="300">
<Grid Name="Grid1">
<Grid.RowDefinitions>
<RowDefinition Height="56*" />
<RowDefinition Height="43*" />
<RowDefinition Height="43*" />
<RowDefinition Height="43*" />
<RowDefinition Height="43*" />
<RowDefinition Height="42*" />
</Grid.RowDefinitions>
<Button Content="{Binding Ans1}" Name="Answer1" HorizontalAlignment="Left" Margin="108,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75" Height="22" Command="{Binding Ans1Command}"/>
<Button Command="{Binding Ans2Command}" Content="{Binding Ans2}" Name="Answer2" HorizontalAlignment="Left" Margin="108,10,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Height="22"/>
<Button Command="{Binding Ans3Command}" Content="{Binding Ans3}" Name="Answer3" HorizontalAlignment="Left" Margin="108,10,0,0" Grid.Row="3" VerticalAlignment="Top" Width="75" Height="22"/>
<Button Command="{Binding Ans4Command}" Content="{Binding Ans4}" Name="Answer4" HorizontalAlignment="Left" Margin="108,13,0,0" Grid.Row="4" VerticalAlignment="Top" Width="75" Height="22"/>
<Button Command="{Binding StartQuizCommand}" Content="{Binding Ans5}" Name="Answer5" HorizontalAlignment="Left" Margin="108,10,0,0" Grid.Row="5" VerticalAlignment="Top" Width="75" Height="22"/>
<TextBlock Text="{Binding Text}" Name="QuestionBox" HorizontalAlignment="Left" Margin="32,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="-1.48,-0.688" Height="36" Width="220"><Run Language="da-dk" Text=""/></TextBlock>
</Grid>
最后是windows绑定的viewModel(注意它可以找到按钮绑定的内容而不是ICommands)
MultipleQuizViewModel
public class MultipleQuizViewModel : ViewModelBase {
internal MainViewModel mvm;
private string ans1;
public string Ans1 {
get {
return ans1;
}
set {
ans1 = value;
}
}
private string ans2;
public string Ans2 {
get {
return ans2;
}
set {
ans2 = value;
}
}
private string ans3;
public string Ans3 {
get {
return ans3;
}
set {
ans3 = value;
}
}
private string ans4;
public string Ans4 {
get {
return ans4;
}
set {
ans4 = value;
}
}
private string ans5;
public string Ans5 {
get {
return ans5;
}
set {
ans5 = value;
}
}
private string text;
public string Text {
get {
return text;
}
set {
text = value;
}
}
MultipleQuiz Quiz;
public ICommand Ans1Command { get; private set; }
public ICommand Ans2Command { get; private set; }
public ICommand Ans3Command { get; private set; }
public ICommand Ans4Command { get; private set; }
public ICommand Ans5Command { get; private set; }
public MultipleQuizViewModel(MainViewModel mvm) {
this.mvm = mvm;
this.Quiz = mvm.model.GenerateQuiz() as MultipleQuiz;
string[] words = Quiz.AllAnswers;
text = "Please translate the word " + Quiz.word.Wordstring;
ans1 = words[0];
ans2 = words[1];
ans3 = words[2];
ans4 = words[3];
ans5 = words[4];
Ans1Command = new RelayCommand(Answer1);
Ans2Command = new RelayCommand(Answer2);
Ans3Command = new RelayCommand(Answer3);
Ans4Command = new RelayCommand(Answer4);
Ans5Command = new RelayCommand(Answer5);
}
public void Answer1() {
text = "please work";
if (Quiz.ansIndex == 1) {
text = "correct!";
}
else {
text = "wrong!";
}
}
public void Answer2() {
if (Quiz.ansIndex == 2) {
text = "correct!";
}
else {
text = "wrong!";
}
}
public void Answer3() {
if (Quiz.ansIndex == 3) {
text = "correct!";
}
else {
text = "wrong!";
}
}
public void Answer4() {
if (Quiz.ansIndex == 4) {
text = "correct!";
}
else {
text = "wrong!";
}
}
public void Answer5() {
if (Quiz.ansIndex == 5) {
text = "correct!";
}
else {
text = "wrong!";
}
}
}
我知道那里有很多代码,但我真的希望有人可以告诉我出了什么问题。很明显,命令永远不会执行,Visual Studio也可以通过xaml中的绑定找到它们。
该项目使用GalaSoft btw。
提前感谢任何可能提出可以引导我朝正确方向前进的人:)
答案 0 :(得分:1)
为什么要在命令方法中设置私有字段“text”(public void Answer1)?您应该设置公共属性Text(使用大写字母T)并让它实现INotifyPropertyChanged(OnPropertyChanged(“Text”);)以告诉视图属性已更改。
这些命令很可能正在运行,但由于我提到的Text问题,你没有看到UI的变化。尝试在Answer1()方法中放置一个断点,看看它是否在单击按钮时被击中,如果问题出现在Text属性中。
除此之外,你的测验窗口中有一个拼写错误* Button Content =“{Binding Ans1}”......应该是“{Binding Ans1Command}”