0

Windows 8 で MVVM デザイン パターンを使用して簡単なクイズ プログラムを作成しようとしています。私は PRISM と MVVMlite を使用しようとしましたが、私は初心者であり、正しく使用する方法を理解するのに十分なデータとコントロール バインディングの知識がありません。私はそれの大部分が機能していると思いますが、いくつかの大きな問題があります。1. GUI が正しく更新されません。2. 複数のエラーが表示されます。3. コードの一部を修正すると、別の部分が壊れます。4. XAML のコマンドから「送信者」情報を取得する方法がわかりません。

これまでの私のコードは次のとおりです。

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];
    }
}

i コマンド:

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 つは XAML データ コンテキストで参照されている最初のエラーと同じです。

エラー 1 (x2)
ファイル 'C:\Users\Me\AppData\Local\Microsoft\VisualStudio\11.0\Designer\ShadowCache\cv0te54x.fpv\5ncl4yxi.hui\Quiz.xml' が見つかりませんでした。

エラー 2 タイプ 'Quiz.QuizViewModel' のインスタンスを作成できません

これは、「Choices」が読み込まれないことに影響しているようです。データ コンテキストを削除することでこれを修正できますが、「myCommand」をバインドできません。

3 番目の問題は、コマンド入力から送信者情報を取得して、それが正しいか間違っているかを評価する方法です。

4

1 に答える 1

0

エラー #1 を見てください。コードはQuiz.xmlファイルを見つけることができず、エラーの説明にある場所でファイルを探しています。間違った場所を見ているようですので、より具体的なパスを指定する必要があるかもしれません。リソースにxmlファイルがある場合、この質問が役立つ場合があります。これはQuiz.QuizViewModelコンストラクターで行われるため、インスタンスの作成は失敗し、エラー #2 が生成されます。

3 番目の部分については、コマンドに任意のパラメーターを渡すことができます。この場合、それは選択またはその位置である可能性があります。このようなもの

于 2013-06-01T09:46:35.773 に答える