-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support replacing root for MainPage, non-Shell Navigation, modals (#104)
- Loading branch information
1 parent
42b2146
commit f1fb4ca
Showing
13 changed files
with
339 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/BlazorBindings.Maui/Elements/Handlers/ApplicationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.Maui.Controls; | ||
using MC = Microsoft.Maui.Controls; | ||
|
||
namespace BlazorBindings.Maui.Elements.Handlers | ||
{ | ||
internal class ApplicationHandler : IMauiContainerElementHandler | ||
{ | ||
private readonly Application _application; | ||
|
||
public ApplicationHandler(Application application) | ||
{ | ||
_application = application; | ||
} | ||
|
||
public void AddChild(MC.BindableObject child, int physicalSiblingIndex) | ||
{ | ||
_application.MainPage = (MC.Page)child; | ||
} | ||
|
||
public int GetChildIndex(MC.BindableObject child) | ||
{ | ||
return Equals(_application.MainPage, child) ? 0 : -1; | ||
} | ||
|
||
public void RemoveChild(MC.BindableObject child) | ||
{ | ||
// It is not allowed to have no MainPage. | ||
} | ||
|
||
public MC.BindableObject ElementControl => _application; | ||
public object TargetElement => _application; | ||
public void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using MC = Microsoft.Maui.Controls; | ||
|
||
namespace BlazorBindings.Maui | ||
{ | ||
internal class NavigationHandler : IMauiContainerElementHandler | ||
{ | ||
private readonly NavigationTarget _target; | ||
private readonly MC.INavigation _navigation; | ||
private readonly bool _animated; | ||
private readonly TaskCompletionSource _taskCompletionSource = new(); | ||
private MC.Page _currentPage; | ||
private bool _firstAdd = true; | ||
|
||
public NavigationHandler(MC.INavigation navigation, NavigationTarget target, bool animated) | ||
{ | ||
_target = target; | ||
_navigation = navigation; | ||
_animated = animated; | ||
} | ||
|
||
public Task WaitForNavigation() => _taskCompletionSource.Task; | ||
public event Action PageClosed; | ||
|
||
public async Task AddChildAsync(MC.Page child) | ||
{ | ||
// The order of AddChild and RemoveChild is undetermined. We need to make sure that the previous page is removed. | ||
if (_currentPage != null) | ||
await RemoveChildAsync(_currentPage); | ||
|
||
_currentPage = child; | ||
|
||
if (_target == NavigationTarget.Modal) | ||
{ | ||
await _navigation.PushModalAsync(child, _firstAdd && _animated); | ||
} | ||
else | ||
{ | ||
await _navigation.PushAsync(child, _firstAdd && _animated); | ||
} | ||
|
||
_taskCompletionSource.TrySetResult(); | ||
_firstAdd = false; | ||
|
||
child.ParentChanged += ParentChanged; | ||
} | ||
|
||
public async Task RemoveChildAsync(MC.Page child) | ||
{ | ||
child.ParentChanged -= ParentChanged; | ||
if (_target == NavigationTarget.Modal) | ||
{ | ||
if (_navigation.ModalStack.LastOrDefault() == child) | ||
{ | ||
await _navigation.PopModalAsync(animated: false); | ||
} | ||
} | ||
else | ||
{ | ||
if (_navigation.NavigationStack.Contains(child)) | ||
_navigation.RemovePage(child); | ||
} | ||
} | ||
|
||
private void ParentChanged(object sender, EventArgs e) | ||
{ | ||
var page = sender as MC.Page; | ||
|
||
if (page == _currentPage && page.Parent == null) | ||
{ | ||
PageClosed?.Invoke(); | ||
} | ||
|
||
page.ParentChanged -= ParentChanged; | ||
} | ||
|
||
public async void RemoveChild(MC.BindableObject child) | ||
{ | ||
await RemoveChildAsync((MC.Page)child); | ||
} | ||
|
||
public async void AddChild(MC.BindableObject child, int physicalSiblingIndex) | ||
{ | ||
await AddChildAsync((MC.Page)child); | ||
} | ||
|
||
public int GetChildIndex(MC.BindableObject child) => -1; | ||
public void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName) { } | ||
public MC.BindableObject ElementControl => null; | ||
public object TargetElement => null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace BlazorBindings.Maui | ||
{ | ||
internal enum NavigationTarget | ||
{ | ||
Navigation, Modal | ||
} | ||
} |
15 changes: 13 additions & 2 deletions
15
src/BlazorBindings.UnitTests/Components/PageContentWithDispose.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/BlazorBindings.UnitTests/Components/SwitchablePages.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@if (_switch) | ||
{ | ||
<ContentPage Title="Page1"> | ||
<Button OnClick="Switch" /> | ||
</ContentPage> | ||
} | ||
else | ||
{ | ||
<ContentPage Title="Page2"> | ||
<Button OnClick="Switch" /> | ||
</ContentPage> | ||
} | ||
|
||
@code { | ||
bool _switch = true; | ||
void Switch() => _switch = !_switch; | ||
} |
Oops, something went wrong.