我在 Blazor Hybrid WPF 中使用了这个 Blazor 组件:
@using DialogResult = System.Windows.Forms.DialogResult
@using System.Windows.Forms
<MudText Typo="Typo.caption">@Label</MudText>
<MudPaper Style="display: flex; padding: 10px; margin: 5px 0 20px 0" Elevation="3">
<StringTextField
BoundValue="@BoundValue"
ValidationAttribute="ValidationAttribute"
HelperText="@HelperText"
HelperTextOnFocus="true"
FieldInputType="InputType.Text"
OnValueChanged="OnValueChanged"
Label="@string.Empty"/>
<MudIconButton Icon="@Icons.Material.Filled.Folder" OnClick="SelectFolder" Style="width: 10%"/>
</MudPaper>
@code {
[Parameter]
public EventCallback<string> OnValueChanged { get; set; }
[Parameter]
public string Label { get; set; }
[Parameter]
public string BoundValue { get; set; }
[Parameter]
public string HelperText { get; set; }
[Parameter]
public object? ValidationAttribute { get; set; }
private async Task SelectFolder()
{
var folderPath = await Dispatcher.CreateDefault().InvokeAsync(() =>
{
using var dialog = new FolderBrowserDialog
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
};
var result = dialog.ShowDialog();
var path = result == DialogResult.OK ? dialog.SelectedPath : string.Empty;
return path;
});
BoundValue = folderPath;
await OnValueChanged.InvokeAsync(BoundValue);
StateHasChanged();
}
}
并且有时在选择文件夹时会崩溃(文件选择器也是如此)。 Try-catch 没有帮助,应用程序会默默关闭。有什么想法吗?
这里也有同样的问题。尝试了所有可能的调度员组合。没有任何效果。