【问题标题】:Windows 8 C# to XAML databinding issues, GUI not updatingWindows 8 C# 到 XAML 数据绑定问题,GUI 未更新
【发布时间】:2013-06-01 09:34:18
【问题描述】:

我正在尝试使用 MVVM 设计模式在 Windows 8 中制作一个简单的测验程序。我尝试使用 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];
    }
}

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 个是相同的 第一个错误指的是 XAML 数据上下文中的:

错误 1 ​​(x2)
找不到文件“C:\Users\Me\AppData\Local\Microsoft\VisualStudio\11.0\Designer\ShadowCache\cv0te54x.fpv\5ncl4yxi.hui\Quiz.xml”。

错误 2 无法创建“Quiz.QuizViewModel”类型的实例

这似乎影响了我的“选择”没有填充,我可以通过删除数据上下文来解决这个问题,但是我无法绑定“myCommand”

第三个问题是如何从命令输入中获取 Sender 信息,以便评估它是对还是错?

【问题讨论】:

    标签: c# winrt-xaml


    【解决方案1】:

    查看错误 #1,您的代码找不到 Quiz.xml 文件,它正在错误描述中的位置查找它。它似乎正在寻找错误的位置,因此您可能必须为其指定更具体的路径。如果资源中有 xml 文件,This 问题可能会有所帮助。因为这是在 Quiz.QuizViewModel 构造函数中完成的,所以实例的创建失败并产生错误 #2。

    至于第三部分,你可以给命令传递任意参数。在这种情况下,它可能是选择或其位置。 Something like this

    【讨论】:

    • 它最初会填充,只是不会更新到下一个选择。我试图使路径更具体,但是当我这样做时它引发了许多错误。我现在将其设置为内容,我尝试了嵌入式资源,但没有奏效。对于第三个问题,我会尝试你的想法。谢谢
    • 关于如何使“Quiz.xml”的路径更具体的任何建议。它目前位于源目录中,所以我无法使其更具体。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多