这个问题可能很容易解决,但我在将命令从 viewModel 绑定到 xaml 视图文件时遇到了一些严重问题。您可能已经猜到,我们的应用程序基于 MVVM 设计模式,目前有 2 个窗口(2 个 XAML 文件)和 2 个视图模型(cs 文件)。
MainWindow.xaml 绑定到 MainViewModel,其中所有命令都存储为 ICommand 对象。一切正常,所有按钮都能正常工作并到达它们在 viewModel 中绑定的命令。然后,一个按钮会创建一个名为 MultipleQuizWindow.xaml 的新窗口,该窗口绑定到一个名为 MultipleQuizViewModel.cs 的新 ViewModel,但这就是有趣的开始。按钮能够找到用作标签的内容,并且看起来它也可以找到命令(运行时输出菜单中没有错误)但是当我单击按钮时没有任何反应?几乎已经尝试了所有方法,并且开始不知道为什么它可以在不执行 ICommand 的情况下找到它。这是创建新窗口的按钮的代码:
(MainViewModel.cs)
public void StartQuiz() {
MultipleQuizWindow newWindow = new MultipleQuizWindow(this);
newWindow.Show();
}
然后调用新的 Window 并且特定的 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>
最后是窗口绑定的viewModel(注意它可以找到按钮绑定的内容,但不能找到ICommands)
多测验视图模型
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 顺便说一句。
在此先感谢任何可能提供可以引导我朝着正确方向前进的小费的人:)