c# - 使用 Windows Phone 在运行时加载 xaml viewmodel

标签 c# xaml windows-phone-7 windows-phone-8 expression-blend

我目前正在学习 Microsoft 虚拟学院的 Windows Phone 教程,挑战之一是使用在项目中创建并在运行时加载的设计 xaml View 模型。

在研究了几个小时之后,我认为是时候求助于 stackoverflow 了,因为我一无所获。我已经阅读了很多文章,但没有一篇给我正确的答案,所以我有几个问题:

  1. 如何解决我的错误?
  2. 如何以编程方式在运行时加载 xaml 模型 View ?
  3. 如何使用 xaml 在运行时加载 xaml 模型 View ?
  4. 在运行时在哪里调用 xaml 的加载

示例数据文件,即 SoundViewModelSampleData.xaml,如下所示:

<vm:SoundViewModel
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:Soundboard.ViewModels"
    xmlns:mo="clr-namespace:Soundboard.Models">

    <vm:SoundViewModel.Animals>
        <vm:SoundGroupViewModel Title="Animals Sample">
            <vm:SoundGroupViewModel.Items>
                <mo:SoundDataModel Title="Animals 1" FilePath="Animals.wav" />
            </vm:SoundGroupViewModel.Items>
        </vm:SoundGroupViewModel>
    </vm:SoundViewModel.Animals>
    <vm:SoundViewModel.Cartoons>
        <vm:SoundGroupViewModel Title="Cartoons Sample">
            <vm:SoundGroupViewModel.Items>
                <mo:SoundDataModel Title="Cartoons 1" FilePath="Cartoons.wav" />
                <mo:SoundDataModel Title="Cartoons 2" FilePath="Cartoons.wav" />
            </vm:SoundGroupViewModel.Items>
        </vm:SoundGroupViewModel>
    </vm:SoundViewModel.Cartoons>
</vm:SoundViewModel>

我发现以编程方式加载此代码的最简单代码是:

string path = @".\SampleData\SoundViewModelSampleData.xaml";
using (System.IO.StreamReader reader = new System.IO.StreamReader(path))
{
    SoundViewModel vm = XamlReader.Load(reader.ReadToEnd()) as SoundViewModel;
}

虽然我现在可能是从错误的位置调用它,但我收到以下错误:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll

{System.Windows.Markup.XamlParseException: Unknown parser error: Scanner 2147500037. [Line: 5 Position: 14] at MS.Internal.XcpImports.CreateFromXaml(String xamlString, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers, Boolean expandTemplatesDuringParse, Boolean trimDeclaredEncoding) at System.Windows.Markup.XamlReader.Load(String xaml) at Soundboard.ViewModels.SoundViewModel.LoadData()}

Unknown parser error: Scanner 2147500037. [Line: 5 Position: 14]

假设我可以解决这个错误,这将解决我的问题 1 和 2(修复错误并以编程方式加载数据)

您能找出导致此问题的原因吗?

如上所述,我可能在错误的位置加载它,即在加载应用程序时从我的 ViewModel 中创建它。

namespace Soundboard.ViewModels
{
    public class SoundViewModel
    {
        public SoundGroupViewModel Animals { get; set; }
        public SoundGroupViewModel Cartoons { get; set; }

        public bool IsDataLoaded { get; set; }

        public void LoadData()
        {
            string path = @".\SampleData\SoundViewModelSampleData.xaml";
            using (System.IO.StreamReader reader = new System.IO.StreamReader(path))
            {
                SoundViewModel vm = System.Windows.Markup.XamlReader.Load(reader.ReadToEnd()) as SoundViewModel;
        }
        IsDataLoaded = true;
    }
}

在我的 app.xaml.cs 中我有以下内容:

public static SoundViewModel SoundViewModel
{
    get
    {
        if (_soundViewModel == null)
        {
            _soundViewModel = new SoundViewModel();
            _soundViewModel.LoadData();
        }

        return _soundViewModel;
    }
}

现在如何在运行时仅使用 xaml 并在设计时使用 d:datacontext 来实现同样的效果。

我读过几篇文章,但它们都是针对 wpf 的,但大多数都与加载用户控件等有关,但与 View 模型无关

如有任何帮助,我们将不胜感激。

谢谢。

最佳答案

我正忙于解决与 XamlReader 类似的问题。我发现您应该在 xaml 文件的根元素中定义程序集命名空间,即使它包含在同一个程序集中也是如此。在下面的示例代码中,即使 xaml 包含在 SoundBoard.dll 中,我也在 xaml 文件中声明了它的命名空间。

xmlns:vm = "clr-namespace:SoundBoard.ViewModels;assembly=SoundBoard">

关于c# - 使用 Windows Phone 在运行时加载 xaml viewmodel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18546545/

相关文章:

c# - 将 List<List<string>> 转换为 string[][] 的快速方法是什么?

c# - 在浏览器中与称重秤进行串行/并行通信

c# - 根据通过 Internet 接收的 Windows Phone 数据显示图像

xaml - Visual Studio 2013 更新 3,设计器不会加载

.net - 前台属性行为困惑

wpf - 电子节目指南 (EPG) XAML

c# - 在 ASP.NET Core 依赖注入(inject)中使用非泛型实现注册泛型接口(interface)

c# Linq 关键字?

c# - 从另一个页面读取 IsolatedStorage 中的设置

c# - 将 TPL 任务与 HttpWebRequest 结合使用