Skip to content

Commit

Permalink
完成主线程调用
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Dec 19, 2023
1 parent 718de52 commit 2fc43a8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
@@ -1,3 +1,7 @@
using Microsoft.Extensions.DependencyInjection;

using UnoFileDownloader.Utils;

namespace UnoFileDownloader
{
public class App : Application
Expand Down Expand Up @@ -68,6 +72,10 @@ protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
// TODO: Register your services
//services.AddSingleton<IMyService, MyService>();
services.AddSingleton<IDispatcherQueueProvider>(c =>
{
return new DispatcherQueueProvider(MainWindow!.DispatcherQueue);
});
})
.UseNavigation(ReactiveViewModelMappings.ViewModelMappings, RegisterRoutes)
);
Expand Down
Expand Up @@ -6,9 +6,11 @@

using Microsoft.UI.Dispatching;

using UnoFileDownloader.Utils;

namespace UnoFileDownloader.Presentation
{
public partial record AboutModel(INavigator Navigator)
public partial record AboutModel(INavigator Navigator, IDispatcherQueueProvider DispatcherQueueProvider)
{
public void CloseAbout()
{
Expand All @@ -17,12 +19,12 @@ public void CloseAbout()

public void GotoGitHub()
{
var d = Dispatcher;

_ = Windows.System.Launcher.LaunchUriAsync(new Uri("https://github.com/dotnet-campus/dotnetCampus.FileDownloader"));
Dispatcher.TryEnqueue(() =>
{
_ = Windows.System.Launcher.LaunchUriAsync(new Uri("https://github.com/dotnet-campus/dotnetCampus.FileDownloader"));
});
}

// 这是没有用的,返回的是空
protected DispatcherQueue Dispatcher { get; } = DispatcherQueue.GetForCurrentThread();
protected DispatcherQueue Dispatcher { get; } = DispatcherQueueProvider.Dispatcher;
}
}
@@ -0,0 +1,13 @@
using Microsoft.UI.Dispatching;

namespace UnoFileDownloader.Utils
{
public record DispatcherQueueProvider(DispatcherQueue Dispatcher) : IDispatcherQueueProvider
{
}

public interface IDispatcherQueueProvider
{
Microsoft.UI.Dispatching.DispatcherQueue Dispatcher { get; }
}
}

0 comments on commit 2fc43a8

Please sign in to comment.