0

Im having problem sharing a windows viewmodel to the windows hosted frame.

Therefore I made a static viewmodel for the mainwindow, so any class can edit it´s properties:

class GUICollection
{
    public static MainWindowViewModel MainWindowViewModel = new MainWindowViewModel();

}

This is then set into the MainWindows datacontext:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = GUICollection.MainWindowViewModel;
    }
}

This is the windows xaml:

<Window x:Class="MVVMFrameQuestiontoStackOverflow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Frame NavigationUIVisibility="Hidden" Source="{Binding MainWindow.FrameURI}"/>
</Grid>

It´s view model:

class MainWindowViewModel
{
    private string startUpURI;
    private object startUpDataContext;
    private MainWindowModel mainWindow;


    public MainWindowViewModel()
    {
        startUpURI = "pack://application:,,,/MVVMFrameQuestiontoStackOverflow;component/Page1.xaml";
        mainWindow = new MainWindowModel(startUpURI);
    }

    /// <summary>
    /// Gets the MainWindow instance
    /// </summary>
    public MainWindowModel MainWindow
    {
        get
        {
            return mainWindow;
        }
    }
}

So from here I can choose the frame Source, which means I can choose which view to show. However Im wondering if I could avoid the static initiliazing and still being able to access the mainwindows FrameURI property (Here is my current logic):

    public Page1()
    {
        InitializeComponent();
        DataContext = new MainMenuViewModel();
        //Statement below causes an exception, but the whole issue is about accesing this without using a static instance.
        GUICollection.MainWindowViewModel.MainWindow.FrameURI = "Change MainWindows FrameURI property!";
    }

Is the same behaviour able to produce without using a static class? If so an example would be warmly appreciated.

Thanks on advance!

2
  • What reason do you have for not wanting to use a static class? What problem does having GUICollection.MainWindowViewModel as a static object cause for you? Commented May 12, 2014 at 20:09
  • @eshs Since it goes against the MVVM pattern? Also now the GUIs memory must be kept on the RAM. So I just believe there is a better approach than this. Commented May 13, 2014 at 14:37

1 Answer 1

1

I think in fact you problem is that you haven't understood MVVM and use a mix between MVC and MVVM : You create the view (mainWindows) in the ViewModel wich is forbidden.

Your App must load the main view.

In the constructor of the main view you should create the view-model as a private field.

When you will create new windows (and you should only do that from view, never from viewmodel) you will give the viewmodel datacontext object as a parameter for the new view, wich will give it to it's own viewmodel via parameters.

If you have Model object(s) wich is(are) shared throught all the application, create it in the App launch method, and pass it throught views via their constructors as a parameter.

Sign up to request clarification or add additional context in comments.

4 Comments

OK, your mentioning "new windows", however Im trying to create pages, that should be displayed inside a frame. And I have problems getting how to set that up properly, since the frame is not the same as a Window if I got it right. And also a question, when you say view your talking about the code behind of the xaml? If so, I have heard that it should only contain the datacontext, and it´s up to the viewmodel, to actually create the logic. Source: youtu.be/EpGvqVtSYjs?t=20m38s
Yes main problem with MVVM is to understand what they call 'logic'. In fact they speak about "manipulating data" and not "UI logic". A good article on the subject : link I never used Frames, but theorically, process is the same : you have a parent view (often a windows, here a frame) wich have subcomponent (userControls, dialog Screen or here pages). Your parent view create theses subcomponents (during initialization or responding to a event) and so could pass them parameters.
I see, the problem then is that how do I actually make the frame load in that "view". Currently databinding, does the job displaying the "view". But if I create the view in code, assign datacontext etc then I must call the frames Navigate method. To load in the "view", and all of a sudden I have to name the frame and that seems to also violate MVVM?
Stric application of theorical mvvm implie heavy messaging system (wich exist in some dedicated framework, you can see stackoverflow.com/questions/5069783/… for exemple). A common more lightway approach is to skip it, and put some 'view manipulation only" code in the views codebehind. It doesn't violate MVVM essence. Up to you to choose your approach (you have many debates about that on the net if you search about MVVM, since it's no a stric defined academical pattern)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.