Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad xaml viewmodel at run-time with windows phone
    text
    copied!<p>I'm currently following a windows phone tutorial from Microsoft virtual academy and one of the challenges was to use the design xaml viewmodel that was created in the project and loaded at run-time.</p> <p>After researching this for hours, I thought it was time to resort to stackoverflow as I'm not getting anywhere. I've read numerous articles and none are giving me a correct answer, so I've got a few questions:</p> <ol> <li>How to fix my error?</li> <li>How to load the xaml model view at run-time programmatically?</li> <li>How to load the xaml model view at run-time using xaml?</li> <li>Where to call the loading of the xaml at run-time </li> </ol> <p>The sample data file i.e. SoundViewModelSampleData.xaml, looks like this:</p> <pre><code>&lt;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"&gt; &lt;vm:SoundViewModel.Animals&gt; &lt;vm:SoundGroupViewModel Title="Animals Sample"&gt; &lt;vm:SoundGroupViewModel.Items&gt; &lt;mo:SoundDataModel Title="Animals 1" FilePath="Animals.wav" /&gt; &lt;/vm:SoundGroupViewModel.Items&gt; &lt;/vm:SoundGroupViewModel&gt; &lt;/vm:SoundViewModel.Animals&gt; &lt;vm:SoundViewModel.Cartoons&gt; &lt;vm:SoundGroupViewModel Title="Cartoons Sample"&gt; &lt;vm:SoundGroupViewModel.Items&gt; &lt;mo:SoundDataModel Title="Cartoons 1" FilePath="Cartoons.wav" /&gt; &lt;mo:SoundDataModel Title="Cartoons 2" FilePath="Cartoons.wav" /&gt; &lt;/vm:SoundGroupViewModel.Items&gt; &lt;/vm:SoundGroupViewModel&gt; &lt;/vm:SoundViewModel.Cartoons&gt; &lt;/vm:SoundViewModel&gt; </code></pre> <p>The simplest code to load this programmatically that I found was:</p> <pre><code>string path = @".\SampleData\SoundViewModelSampleData.xaml"; using (System.IO.StreamReader reader = new System.IO.StreamReader(path)) { SoundViewModel vm = XamlReader.Load(reader.ReadToEnd()) as SoundViewModel; } </code></pre> <p>While I'm probably calling it from the wrong location for now, I'm getting the following error:</p> <blockquote> <p>A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll</p> <p>{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()}</p> <p><strong>Unknown parser error: Scanner 2147500037. [Line: 5 Position: 14]</strong></p> </blockquote> <p>Assuming I can resolve this error, this would take care of my question 1 &amp; 2 (fixing error and loading the data programmatically)</p> <p>Can you spot what's causing this problem?</p> <p>As mentioned above, I'm probably loading this in the wrong place i.e. from within my ViewModel when it is created when the app loads.</p> <pre><code>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; } } </code></pre> <p>}</p> <p>And in my app.xaml.cs I have the following:</p> <pre><code>public static SoundViewModel SoundViewModel { get { if (_soundViewModel == null) { _soundViewModel = new SoundViewModel(); _soundViewModel.LoadData(); } return _soundViewModel; } } </code></pre> <p>Now how can I achieve the same using just xaml for the run-time and use d:datacontext for design-time.</p> <p>I've read a few articles but they are all for wpf but most are related to loading usercontrol, etc.. but not a viewmodel</p> <p>Any help would be greatly appreciated.</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload