This commit is contained in:
Dan Clark
2024-11-19 09:37:18 +00:00
parent 748487031d
commit 71d297b343
10 changed files with 373 additions and 362 deletions
@@ -10,14 +10,18 @@ public class ClassicDesktopStyleApplicationHandler :
{ {
public void Handle(NavigateTemplateEventArgs args) public void Handle(NavigateTemplateEventArgs args)
{ {
if (Application.Current?.ApplicationLifetime is if (Application.Current?.ApplicationLifetime
IClassicDesktopStyleApplicationLifetime lifeTime) is not IClassicDesktopStyleApplicationLifetime lifeTime)
{ {
if (args.Template is Window window) return;
}
if (args.Template is not Window window)
{ {
return;
}
lifeTime.MainWindow = window; lifeTime.MainWindow = window;
window.DataContext = args.Content; window.DataContext = args.Content;
} }
} }
}
}
+8 -7
View File
@@ -9,11 +9,16 @@ public class ContentControlHandler :
{ {
public void Handle(NavigateTemplateEventArgs args) public void Handle(NavigateTemplateEventArgs args)
{ {
if (args.Region is ContentControl contentControl) if (args.Region is not ContentControl contentControl)
{ {
if (args.Template is Control control) return;
}
if (args.Template is not Control control)
{ {
TaskCompletionSource taskCompletionSource = new(); return;
}
void HandleLoaded(object? sender, RoutedEventArgs args) void HandleLoaded(object? sender, RoutedEventArgs args)
{ {
control.Loaded -= HandleLoaded; control.Loaded -= HandleLoaded;
@@ -24,8 +29,6 @@ public class ContentControlHandler :
activation.IsActive = true; activation.IsActive = true;
} }
} }
taskCompletionSource.SetResult();
} }
void HandleUnloaded(object? sender, RoutedEventArgs args) void HandleUnloaded(object? sender, RoutedEventArgs args)
@@ -54,5 +57,3 @@ public class ContentControlHandler :
contentControl.Content = control; contentControl.Content = control;
} }
} }
}
}
-3
View File
@@ -114,9 +114,6 @@ public class ContentDialogHandler :
dialog.SecondaryButtonClick += HandleSecondaryButtonClick; dialog.SecondaryButtonClick += HandleSecondaryButtonClick;
await dialog.ShowAsync(); await dialog.ShowAsync();
dialog.PrimaryButtonClick += HandlePrimaryButtonClick;
dialog.SecondaryButtonClick += HandleSecondaryButtonClick;
} }
} }
} }
+10 -5
View File
@@ -13,11 +13,18 @@ public class FrameHandler(ITransientNavigationStore<Frame> navigationStore) :
{ {
public void Handle(NavigateTemplateEventArgs args) public void Handle(NavigateTemplateEventArgs args)
{ {
if (args.Region is Frame frame) if (args.Region is not Frame frame)
{ {
return;
}
if (args.Template is not Control control)
{
return;
}
frame.NavigationPageFactory ??= new NavigationPageFactory(); frame.NavigationPageFactory ??= new NavigationPageFactory();
if (args.Template is Control control)
{
void Navigated(Control sender) void Navigated(Control sender)
{ {
void HandleNavigatedTo(object? _, NavigationEventArgs __) void HandleNavigatedTo(object? _, NavigationEventArgs __)
@@ -197,8 +204,6 @@ public class FrameHandler(ITransientNavigationStore<Frame> navigationStore) :
postAction.Invoke(); postAction.Invoke();
} }
} }
}
}
public void Handle(NavigateBackEventArgs<Frame> args) public void Handle(NavigateBackEventArgs<Frame> args)
{ {
-7
View File
@@ -6,10 +6,3 @@ public record NavigateEventArgs(string Route,
object? Sender = null, object? Sender = null,
EventHandler? Navigated = null, EventHandler? Navigated = null,
IDictionary<string, object>? Parameters = null); IDictionary<string, object>? Parameters = null);
public record NavigateTemplateEventArgs(object Region,
object Template,
object Content,
object? Sender = null,
IDictionary<string, object>? Parameters = null);
@@ -0,0 +1,7 @@
namespace Toolkit.Foundation;
public record NavigateTemplateEventArgs(object Region,
object Template,
object Content,
object? Sender = null,
IDictionary<string, object>? Parameters = null);
+2 -5
View File
@@ -37,8 +37,7 @@ public class Navigation(IServiceProvider provider,
.GetConstructors() .GetConstructors()
.FirstOrDefault()? .FirstOrDefault()?
.GetParameters() .GetParameters()
.Select(x => .Select(x => x?.Name is not null && arguments is not null && arguments.TryGetValue(x.Name, out object? argument)
x?.Name is not null && arguments is not null && arguments.TryGetValue(x.Name, out object? argument)
? argument ? argument
: null) : null)
.Where(argument => argument is not null) .Where(argument => argument is not null)
@@ -70,9 +69,7 @@ public class Navigation(IServiceProvider provider,
object? content = contentFactory.Create(descriptor, resolvedArguments); object? content = contentFactory.Create(descriptor, resolvedArguments);
if (content is not null) if (content is not null)
{ {
Type navigationType = region is Type type ? type : region.GetType(); messenger.Send(new NavigateTemplateEventArgs(region, template, content, sender, parameters), region is string ? $"{region}" : region.GetType().Name);
messenger.Send(new NavigateTemplateEventArgs(region, template, content, sender, parameters), navigationType.Name);
if (currentSegmentIndex == segmentCount) if (currentSegmentIndex == segmentCount)
{ {
navigated?.Invoke(this, EventArgs.Empty); navigated?.Invoke(this, EventArgs.Empty);
+1 -1
View File
@@ -75,7 +75,7 @@ public class NavigateAction :
ImmutableDictionary<string, object>.Empty; ImmutableDictionary<string, object>.Empty;
observableViewModel.Messenger.Send(new NavigateEventArgs(Route, Region.Equals(this) ? content : Region, Scope ?? null, observableViewModel.Messenger.Send(new NavigateEventArgs(Route, Region.Equals(this) ? content : Region, Scope ?? null,
content.DataContext, Navigated, parameters)); content, Navigated, parameters));
} }
} }
+8 -9
View File
@@ -9,11 +9,16 @@ public class ContentControlHandler :
{ {
public void Handle(NavigateTemplateEventArgs args) public void Handle(NavigateTemplateEventArgs args)
{ {
if (args.Region is ContentControl contentControl) if (args.Region is not ContentControl contentControl)
{ {
if (args.Template is Control control) return;
}
if (args.Template is not Control control)
{ {
TaskCompletionSource taskCompletionSource = new(); return;
}
void HandleLoaded(object? sender, RoutedEventArgs args) void HandleLoaded(object? sender, RoutedEventArgs args)
{ {
control.Loaded -= HandleLoaded; control.Loaded -= HandleLoaded;
@@ -24,8 +29,6 @@ public class ContentControlHandler :
activation.IsActive = true; activation.IsActive = true;
} }
} }
taskCompletionSource.SetResult();
} }
void HandleUnloaded(object? sender, RoutedEventArgs args) void HandleUnloaded(object? sender, RoutedEventArgs args)
@@ -49,10 +52,6 @@ public class ContentControlHandler :
control.Unloaded += HandleUnloaded; control.Unloaded += HandleUnloaded;
control.DataContext = args.Content; control.DataContext = args.Content;
contentControl.Content = null;
contentControl.Content = control; contentControl.Content = control;
} }
} }
}
}
+14 -6
View File
@@ -1,4 +1,5 @@
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Toolkit.Foundation; using Toolkit.Foundation;
namespace Toolkit.WinUI; namespace Toolkit.WinUI;
@@ -8,8 +9,19 @@ public class ContentDialogHandler :
{ {
public async void Handle(NavigateTemplateEventArgs args) public async void Handle(NavigateTemplateEventArgs args)
{ {
if (args.Template is ContentDialog dialog) if (args.Template is not ContentDialog dialog)
{ {
return;
}
if (args.Sender is not Control parent)
{
return;
}
dialog.XamlRoot = parent.XamlRoot;
dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
dialog.DataContext = args.Content; dialog.DataContext = args.Content;
async void HandlePrimaryButtonClick(ContentDialog sender, async void HandlePrimaryButtonClick(ContentDialog sender,
@@ -110,9 +122,5 @@ public class ContentDialogHandler :
dialog.SecondaryButtonClick += HandleSecondaryButtonClick; dialog.SecondaryButtonClick += HandleSecondaryButtonClick;
await dialog.ShowAsync(); await dialog.ShowAsync();
dialog.PrimaryButtonClick += HandlePrimaryButtonClick;
dialog.SecondaryButtonClick += HandleSecondaryButtonClick;
}
} }
} }