Ghost1372

everything can be handy

NavigationService

First Create a Class myPageService and inherit from PageServiceEx

then in this class in ctor add your pages with a key into _pageKeyToTypeMap dictionary:

1
2
3
4
5
6
7
8
9
10
11
12
public class myPageService : PageServiceEx
{
public myPageService()
{
_pageKeyToTypeMap = new Dictionary<string, Type>
{
{ "BlankPage1", typeof(BlankPage1) },
{ "BlankPage2", typeof(BlankPage2) },
{ "BlankPage3", typeof(BlankPage3) },
};
}
}

you can also inherit from IPageService

now create a new myPageService class, and here you can set DefaultPage or SettingsPage for NavigationView:

1
2
3
var pageService = new myPageService();
pageService.SetDefaultPage(typeof(HomeLandingsPage));
pageService.SetSettingsPage(typeof(GeneralPage));

after creating myPageService, Create INavigationViewServiceEx and INavigationServiceEx:

1
2
3
4
5
6
INavigationViewServiceEx navigationViewService;
INavigationServiceEx navigationService;

navigationService = new NavigationServiceEx(pageService);
navigationService.Frame = navFrame;
navigationViewService = new NavigationViewServiceEx(navigationService, pageService);

as you can see, we should set Frame in navigationService.

then we should call Initialize Method:

1
navigationViewService.Initialize(navigationView);

now you should add a NavigateTo in your NavigationViewItem:

1
<NavigationViewItem helper:NavigationHelperEx.NavigateTo="BlankPage1" Content="First" />

BlankPage1 is a key, you defined it in myPageService.

Config

there are some config methods:

ConfigAutoSuggestBox

You can search in the autosuggestbox and be navigated to the results page

1
navigationViewService.ConfigAutoSuggestBox(autoSuggestBox);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<NavigationView Name="navigationView">
<NavigationView.AutoSuggestBox>
<AutoSuggestBox Name="autoSuggestBox" />
</NavigationView.AutoSuggestBox>
<NavigationView.MenuItems>
<NavigationViewItem dev:NavigationHelper.NavigateTo="BlankPage1"
Content="First" />
<NavigationViewItem Content="Second">
<NavigationViewItem.MenuItems>
<NavigationViewItem dev:NavigationHelper.NavigateTo="BlankPage2"
Content="Third" />
<NavigationViewItem Content="Four">
<NavigationViewItem.MenuItems>
<NavigationViewItem dev:NavigationHelper.NavigateTo="BlankPage3"
Content="Five" />
</NavigationViewItem.MenuItems>
</NavigationViewItem>
</NavigationViewItem.MenuItems>
</NavigationViewItem>
</NavigationView.MenuItems>
<Frame Name="navFrame" />
</NavigationView>

Complete Codes

1
2
3
4
5
6
7
8
9
10
11
12
13
INavigationViewServiceEx navigationViewService;
INavigationServiceEx navigationService;

var pageService = new myPageService();
pageService.SetDefaultPage(typeof(HomeLandingsPage));
pageService.SetSettingsPage(typeof(GeneralPage));

navigationService = new NavigationServiceEx(pageService);
navigationService.Frame = navFrame;

navigationViewService = new NavigationViewServiceEx(navigationService, pageService);
navigationViewService.Initialize(navigationView);
navigationViewService.ConfigAutoSuggestBox(autoSuggestBox);

DevWinUI

MVVM Pattern

you can use MVVM pattern:

First Register your Services:

1
2
3
4
var services = new ServiceCollection();
services.AddSingleton<IPageServiceEx, myPageService>();
services.AddSingleton<INavigationServiceEx, NavigationServiceEx>();
services.AddTransient<INavigationViewServiceEx, NavigationViewServiceEx>();

then, in your MainViewModel

1
2
3
4
5
6
7
public INavigationServiceEx NavigationService { get; }
public INavigationViewServiceEx NavigationViewService { get; }
public MainViewModel(INavigationServiceEx navigationService, INavigationViewServiceEx navigationViewService)
{
NavigationService = navigationService;
NavigationViewService = navigationViewService;
}

finally in your MainPage.xaml.cs

1
2
ViewModel.NavigationService.Frame = frame;
ViewModel.NavigationViewService.Initialize(navigationView);

Demo

you can run demo and see this feature.

DevWinUI

0%