加载每个视图模型具有不同视图的 xml 文件?

发布于 2024-11-24 22:28:52 字数 1040 浏览 0 评论 0原文

我想用 MVVMLight 做两件事,但我不知道从哪里开始。

  • 将 XML 文件序列化为视图模型或用作数据源

您会推荐不同的格式吗(我现在可以更改),我找不到任何与最初设置数据源相关的 mvvmlight 教程。

  • 根据 xml 文件中记录的特定类型使用不同的布局

我正在考虑使用 switch 语句以编程方式隐藏/显示和重新组织所有内容,但它似乎不正确。

我目前使用 linq 读取模型,但没有使用 MVVMlight(这仍然适用于 MVVM 吗?)

public IEnumerable<Quiz.Question> LoadQuestions()
{

        XDocument questionData = XDocument.Load("questions.xml");

        var data = from query in questionData.Descendants("question")
                   select new Quiz.Question
                   {
                       QuestionTitle = (string)query.Attribute("title"),
                       QuestionText = query.Element("text") != null ? query.Element("text").Value.Trim() : string.Empty,
                       QuestionImage = query.Element("image") != null ? query.Element("image").Attribute("src").Value : string.Empty
                   };

        var shuffledData = Enumerable.Shuffle<Quiz.Question>(data);

        return shuffledData;
}

There are two things i want to do with MVVMLight but I don't know where to start.

  • Serialise an XML file as a viewmodel or use as a datasource

Would you recommend a different format (i can change at this point), I can't find any tutorials for mvvmlight relating to setting up a datasource initially.

  • Use a different layout depending on a particular type of record in the xml file

i'm considering using switch statements to hide/show and reorganise everything programmatically but it doesn't seem right.

I currently use linq to read into a model, but without using MVVMlight (will this still work with MVVM?)

public IEnumerable<Quiz.Question> LoadQuestions()
{

        XDocument questionData = XDocument.Load("questions.xml");

        var data = from query in questionData.Descendants("question")
                   select new Quiz.Question
                   {
                       QuestionTitle = (string)query.Attribute("title"),
                       QuestionText = query.Element("text") != null ? query.Element("text").Value.Trim() : string.Empty,
                       QuestionImage = query.Element("image") != null ? query.Element("image").Attribute("src").Value : string.Empty
                   };

        var shuffledData = Enumerable.Shuffle<Quiz.Question>(data);

        return shuffledData;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆依然 2024-12-01 22:28:52

您存储数据的格式和您使用的文件数量将取决于数据及其在应用程序中的使用方式。

您需要在以下方面找到平衡:序列化和反序列化性能/速度、数据处理的便捷性、文件大小、文件数量(和文件管理)以及使用外部库的能力。

当涉及文件格式和序列化时,二进制通常比 JSON 更快,而 JSON 通常比 XML 更快。

用于序列化的库也会产生很大的影响。 (提示。如果使用 JSON 格式数据,请使用 JSON.Net 而不是 DatContractjsonSerializer。)

同样,对于如何显示不同数据没有通用规则。如何执行此操作将取决于数据、差异以及应用程序的架构。

如果数据差异很大,您可能需要为不同的数据使用不同的页面。
您可以对每种类型的数据使用不同的用户控件,并在页面中加载适当的用户控件。
您可以使用不同的模板。
或者,您可以将 UIElement 的可见性链接(绑定)到数据类型或数据中是否存在某些字段。

The format you store you data in and the number of files you use will depend on the data and how it's used within the application.

You need to find the balance in: serialization and deserialization performance/speed, ease of working with the data, size of files, number of files (and management of files), and ability to use external libraries.

When it comes to file foramts and serialization, binary is normally faser than JSON, which is normally faster than XML.

The library you use for the serialization can also affect things greatly. (Hint. if using JSON format data, use JSON.Net rather than DatContractjsonSerializer.)

Again there are no general rules for how to display different data. How you do so will depend on the data, what differs and the archtecture of the app.

If the data is very different you may want to use different pages for the different data.
You could use different usercontrols for each type of data and load the appropriate one within the page.
You could use different templates.
Or you could link (bind) the visibility of a UIElement to the data type or whether certain fields are present in the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文