none
How can I add a button that takes me back to the start up page in MvvmLight when using UserControl RRS feed

  • Question

  • Hi,

    I'm currently practicing MvvmLight and I'm kind of stock. What I currently have is an app with three files, mainPage, page2 and page3, mainPage is the startUp page and contains two buttons, Page 2 and Page 3, each takes you to its page respectively, what I'm not sure how to do is add a third button for Main Page so I can go back to it, right now once you click the Page 2 or Page 3 button you don't have a way to go back to <g class="gr_ gr_99 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="99" id="99">mainPage</g>. 

    How can I add a button that takes me back to the page in MvvmLight? 

    I hope my question makes sense. 


    Main Page XMAL File

    <Window x:Class="Tool.Content.MainPage.View.MainPageView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
            xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            xmlns:ignore="http://www.galasoft.ch/ignore"
            mc:Ignorable="d ignore"
            DataContext="{Binding MainPageViewModel, Source={StaticResource Locator}}">

        <Grid>

            <Button x:Name="Page2Button" Content="Page 2" HorizontalAlignment="Left" Margin="156,56,0,0" VerticalAlignment="Top" Width="75">
                <Custom:Interaction.Triggers>
                    <Custom:EventTrigger EventName="Click">
                        <GalaSoft_MvvmLight_Command:EventToCommand x:Name="Page2ButtonClicked"
                         Command="{Binding Page2Command, Mode=OneWay}" />
                    </Custom:EventTrigger>
                </Custom:Interaction.Triggers>
            </Button>

            <Button x:Name="Page3Button" Content="Page 3" HorizontalAlignment="Left" Margin="259,56,0,0" VerticalAlignment="Top" Width="75">
                <Custom:Interaction.Triggers>
                    <Custom:EventTrigger EventName="Click">
                        <GalaSoft_MvvmLight_Command:EventToCommand x:Name="Page3ButtonClicked"
                         Command="{Binding Page3Command, Mode=OneWay}" />
                    </Custom:EventTrigger>
                </Custom:Interaction.Triggers>
            </Button>

            <ContentControl Name= "contentControl1" HorizontalAlignment="Left" Margin="10,105,0,0" VerticalAlignment="Top" Height="390" Width="740">

            </ContentControl>

        </Grid>
    </Window>

    Main Page .xaml.cs file

    using System.Windows;
    using GalaSoft.MvvmLight.Messaging;
    using Tool.ViewModel;
    using Tool.Content.Page2.View;
    using Tool.Content.Page3.View;

    namespace Tool.Content.MainPage.View
    {
        /// <summary>
        /// Description for MainPageView.
        /// </summary>
        public partial class MainPageView : Window
        {
            /// <summary>
            /// Initializes a new instance of the MainPageView class.
            /// </summary>
            public MainPageView()
            {
                InitializeComponent();

                Closing += (s, e) => ViewModelLocator.Cleanup();
                Messenger.Default.Register<GoToPageMessage>
                    (
                        this,
                        (action) => ReceiveMessage(action)
                    );
            }

            private Page2View _page2View;
            private Page2View Page2View
            {
                get {
                    if (_page2View == null)
                        _page2View = new Page2View();
                    return _page2View;
                }
            }


            private Page3View _page3View;
            private Page3View Page3View
            {
                get {
                    if (_page3View == null)
                        _page3View = new Page3View();
                    return _page3View;
                }
            }


            private object ReceiveMessage(GoToPageMessage action)
            {
                switch (action.PageName) {
                    case "Page2View":
                        if (this.contentControl1.Content != this.Page2View)
                            this.contentControl1.Content = this.Page2View;
                        break;
                    case "Page3View":
                        if (this.contentControl1.Content != this.Page3View)
                            this.contentControl1.Content = this.Page3View;
                        break;
                    default:
                        break;
                }
                return null;
            }
        }
    }

    Main Page .cs File

    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;

    namespace Tool.Content.MainPage.ViewModel
    {
        /// <summary>
        /// This class contains properties that a View can data bind to.
        /// <para>
        /// See http://www.galasoft.ch/mvvm
        /// </para>
        /// </summary>
        public class MainPageViewModel : ViewModelBase
        {
            /// <summary>
            /// Initializes a new instance of the MainPageViewModel class.
            /// </summary>
            public MainPageViewModel()
            {
                Page2Command = new RelayCommand(() => GoToPage2());
                Page3Command = new RelayCommand(() => GoToPage3());
            }

            public RelayCommand Page2Command { get; private set; }
            public RelayCommand Page3Command { get; private set; }

            private object GoToPage2()
            {
                var msg = new GoToPageMessage() { PageName = "Page2View" };
                Messenger.Default.Send<GoToPageMessage>(msg);
                return null;
            }

            private object GoToPage3()
            {
                var msg = new GoToPageMessage() { PageName = "Page3View" };
                Messenger.Default.Send<GoToPageMessage>(msg);
                return null;
            }

        }
    }

    Page2 XAML File

    <UserControl x:Class="Tool.Content.Page2.View.Page2View"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:ignore="http://www.galasoft.ch/ignore"
            mc:Ignorable="d ignore"
            DataContext="{Binding Page2ViewModel, Source={StaticResource Locator}}">

        <Grid Height="303" Width="394">
            <RadioButton x:Name="radioButton" Content="RadioButton" HorizontalAlignment="Left" Margin="174,73,0,0" VerticalAlignment="Top"/>
            <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="100" Margin="43,30,0,0" VerticalAlignment="Top" Width="100"/>
            <Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="43,170,0,0" VerticalAlignment="Top"/>
            <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="160,196,0,0" VerticalAlignment="Top" Width="75"/>
        </Grid>
    </UserControl>

    Page2 .XAML.CS File

    using System.Windows;
    using System.Windows.Controls;

    namespace Tool.Content.Page2.View
    {
        /// <summary>
        /// Description for Page2View.
        /// </summary>
        public partial class Page2View : UserControl
        {
            /// <summary>
            /// Initializes a new instance of the Page2View class.
            /// </summary>
            public Page2View()
            {
                InitializeComponent();
            }
        }
    }

    Page2 .CS File

    using GalaSoft.MvvmLight;

    namespace Tool.Content.Page2.ViewModel
    {
        /// <summary>
        /// This class contains properties that a View can data bind to.
        /// <para>
        /// See http://www.galasoft.ch/mvvm
        /// </para>
        /// </summary>
        public class Page2ViewModel : ViewModelBase
        {
            /// <summary>
            /// Initializes a new instance of the Page2ViewModel class.
            /// </summary>
            public Page2ViewModel()
            {
            }
        }
    }

    Page3 Code is the same as Page2...

    ViewModelLocator.cs

    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Ioc;
    using Microsoft.Practices.ServiceLocation;
    using Tool.Model;
    using Tool.Content.MainPage.ViewModel;
    using Tool.Content.Page2.ViewModel;
    using Tool.Content.Page3.ViewModel;

    namespace Tool.ViewModel
    {
        /// <summary>
        /// This class contains static references to all the view models in the
        /// application and provides an entry point for the bindings.
        /// <para>
        /// See http://www.mvvmlight.net
        /// </para>
        /// </summary>
        public class ViewModelLocator
        {
            static ViewModelLocator()
            {
                ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

                if (ViewModelBase.IsInDesignModeStatic) {
                    SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
                }
                else {
                    SimpleIoc.Default.Register<IDataService, DataService>();
                }

                /// Register  ViewModels here
                SimpleIoc.Default.Register<MainPageViewModel>();
                SimpleIoc.Default.Register<Page2ViewModel>();
                SimpleIoc.Default.Register<Page3ViewModel>();
            }

            /// <summary>
            /// Gets the MainPageViewModel property.
            /// </summary>
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
                "CA1822:MarkMembersAsStatic",
                Justification = "This non-static member is needed for data binding purposes.")]
            public MainPageViewModel MainPageViewModel
            {
                get {
                    return ServiceLocator.Current.GetInstance<MainPageViewModel>();
                }
            }

            /// <summary>
            /// Gets the Page2ViewModel property.
            /// </summary>
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
                "CA1822:MarkMembersAsStatic",
                Justification = "This non-static member is needed for data binding purposes.")]
            public Page2ViewModel Page2ViewModel
            {
                get {
                    return ServiceLocator.Current.GetInstance<Page2ViewModel>();
                }
            }


            /// <summary>
            /// Gets the Page3ViewModel property.
            /// </summary>
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
                "CA1822:MarkMembersAsStatic",
                Justification = "This non-static member is needed for data binding purposes.")]
            public Page3ViewModel Page3ViewModel
            {
                get {
                    return ServiceLocator.Current.GetInstance<Page3ViewModel>();
                }
            }

            /// <summary>
            /// Cleans up all the resources.
            /// </summary>
            public static void Cleanup()
            {
            }
        }
    }

    GoToPageMessage

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Tool
    {
        class GoToPageMessage
        {
            public string PageName { get; set; }
        }
    }







    • Edited by fsdolphin Friday, October 27, 2017 12:57 AM
    • Moved by Bob Ding Tuesday, November 7, 2017 9:33 AM
    Friday, October 27, 2017 12:48 AM

All replies

  • Hi,

    Since MvvmLight is outside of this forum scope of support, your thread will move to off-topic forum.

    I suggest you ask question here:https://stackoverflow.com/questions/tagged/mvvm-light

    Your understanding and cooperation will be grateful.

    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 27, 2017 6:36 AM