我試圖在Windows 8中使用MVVM設計模式做一個簡單的測驗程序。我試圖使用PRISM和MVVMlite,但是我是一個新手,根本沒有足夠的數據和控制綁定知識來理解如何正確使用它。我認爲我的大部分工作正在進行,但我有一些主要問題。 1.我的GUI不能正確更新。 2.我收到幾個錯誤。 3.修復我的代碼的一部分打破了另一部分。 4.無法弄清楚如何從XAML中的命令獲取「發件人」信息。Windows 8 C#到XAML數據綁定問題,GUI不更新
這裏是到目前爲止我的代碼:
XML數據:
<root>
<Object>
<Question>What do you do for work</Question>
<Answer>Wrestle giant tentical monsters</Answer>
<Choices>Battle robots</Choices>
<Choices>Glorious ruler of North Korea</Choices>
<Choices>Wrestle Giant Tentical Monsters</Choices>
<Choices>Defender of all that is good</Choices>
</Object>
<Object>
<Question>What do you drive</Question>
<Answer>Moped</Answer>
<Choices>Helicopter</Choices>
<Choices>Pegasus</Choices>
<Choices>Rocketship</Choices>
<Choices>Moped</Choices>
</Object>
</root>
型號:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Xml.Linq;
using System.Windows.Input;
namespace Quiz
{
class QuizModel : INotifyPropertyChanged
{
private string _question;
public string Question
{
get { return _question; }
set
{
_question = value;
OnPropertyChanged("Question");
}
}
private string _answer;
public string Answer
{
get { return _answer; }
set
{
_answer = value;
OnPropertyChanged("Answer");
}
}
private List<string> _choices;
public List<string> Choices
{
get { return _choices; }
set
{
_choices = value;
OnPropertyChanged("Choices");
}
}
public QuizModel(string quesiton, string answer, List<string> choices)
{
Question = quesiton;
Answer = answer;
Choices = choices;
}
public static List<QuizModel> Query(string datasource)
{
XElement quizdata = XElement.Load(datasource);
List<QuizModel> query = (from d in quizdata.Descendants("Object")
select new QuizModel(
(string)d.Element("Question"),
(string)d.Element("Answer"),
d.Elements("Choices").Select(a => a.Value).ToList()
)).ToList();
return query;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
視圖模型:
class QuizViewModel
{
public static List<QuizModel> QuizList { get; set; }
public static QuizModel Quiz { get; set; }
public static int Indexer { get; set; }
public ICommand myCommand { get; set; }
//Initiallizes view model
public QuizViewModel()
{
Indexer = 0;
QuizList = QuizModel.Query("Quiz.xml");
Quiz = QuizList[Indexer];
myCommand = new ActionCommand(Evaluate);
}
//Increments to next question
private void Evaluate()
{
Indexer++;
Quiz = QuizList[Indexer];
}
}
的ICommand:
public class ActionCommand : ICommand
{
private readonly Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged; //ERROR event Never Used
}
}
檢視:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<TextBlock FontSize="50" Text="{Binding Quiz.Question}">
<TextBlock.DataContext>
<local:QuizViewModel/> <!--Can't find Quiz.xml-->
</TextBlock.DataContext>
</TextBlock>
<ListView Grid.Row="1" FontSize="30" ItemsSource="{Binding Quiz.Choices}">
<ListView.DataContext>
<local:QuizViewModel/> <!--Can't find Quiz.xml-->
</ListView.DataContext>
<ListView.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Mode=OneWay}" Command="{Binding myCommand}">
<Button.DataContext>
<local:QuizViewModel/>
</Button.DataContext>
</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
我有3個電流誤差2,其是相同的 第一個錯誤是指在XAMLs的datacontext:
錯誤1(×2)
找不到文件'C:\ Users \ Me \ AppData \ Local \ Microsoft \ VisualStudio \ 11.0 \ Designer \ ShadowCache \ cv0te54x.fpv \ 5ncl4yxi.hui \ Quiz.xml'。
錯誤2 無法創建類型爲「Quiz.QuizViewModel」
這似乎影響了我的「選擇」不填充,我可以通過刪除數據方面解決這個問題,但我不能結合「的實例myCommand「
第三個問題是我如何從命令輸入發件人信息,以便我可以評估它是對還是錯?
那麼它最初填充,它只是不會更新到下一個選擇。我試圖讓這條路更加具體,但是當我這樣做的時候,它拋出了許多錯誤。我現在把它設置爲內容,我嘗試了嵌入式資源,但這並不起作用。我會嘗試你的想法來解決第三個問題。謝謝 – evilsushi
任何關於如何使我的路徑更具體的「Quiz.xml」的建議。它目前在源代碼目錄中,所以我沒有辦法讓它更具體。 – evilsushi