Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM - Open Second View Windows 8
    primarykey
    data
    text
    <p>Edit: Example App -> <a href="http://www10.zippyshare.com/v/29730402/file.html" rel="nofollow noreferrer">http://www10.zippyshare.com/v/29730402/file.html</a></p> <p>I'm programming an app for Windows 8 &amp; Windows Phone. I'm using the portable class library (see this article <a href="http://blog.tattoocoder.com/2013/01/portable-mvvm-light-move-your-view.html" rel="nofollow noreferrer">http://blog.tattoocoder.com/2013/01/portable-mvvm-light-move-your-view.html</a>).</p> <p>My problem is: How can I open a second window by clicking on a button by using the MVVM-pattern? I don't want to do it in the behind-code.</p> <p>My datacontext for the Windows 8 app looks in the xaml like this</p> <pre><code>DataContext="{Binding Main, Source={StaticResource Locator}}" </code></pre> <p>which uses the ViewModel of the PCL (= ViewModel for both, W8 &amp; WP8)</p> <pre><code>xmlns:vm="using:Mvvm.PCL.ViewModel" </code></pre> <p>I don't know how to assign 2 datacontext to my MainPage.xaml, nor do I know how to assign my MainPage.xaml to the ViewModel for my Windows 8 app.</p> <p>I've tried something like this:</p> <pre><code>Command="{Binding DisplayView}" CommandParameter="SecondView" </code></pre> <p>but the program uses the ViewModel for both platforms and I can't program there the windows-assignment for the specific platforms. (it should look something like this <a href="https://stackoverflow.com/questions/3100471/opening-multiple-views-by-clicking-button-using-mvvm-silverlight-approach">Opening multiple views by clicking button using MVVM silverlight approach</a> ...)</p> <p>To make it clear:</p> <p>I have 2 projects. Both MainWindows of the projects refer to the ViewModel of the "MainProject". If I want to click on a button in my MainWindow, I want to open a new view, but I can only use the ViewModel for both projects, which means that I can't use any views of the 2 projects in my ViewModel of the "MainProject".</p> <p>edit: I've seen that many people use ContentControl. (Still doesn't work. Btw im new to MVVM).</p> <pre><code>&lt;ContentControl Grid.Row="2" Content="{Binding CurrentView}" IsTabStop="False" Margin="10" /&gt; &lt;Button Command="{Binding DisplayView}" CommandParameter="SecondView"&gt; </code></pre> <p>MainViewModel.cs (For both platforms)</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows.Input; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using Mvvm.PCL.Model; #if NETFX_CORE using Mvvm.Store.Views; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; #endif namespace Mvvm.PCL.ViewModel { public class MainViewModel : ViewModelBase, INotifyPropertyChanged { /// &lt;summary&gt; /// Initializes a new instance of the MainViewModel class. /// &lt;/summary&gt; public MainViewModel() { #if NETFX_CORE DisplayView = new RelayCommand&lt;string&gt;(DisplayViewCommandExecute); #endif } #region Commands public RelayCommand&lt;string&gt; DisplayView { get; private set; } #endregion #if NETFX_CORE #region CurrentView Property public const string CurrentViewPropertyName = "CurrentView"; private Page _currentView; public Page CurrentView { get { return _currentView; } set { if (_currentView == value) return; _currentView = value; RaisePropertyChanged(CurrentViewPropertyName); } } private SecondView _secondview = new SecondView(); public SecondView SecondView { get { return _secondview; } } #endregion private void DisplayViewCommandExecute(string viewName) { switch (viewName) { case "SecondView": CurrentView = _secondview; var frame = (Frame)Window.Current.Content; frame.Navigate(typeof(SecondView)); break; } } #endif } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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