You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI问题:选择文件后Entry控件无法显示文件路径

.NET MAUI文件路径Entry字段不更新问题

我正在开发一个简易.NET MAUI应用学习相关技术:应用要求用户输入产品名称(string类型)、价格(decimal类型),并通过文件资源管理器选择图片(文件路径以string类型存储);随后将这些数据存入自定义的Sneaker类(仅包含上述三个字段),并在第二页展示给用户。

功能基本正常,但存在一个问题:用户选择文件后,XAML页面中对应的Entry字段始终为空,无法显示所选文件的路径——尽管路径已正确保存到对应字段,且图片能在第二页正常显示。


更新:已尝试的解决方案

将Sneaker类改为partial类,同时为filePath字段添加[ObservableProperty]特性(属性名改为驼峰命名,避免属性与字段名称冲突):

public partial class Sneaker: ObservableObject
{
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    [ObservableProperty]
    public string? filePath;
}

相关代码

用户输入XAML页面(Page1.xaml)

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiTestApplication.Page1"
             xmlns:viewmodel="clr-namespace:MauiTestApplication.ViewModels"
             x:DataType="viewmodel:SneakerViewModel"
             Title="Page1">

    <VerticalStackLayout
        Margin="30"
        Padding="30,0"
        Spacing="25">
        <Label 
            Text="Welcome to .NET MAUI! This is product page!"
            TextColor="{AppThemeBinding Dark=Black}"
            Style="{StaticResource Headline}"
            SemanticProperties.HeadingLevel="Level1"
            HorizontalOptions="Center" />
        <Entry Placeholder="Enter product name" Text="{Binding Sneaker.ProductName}" />
        <Entry Placeholder="Price USD" Text="{Binding Sneaker.Price}" Keyboard="Numeric" />
        <Grid ColumnSpacing="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Entry x:Name="FilePick" Grid.Column="0" Placeholder="File path" Text="{Binding Sneaker.FilePath}" />
            <Button Grid.Column="1" Text="Select a picture..." Command="{Binding PickFileCommand}" />
        </Grid>
        <Button Grid.Column="0" Grid.Row="1" Text="Add position" Command="{Binding AddSneakerCommand}" />
    </VerticalStackLayout>
</ContentPage>

SneakerViewModel.cs代码

public class SneakerViewModel: INotifyPropertyChanged
{
    private Sneaker _sneaker;
    public Sneaker Sneaker
    {
        get { return _sneaker; }
        set
        {
            _sneaker = value;
            OnPropertyChanged(nameof(Sneaker));
        }
    }
    public List<Sneaker> Sneakers { get; } = new List<Sneaker>();
    public ICommand AddSneakerCommand { get; }
    public ICommand PickFileCommand { get; private set; }

    private string _filePath;

    public string FilePath
    {
        get { return _filePath; }
        set
        {
            _filePath = value;
            Sneaker.FilePath = value;
            OnPropertyChanged(nameof(FilePath));
        }
    }

    private async Task PickFile() // 使用FilePicker避免手动输入文件路径
    {
        var result = await FilePicker.Default.PickAsync();
        if (result != null)
        {
            Sneaker.FilePath = result.FullPath;
            OnPropertyChanged(nameof(FilePath));
        }
    }

    public SneakerViewModel()
    {
        Sneaker = new Sneaker();
        AddSneakerCommand = new Command(AddSneaker);
        PickFileCommand = new Command(async () => await PickFile());
    }

    private void AddSneaker()
    {
        Sneakers.Add(Sneaker);
        Sneaker = new Sneaker(); // 添加到列表后重置Sneaker实例
        OnPropertyChanged(nameof(Sneaker)); // 通知UI更新绑定
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Page1.xaml.cs代码

public partial class Page1 : ContentPage
{
    public Page1()
    {
        InitializeComponent();
        BindingContext = ViewModelLocator.SneakerViewModel;
    }
}

ViewModelLocator.cs代码

public class ViewModelLocator
{
    private static SneakerViewModel _sneakerViewModel;

    public static SneakerViewModel SneakerViewModel
    {
        get
        {
            if (_sneakerViewModel == null)
            {
                _sneakerViewModel = new SneakerViewModel();
            }

            return _sneakerViewModel;
        }
    }
}

应用界面截图

用户输入页面


内容的提问来源于stack exchange,提问作者witchgen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 08:04:56