Share via

Mvvm navigation in WinUI

Eduardo Gomez 3,471 Reputation points
Jun 22, 2023, 9:15 PM

I want to implement mvvm, to navigate to a new page within a NavBar.

The problem is that the docs for this package, isnot existent or comfuding (wpf.native, uwp.native, Uno.native) etc.

User's image

ViewModel


        public Command NavigateCommand { get; }

        public NavWindowViewModel() {

            NavigateCommand = new Command(NavigateAction);
        }

        private void NavigateAction(object obj) {
            throw new NotImplementedException();
        }
    }


View

 <NavigationView Background="{ThemeResource LayerOnAccentAcrylicFillColorDefault}"
                    IsBackButtonVisible="Collapsed"
                    IsSettingsVisible="False"
                    SelectedItem="{Binding SelectedItem}"
                    PaneDisplayMode="LeftCompact">
        <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="SelectionChanged">
                
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
        <NavigationView.MenuItems>
            <NavigationViewItem
                Content="Manage Class"
                Icon="People"
                Tag="ManageClassPage" />
            <NavigationViewItem
                Content="Create Meeting"
                Icon="VideoChat"
                Tag="CreateMeeting" />
        </NavigationView.MenuItems>

        <Frame />
    </NavigationView>

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
851 questions
0 comments No comments
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 21,236 Reputation points Microsoft External Staff
    Jun 23, 2023, 4:22 AM

    Hello @Eduardo Gomez , Welcome to Microsoft Q&A!

    Use EventTriggerBehavior.Actions and InvokeCommandAction to handle event ItemInvoked,then use x:Bind to link your ViewModel commands.

          <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="ItemInvoked">
                <Core:EventTriggerBehavior.Actions>
                    <Core:InvokeCommandAction Command="{x:Bind viewModel.NavigateCommand}" />
                </Core:EventTriggerBehavior.Actions>
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
    
            //using CommunityToolkit.Mvvm.ComponentModel;
            //using CommunityToolkit.Mvvm.Input;
    
    
            private ICommand _NavigateCommand;
            public ICommand NavigateCommand => this._NavigateCommand ?? (this._NavigateCommand = 
                new RelayCommand<NavigationViewSelectionChangedEventArgs>(OnItemInvoked));
    
            private void OnItemInvoked(NavigationViewSelectionChangedEventArgs args)
            {
                //NavigateTo your new page
                NavigationService.NavigateTo(args.SelectedItem);
            }
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.