Skip to content
This repository was archived by the owner on Apr 23, 2023. It is now read-only.

Commit 37d5815

Browse files
fix navigation
1 parent 0e6ce8c commit 37d5815

File tree

10 files changed

+54
-29
lines changed

10 files changed

+54
-29
lines changed

Patterns/BooksSample/BooksApp/App.xaml.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using System;
1+
using Framework.Services;
2+
using System;
23
using Windows.ApplicationModel;
34
using Windows.ApplicationModel.Activation;
5+
using Windows.UI.Core;
46
using Windows.UI.Xaml;
57
using Windows.UI.Xaml.Controls;
68
using Windows.UI.Xaml.Navigation;
9+
using Microsoft.Extensions.DependencyInjection;
710

811
namespace BooksApp
912
{
@@ -61,6 +64,13 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
6164
// Ensure the current window is active
6265
Window.Current.Activate();
6366
}
67+
68+
var navigationService = ApplicationServices.Instance.ServiceProvider.GetService<INavigationService>();
69+
SystemNavigationManager.GetForCurrentView().BackRequested += (sender, e1) =>
70+
{
71+
e1.Handled = true;
72+
navigationService.GoBackAsync();
73+
};
6474
}
6575

6676
/// <summary>

Patterns/BooksSample/BooksApp/MainPage.xaml.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public MainPage()
2020

2121
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
2222
{
23-
EventAggregator<NavigationInfoEvent>.Instance.Publish(this, new NavigationInfoEvent { UseNavigation = e.NewSize.Width < 1024 });
23+
ViewModel.UseNavigation(e.NewSize.Width < 1024);
24+
FireNavigation(e.NewSize.Width);
2425
}
26+
27+
private void FireNavigation(double width) =>
28+
EventAggregator<NavigationInfoEvent>.Instance.Publish(this, new NavigationInfoEvent { UseNavigation = width < 1024 });
2529
}
2630
}

Patterns/BooksSample/BooksApp/Services/UWPNavigationService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public UWPNavigationService(UWPInitializeNavigationService initializeNavigation)
1717
_initializeNavigation = initializeNavigation ?? throw new ArgumentNullException(nameof(initializeNavigation));
1818
}
1919

20+
public bool UseNavigation { get; set; }
21+
2022
private Dictionary<string, Type> _pages;
2123
private Dictionary<string, Type> Pages => _pages ?? (_pages = _initializeNavigation.Pages);
2224

Patterns/BooksSample/BooksApp/ViewModels/MainPageViewModel.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ public class MainPageViewModel : ViewModelBase
2121
private readonly UWPInitializeNavigationService _initializeNavigationService;
2222
public MainPageViewModel(INavigationService navigationService, UWPInitializeNavigationService initializeNavigationService)
2323
{
24-
_navigationService = navigationService;
25-
_initializeNavigationService = initializeNavigationService;
24+
_navigationService = navigationService ?? throw new ArgumentNullException(nameof(navigationService));
25+
_initializeNavigationService = initializeNavigationService ?? throw new ArgumentNullException(nameof(initializeNavigationService));
2626
}
2727

2828
public void SetNavigationFrame(Frame frame) => _initializeNavigationService.Initialize(frame, _pages);
2929

