locked
Convert code to MVVM Pattern RRS feed

  • Question

  • Hi Experts,

    I want to convert my code to MVVM pattern.

    Currently my code is:-

            Sub New()
    
                InitializeComponent()
    
                AddHandler Me.Closing, AddressOf Main_closing
            End Sub
    
            Private Sub Main_closing(sender As Object, e As CancelEventArgs)
                Globals.IsBestBillStatementActive = False
                Global.Main.ResetSessionTimeoutTimer()
            End Sub

    For converting it to MVVM pattern. I have changed the code I have changes in the view as follows:-

    Added namespace in view:-

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

    Added Event

        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Closing">
                <i:InvokeCommandAction Command="{Binding CloseCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

    Please tell what other changes I need to make in View and ViewModel.

    Thanks,
    Rahul

    Thursday, June 11, 2015 1:05 PM

Answers

  • Hi rahul_agg11, first you need to know What MVVM is. So here is an explanation.

    Some samples here.

    Regards.


    Christian Amado
    Software Engineer | MCPD: Windows Phone Developer | MCTS: Silverlight Developer
    Don't forget to mark the right answer and vote up if helps you.
    MCP Profile
    My code samples

    • Marked as answer by Weiwei Cai Wednesday, June 17, 2015 2:18 AM
    Thursday, June 11, 2015 3:21 PM
  • It's kind of a huge subject.

    But basically, you need a CloseCommand in a viewmodel.

    And you set the context of the view to that viewmodel.

    In there you'd have a method - maybe that's a sub in vb

    Something like this maybe

    Private Sub CloseExecute()
         Globals.IsBestBillStatementActive = False
         Global.Main.ResetSessionTimeoutTimer()
    End Sub

    You then wire that up to a command.

    I'm not at all sure about in vb but some c# commands:

            public RelayCommand<string> Insert { get; set; }
            public RelayCommand<string> Commit { get; set; }
            public RelayCommand<string> Refresh { get; set; }
    
            public AdminADGroupSiteViewModel()
            {
                ....
    
                Commit = new RelayCommand<string>(ExecuteCommit);
                Insert = new RelayCommand<string>(ExecuteInsert);
                Refresh = new RelayCommand<string>(ExecuteRefresh);
                var msg = new Admin { IsEditing = true };
                Messenger.Default.Send<Admin>(msg);
    
    ...
                LoadSites();
            }
    The AdminADGroupSiteViewModel() is the constructor of the viewmodel.

    That relaycommand is from the mvvmlight framework, which I recommend.

    https://msdn.microsoft.com/en-us/magazine/dn237302.aspx

    You can use Nuget to search on and add "mvvmlight libraries only" to your project.


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    • Marked as answer by Weiwei Cai Wednesday, June 17, 2015 2:18 AM
    Thursday, June 11, 2015 3:47 PM

All replies

  • Hi rahul_agg11, first you need to know What MVVM is. So here is an explanation.

    Some samples here.

    Regards.


    Christian Amado
    Software Engineer | MCPD: Windows Phone Developer | MCTS: Silverlight Developer
    Don't forget to mark the right answer and vote up if helps you.
    MCP Profile
    My code samples

    • Marked as answer by Weiwei Cai Wednesday, June 17, 2015 2:18 AM
    Thursday, June 11, 2015 3:21 PM
  • It's kind of a huge subject.

    But basically, you need a CloseCommand in a viewmodel.

    And you set the context of the view to that viewmodel.

    In there you'd have a method - maybe that's a sub in vb

    Something like this maybe

    Private Sub CloseExecute()
         Globals.IsBestBillStatementActive = False
         Global.Main.ResetSessionTimeoutTimer()
    End Sub

    You then wire that up to a command.

    I'm not at all sure about in vb but some c# commands:

            public RelayCommand<string> Insert { get; set; }
            public RelayCommand<string> Commit { get; set; }
            public RelayCommand<string> Refresh { get; set; }
    
            public AdminADGroupSiteViewModel()
            {
                ....
    
                Commit = new RelayCommand<string>(ExecuteCommit);
                Insert = new RelayCommand<string>(ExecuteInsert);
                Refresh = new RelayCommand<string>(ExecuteRefresh);
                var msg = new Admin { IsEditing = true };
                Messenger.Default.Send<Admin>(msg);
    
    ...
                LoadSites();
            }
    The AdminADGroupSiteViewModel() is the constructor of the viewmodel.

    That relaycommand is from the mvvmlight framework, which I recommend.

    https://msdn.microsoft.com/en-us/magazine/dn237302.aspx

    You can use Nuget to search on and add "mvvmlight libraries only" to your project.


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    • Marked as answer by Weiwei Cai Wednesday, June 17, 2015 2:18 AM
    Thursday, June 11, 2015 3:47 PM