当我在文件资源管理器中双击一个.txt文件时,会启动记事本,并在那里自动打开该文件.我希望能够使用.NET Maui应用程序重新创建这种行为-我希望我的应用程序在我双击具有特定扩展名的文件时启动,我希望我的应用程序自动打开该文件,以便我可以立即开始使用它,就像处理记事本和.txt文件一样.

我试着在.NET Maui文档和Stack Overflow上四处寻找,但我能得到的最接近的结果是手动打开一个已经在运行的应用程序的文件.

The question here mentions setting the app as default on Windows but this fact is stated as a given, while I don't know how I could go about doing this. Furthermore, if it really is necessary to set something in the system settings, I would like to make it so that my app's installer would take care of it. How can I do that? Here's the question I mentioned:
How to use my app to open a file when I double-click it?

The question here says something about command line arguments, but it doesn't answer the question of how a file can be opened by double-clicking, and it also talks about frameworks other than .NET MAUI:
How can I open a file in my WPF app, when the user double-clicks on that file?

推荐答案

try 在.NET毛伊岛应用程序的项目 list 中声明文件类型扩展名和文件类型关联.打开MauiProject.csproj文件,并在元素中添加以下代码:

<ItemGroup>
  <AppExtension Include=".YOURCUSTOMFILEEXTENSION">
    <ContentType>Text</ContentType>
    <RequestAccess>true</RequestAccess>
  </AppExtension>
</ItemGroup>

然后try 在MauiProgram.cs文件中添加一个文件激活事件处理程序.

        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .ConfigureLifecycleEvents(lifecycle =>
                {
                    lifecycle.AddWindows<FileActivatedHandler>((_, args) =>
                    {
                        // Handle the file activation event
                        if (args.Files?.Count > 0)
                        {
                            var file = args.Files[0];
                            var filePath = file.Path;
                            // Process the file as needed
                            // For example, open the file in your app's editor
                            OpenFile(filePath);
                        }
                    });
                })
                .ConfigureMauiHandlers(handlers =>
                {
                    // Register your app's handlers here
                })
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                });

            return builder.Build();
        }

        private static void OpenFile(string filePath)
        {
            // Open the file in your app's editor
            // You can use the filePath to access the file contents and work with it
            // For example:
            // var content = File.ReadAllText(filePath);
            // Perform any necessary operations on the file content

            //await Launcher.OpenAsync(new OpenFileRequest
            //{
            //    File = new ReadOnlyFile(filePath)
            //});
        }

Csharp相关问答推荐

禁用AutoSuggestBox项目更改时的动画?

总是丢弃返回的任务和使方法puc无效之间有区别吗?

下拉图片点击MudBlazor

在. NET Core 8 Web API中,当为服务总线使用通用消费者时,如何防止IServiceProvider被释放或空?"

"virtual"修饰符对接口成员有什么影响?

迭代C#List并在数据库中 for each 元素执行SELECT语句—更有效的方式?

如果存在对CodeAnalysis.CSharp的引用,则不能引用netStandard2.0库

C#XmlSerializer-输出控制新行的多个XML片段

C#-从基类更新子类

为具有实体框架后端的Reaction项目 Select 正确的Visual Studio模板

C#和ASP.NET核心标识:提高GetUserAsync调用的性能

确定System.Text.Json序列化中是否无法识别Type

TagHelpers在新区域不起作用

如何将FindAll()与Nuget包Interop.UIAutomationClient一起使用

在C#ASP.NET内核中使用INT AS-1进行控制器场景的单元测试

使用Blazor WebAssembly提高初始页面加载时间的性能

如何在ASP.NET Core 8中获取键控服务词典

在C#/ASP.NET Core 7中,什么可能导致POST请求作为GET请求发送

使用DI实例化带有动态参数的服务?

我可以阻止类型上的Object.ToString()吗?