我目前正在关注来自Microsoft虚拟学院的windows phone教程,其中一个挑战是使用在项目中创建并在运行时加载的设计xaml viewmodel 。在运行时加载xaml viewmodel与windows phone
经过几个小时的研究,我认为是时候诉诸于计算器,因为我没有得到任何地方。我已经阅读了很多文章,没有人给我一个正确的答案,所以我有几个问题:
- 如何解决我的错误?
- 如何在运行时以编程方式加载xaml模型视图?
- 如何使用xaml在运行时加载xaml模型视图?
- 凡调用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,但大多数都涉及到装载用户控件,等等。但没有一个视图模型
任何帮助将不胜感激。
谢谢。
嗨巴赫蒂,非常感谢!问题排序!我最终将XamlReader放入了app.xaml.cs中,但它感觉不太对劲!你知道更好的方法吗? – Thierry