Skip to content

Commit c722f5e

Browse files
committed
重新配置跳转逻辑
1 parent 734c6a8 commit c722f5e

1 file changed

Lines changed: 86 additions & 24 deletions

File tree

src/UnoFileDownloader/UnoFileDownloader/App.xaml.cs

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
using System;
22

3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Localization;
35
using Microsoft.Extensions.Logging;
46

57
using Uno.Resizetizer;
8+
using UnoFileDownloader.Business;
9+
using UnoFileDownloader.Business.Models;
10+
using UnoFileDownloader.Presentation;
11+
using UnoFileDownloader.Utils;
612

713
namespace UnoFileDownloader;
814
public partial class App : Application
@@ -17,40 +23,96 @@ public App()
1723
}
1824

1925
protected Window? MainWindow { get; private set; }
26+
protected IHost? Host { get; private set; }
2027

21-
protected override void OnLaunched(LaunchActivatedEventArgs args)
28+
protected async override void OnLaunched(LaunchActivatedEventArgs args)
2229
{
23-
MainWindow = new Window();
30+
31+
32+
var builder = this.CreateBuilder(args)
33+
.UseToolkitNavigation()
34+
.Configure(host =>
35+
{
36+
host.UseConfiguration(configure: configBuilder =>
37+
configBuilder
38+
.EmbeddedSource<App>()
39+
.Section<AppConfig>())
40+
.UseLocalization()
41+
.ConfigureServices((context, services) =>
42+
{
43+
services.AddSingleton<IDispatcherQueueProvider>(c =>
44+
{
45+
return new DispatcherQueueProvider(MainWindow!.DispatcherQueue);
46+
});
47+
48+
// 下载文件管理器
49+
services.AddSingleton<DownloadFileListManager>();
50+
})
51+
.UseNavigation(ReactiveViewModelMappings.ViewModelMappings, RegisterRoutes)
52+
;
53+
});
54+
MainWindow = builder.Window;
55+
//MainWindow = new Window();
2456
#if DEBUG
2557
MainWindow.EnableHotReload();
2658
#endif
27-
28-
29-
// Do not repeat app initialization when the Window already has content,
30-
// just ensure that the window is active
31-
if (MainWindow.Content is not Frame rootFrame)
32-
{
33-
// Create a Frame to act as the navigation context and navigate to the first page
34-
rootFrame = new Frame();
35-
36-
// Place the frame in the current Window
37-
MainWindow.Content = rootFrame;
38-
39-
rootFrame.NavigationFailed += OnNavigationFailed;
40-
}
41-
42-
if (rootFrame.Content == null)
43-
{
44-
// When the navigation stack isn't restored navigate to the first page,
45-
// configuring the new page by passing required information as a navigation
46-
// parameter
47-
rootFrame.Navigate(typeof(MainPage), args.Arguments);
48-
}
59+
Host = await builder.NavigateAsync<Shell>();
60+
var localizer = (Uno.Extensions.Localization.ResourceLoaderStringLocalizer) Host.Services.GetRequiredService<IStringLocalizer>();
61+
string title = localizer["ApplicationName"];
62+
63+
MainWindow.Title = title;
64+
65+
//// Do not repeat app initialization when the Window already has content,
66+
//// just ensure that the window is active
67+
//if (MainWindow.Content is not Frame rootFrame)
68+
//{
69+
// // Create a Frame to act as the navigation context and navigate to the first page
70+
// rootFrame = new Frame();
71+
72+
// // Place the frame in the current Window
73+
// MainWindow.Content = rootFrame;
74+
75+
// rootFrame.NavigationFailed += OnNavigationFailed;
76+
//}
77+
78+
//if (rootFrame.Content == null)
79+
//{
80+
// // When the navigation stack isn't restored navigate to the first page,
81+
// // configuring the new page by passing required information as a navigation
82+
// // parameter
83+
// rootFrame.Navigate(typeof(Shell), args.Arguments);
84+
//}
4985

5086
MainWindow.SetWindowIcon();
5187
// Ensure the current window is active
5288
MainWindow.Activate();
5389
}
90+
91+
private static void RegisterRoutes(IViewRegistry views, IRouteRegistry routes)
92+
{
93+
views.Register
94+
(
95+
new ViewMap(ViewModel: typeof(ShellModel), View: typeof(Shell)),
96+
new ViewMap<MainPage, MainModel>(),
97+
new ViewMap<AboutPage, AboutModel>(),
98+
new ViewMap<NewTaskPage, NewTaskModel>(),
99+
new DataViewMap<SecondPage, SecondModel, Entity>()
100+
);
101+
102+
// 必须写注册哦,否则无法跳转
103+
routes.Register
104+
(
105+
new RouteMap("", View: views.FindByViewModel<ShellModel>(),
106+
Nested: new RouteMap[]
107+
{
108+
new RouteMap("Main", View: views.FindByViewModel<MainModel>()),
109+
new RouteMap("Second", View: views.FindByViewModel<SecondModel>()),
110+
new RouteMap("About", View: views.FindByViewModel<AboutModel>()),
111+
new RouteMap("NewTask",View: views.FindByViewModel<NewTaskModel>()),
112+
}
113+
)
114+
);
115+
}
54116

55117
/// <summary>
56118
/// Invoked when Navigation to a certain page fails

0 commit comments

Comments
 (0)