.net 为什么文件路径不显示在文本字段后FilePicker拿起文件?(剃刀MAUI)

py49o6xq  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(106)

我在.razor中有如下代码。在我拿起文件后,输入字段不会立即显示路径。但如果我再次单击按钮,路径会突然显示。我如何更改代码?

<div>

    <label for="inputdata">Input Data:</label>
    <input type="text" name="inputdata" value="@filePath" />
    <button @onclick="OpenFileAsync">Open</button>

</div>

@code {

    private string filePath;

    public async void OpenFileAsync()
    {
        var customFileType = new FilePickerFileType(
            new Dictionary<DevicePlatform, IEnumerable<string>>
                {
                { DevicePlatform.WinUI, new[] { "jpg" } },
                }
        );

        PickOptions options = new()
        {
            PickerTitle = "Please select a comic file",
            FileTypes = customFileType,
        };
        var result = await FilePicker.Default.PickAsync(options);
        if (result != null)
        {
            filePath = result.FullPath;
        }
    }
}

字符串

yduiuuwa

yduiuuwa1#

您可以使用StateHasChanged方法通知组件其状态已更改。
只需在文件路径更改后添加这段代码,

public async void OpenFileAsync()
{
    ...
    var result = await FilePicker.Default.PickAsync(options);
    if (result != null)
    {
        filePath = result.FullPath;
    }
    // add this code
    StateHasChanged();
}

字符串
希望有帮助!

相关问题