Question

I have a window which will contain multiple subviews. For example, at start-up the user will be presented a login screen and when the user presses 'Login' (and it succeeds) the window should be updated to a new view.

Currently I have created a Window in XAML (the main window) and then a page, which is added to the frame of the window. The idea is that when the user presses login, I which to notify the main window and tell it that the login was a success and it should change view to something else. This will be a very common task and it will be triggered both by its subviews and other actions (such as interaction with menu bar). Is this the right approach?

If so, how would I notify the main window that the subview is finished? Also, I currently uses a page as subview; is this correct?

One goal with this project is for me to learn MVVM, so a MVVM-motivation of the answer is much appreciated!

Thanks in advance!


XAML for MainWindow

<Window x:Class="CAST.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"
        >
    <Frame Source="/LoginPage.xaml"/>
</Window>
Was it helpful?

Solution

I think view inheritance will be your friend in this pursuit. You should think about the most basic operations that all views will need to share. For example, if its is a database app then you may conceivably have methods or events such as:

BeforeInsert
AfterInsert
BeforeUpdate
AfterUpdate
BeforeCancel
AfterCancel
OnError
...
...

For view switching your basic operations could be:

 OnViewLoad
 OnViewUnload
 Validate
 ...

Then I would place all that code in a generic manner into a base view BaseView class. After you have a base view then you can make all normal views inherit the BaseView's functionality. You can further this model by creating a BaseDataView:BaseView in which you introduce your generic database UI operations such as validation.

Your main form could then primarly work with the base or super class such as ((BaseView)currentView).ValidateForm() and so on and so fourth.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top