30+
public void UseNavigation(bool navigation)
31+
{
32+
_navigationService.UseNavigation = navigation;
33+
}
34+
3035
public void OnNavigationSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
3136
{
3237
if (args.SelectedItem is NavigationViewItem navigationItem)
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
using BooksLib.ViewModels;
22
using Microsoft.Extensions.DependencyInjection;
3-
using Windows.UI.Xaml;
3+
using Windows.UI.Core;
44
using Windows.UI.Xaml.Controls;
5+
using Windows.UI.Xaml.Navigation;
56

67
namespace BooksApp.Views
78
{
89
public sealed partial class BookDetailPage : Page
910
{
10-
public BookDetailPage()
11-
{
12-
this.InitializeComponent();
13-
ViewModel.UseNavigation = true; // if the Page is used, enable navigation
14-
}
11+
public BookDetailPage() => InitializeComponent();
1512

1613
public BookDetailViewModel ViewModel { get; } = ApplicationServices.Instance.ServiceProvider.GetService<BookDetailViewModel>();
14+
15+
protected override void OnNavigatedTo(NavigationEventArgs e)
16+
{
17+
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
18+
19+
base.OnNavigatedTo(e);
20+
}
1721
}
1822
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using BooksLib.ViewModels;
22
using Microsoft.Extensions.DependencyInjection;
3+
using Windows.UI.Core;
34
using Windows.UI.Xaml.Controls;
5+
using Windows.UI.Xaml.Navigation;
46

57
namespace BooksApp.Views
68
{
@@ -9,10 +11,16 @@ public sealed partial class BooksPage : Page
911
public BooksPage()
1012
{
1113
InitializeComponent();
12-
ViewModel.UseNavigation = false;
1314
BookDetailUC.ViewModel = ApplicationServices.Instance.ServiceProvider.GetService<BookDetailViewModel>();
1415
}
1516

1617
public BooksViewModel ViewModel { get; } = ApplicationServices.Instance.ServiceProvider.GetService<BooksViewModel>();
18+
19+
protected override void OnNavigatedTo(NavigationEventArgs e)
20+
{
21+
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
22+
23+
base.OnNavigatedTo(e);
24+
}
1725
}
1826
}

PatternsXamarinShared/BooksLib/ViewModels/BookDetailViewModel.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public class BookDetailViewModel : EditableItemViewModel<Book>
1818
public BookDetailViewModel(IItemsService<Book> itemsService, INavigationService navigationService, IMessageService messageService, ILogger<BookDetailViewModel> logger)
1919
: base(itemsService)
2020
{
21-
_itemsService = itemsService;
22-
_navigationService = navigationService;
23-
_messageService = messageService;
21+
_itemsService = itemsService ?? throw new ArgumentNullException(nameof(itemsService));
22+
_navigationService = navigationService ?? throw new ArgumentNullException(nameof(navigationService));
23+
_messageService = messageService ?? throw new ArgumentNullException(nameof(messageService));
2424
_logger = logger;
2525

2626
itemsService.SelectedItemChanged += (sender, book) =>
@@ -29,8 +29,6 @@ public BookDetailViewModel(IItemsService<Book> itemsService, INavigationService
2929
};
3030
}
3131

32-
public bool UseNavigation { get; set; }
33-
3432
public override Book CreateCopy(Book item) =>
3533
new Book
3634
{
@@ -49,7 +47,6 @@ public override async Task OnSaveAsync()
4947
try
5048
{
5149
await _itemsService.AddOrUpdateAsync(EditItem);
52-
throw new Exception("bah");
5350
}
5451
catch (Exception ex)
5552
{
@@ -60,11 +57,10 @@ public override async Task OnSaveAsync()
6057

6158
public override async Task OnEndEditAsync()
6259
{
63-
if (UseNavigation)
60+
if (_navigationService.UseNavigation)
6461
{
6562
await _navigationService.GoBackAsync();
6663
}
67-
}
68-
64+
}
6965
}
7066
}

PatternsXamarinShared/BooksLib/ViewModels/BookItemViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Framework;
33
using Framework.Services;
44
using Framework.ViewModels;
5+
using System;
56

67
namespace BooksLib.ViewModels
78
{
@@ -13,7 +14,7 @@ public class BookItemViewModel : ItemViewModel<Book>
1314
public BookItemViewModel(Book book, IItemsService<Book> booksService)
1415
{
1516
Item = book;
16-
_booksService = booksService;
17+
_booksService = booksService ?? throw new ArgumentNullException(nameof(booksService));
1718
DeleteBookCommand = new RelayCommand(OnDeleteBook);
1819
}
1920

PatternsXamarinShared/BooksLib/ViewModels/BooksViewModel.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Framework.Services;
55
using Framework.ViewModels;
66
using System;
7+
using System.Diagnostics;
78

89
namespace BooksLib.ViewModels
910
{
@@ -18,22 +19,15 @@ public BooksViewModel(IItemsService<Book> booksService, INavigationService navig
1819
_booksService = booksService ?? throw new ArgumentNullException(nameof(booksService));
1920
_navigationService = navigationService ?? throw new ArgumentNullException(nameof(navigationService));
2021

21-
EventAggregator<NavigationInfoEvent>.Instance.Event += (sender, e) =>
22-
{
23-
UseNavigation = e.UseNavigation;
24-
};
25-
2622
PropertyChanged += async (sender, e) =>
2723
{
28-
if (UseNavigation && e.PropertyName == nameof(SelectedItem) && _navigationService.CurrentPage == PageNames.BooksPage)
24+
if (_navigationService.UseNavigation && e.PropertyName == nameof(SelectedItem) && _navigationService.CurrentPage == PageNames.BooksPage)
2925
{
3026
await _navigationService.NavigateToAsync(PageNames.BookDetailPage);
3127
}
3228
};
3329
}
3430

35-
public bool UseNavigation { get; set; }
36-
3731
public override void OnAdd()
3832
{
3933
var newBook = new Book();

PatternsXamarinShared/Framework/Services/INavigationService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Framework.Services
44
{
55
public interface INavigationService
66
{
7+
bool UseNavigation { get; set; }
78
Task NavigateToAsync(string page);
89
Task GoBackAsync();
910
string CurrentPage { get; }

0 commit comments

Comments
 (0)