我正在构建一个只为Windows的应用程序,这需要使用.Net毛伊岛消费一个docx文件.

我使用了建议的类IFilePicker,实现了它,并且在调试时工作得很好(在调试和发布模式下都是如此).

因此,在完成一个预览版之后,我希望使用以下工具"像便携版本一样"部署未打包的版本:

MSBuild.exe D:\Workspace\dotNet\WordReplacer\WordReplacer.App\ /restore /t:Publish /p:TargetFramework=net6.0-windows10.0.19041 /p:configuration=release /p:WindowsAppSDKSelfContained=true /p:Platform=x64 /p:PublishSingleFile=true /p:WindowsPackageType=None /p:RuntimeIdentifier=win10-x64

除了FilePicker不工作并给出错误之外,一切都像调试一样工作得很好:

值不在预期范围内

如果我安装的是带有证书的已发布包,则不会发生此错误.因此,也许我在生成解压应用程序的msBuilder解决方案中遗漏了一些东西.

我使用的是社区工具包.MVVM,我用来挑选文件的方法保留在我的视图模型中:

        private string _inputFilePath;
        
        [ObservableProperty]
        private string _inputFileNameText = "Select a input file";
        
        [RelayCommand]
        public async Task PickInputDocAsync()
        {
            try
            {
                var customFileType = new FilePickerFileType(
                    new Dictionary<DevicePlatform, IEnumerable<string>>
                    {
                        { DevicePlatform.WinUI, new[] { ".doc", ".docx" } },
                    });

                PickOptions options = new()
                {
                    PickerTitle = "Please select a document",
                    FileTypes = customFileType,
                };

                var result = await FilePicker.Default.PickAsync(options).ConfigureAwait(false);

                if (result != null)
                {
                    _inputFilePath = result.FullPath;
                    InputFileNameText = result.FileName;
                }

            }
            catch (Exception ex)
            {
                ErrorMessage = $"{ex.InnerException?.Message} Error: {ex.Message}, InputFilePath: {_inputFilePath}, InputFileName: {InputFileNameText}";
            }

有什么办法修好它吗?

推荐答案

我一直在try 修复这个错误,但由于我对msbuild本身了解不多,所以我采取了另一种方法.

所以,当我实现了一个专门用于Windows平台的FilePicker时,我终于让它工作了.

我采用了答案Folder Picker .NET MAUI中的大部分代码,但是,我使用的不是FolderPicker,而是FilePicker.查看它以了解有关实施和设置的更多信息.

How I do it:

在根文件夹应用程序中创建文件夹帮助器,并创建接口ICustomPicker和数据传输对象(DTO).

public interface ICustomPicker
{
    Task<FileDto> PickFileAsync();
}
public class FileDto
{
    public string DisplayName { get; set; }
    public string Name { get; set; }
    public string FullPath { get; set; }
}

在PlataForms/Windows文件夹中,我创建了CustomFilePicker.cs

public class CustomPicker : ICustomPicker
{
    
     public async Task<FileDto> PickFileAsync()
        {
            try
            {
                var picker = new WindowsFilePicker();

                picker.FileTypeFilter.Add(".docx");
                picker.FileTypeFilter.Add(".doc");

                // Get the current window's HWND by passing in the Window object
                var windowHandle = ((MauiWinUIWindow)App.Current.Windows[0].Handler.PlatformView).WindowHandle;

                // Associate the HWND - window handler, with the file picker
                WinRT.Interop.InitializeWithWindow.Initialize(picker, windowHandle);

                var storageFile = await picker.PickSingleFileAsync();

                if (storageFile != null)
                {
                    return new FileDto()
                    {
                        DisplayName = storageFile.DisplayName,
                        Name = storageFile.Name,
                        FullPath = storageFile.Path
                    };
                }
            }
            catch
            {
                // Ignored
            }
            return null;

        }
}

在My MauiProgram.cs中注册DI

#if WINDOWS
            builder.Services.AddTransient<ICustomPicker, Platforms.Windows.CustomPicker>();
#endif

然后在我的视图模型中,我简单地将其称为


private readonly ICustomPicker _customPicker;
public MainViewModel(ICustomPicker customPicker,)
{        
    _customPicker = customPicker;         
}

[RelayCommand]
public async Task PickInputDocAsync(){

    var file = await _customPicker.PickFileAsync().ConfigureAwait(false);

    if (file != null)
    {
        //do something
    }
}

当毛伊岛发布一个合适的解包发布选项时,这个问题可能会消失(我希望如此).

注意:现在,我只是在Windows 11中测试这种方法.

Csharp相关问答推荐

如何使嵌套for-loop更高效?

以自动方式注销Azure身份应用程序

如何在Reflection. Emit中使用具有运行时定义的类型参数的泛型类型

编写DataAnnotations自定义验证器的多种方法

一种安全的方式来存储SSH凭证(MAUI/C#应用程序)

始终保留数组中的最后N个值,丢弃最老的

Quartz调度程序不调用作业(job)类

Cosmos SDK和Newtonsoft对静态只读记录的可能Mutations

在.NET 7.0 API控制器项目中通过继承和显式调用基类使用依赖项注入

什么类型的对象存储在大对象堆(LOH)中

在C#中过滤Excel文件

多个选项卡上的MudForm验证

岛屿和框架中的自定义控件库.Navigate-AccessViolationException

如何在Polly重试策略成功之前将HttpClient请求排队?

Xamarin.Forms-如何创建可 Select 的显示alert 或弹出窗口?

使用C#代码和SQL SERVER中的相同证书签名会产生不同的结果

在.Net 8 Visual Studio 2022中启用本机AOT发布时发布失败

外部应用&&的LINQ;左外部连接&类似于PostgreSQL的查询

我想我必须手动使用res1(字符串形式的PowerShell哈希表)

我是否以错误的方式使用了异步延迟初始化?