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

WinUI中x:Bind绑定Model属性后UI不更新问题求助

WinUI异步加载数据后UI绑定不更新问题

首次使用WinUI,已成功获取所需数据,但异步函数执行后,绑定到UI的数据无法更新控件。通过ViewModel处理流加载用户选中文件的字符串,并将List<string>存储到实现了INotifyPropertyChanged的Model对象中,但View未触发PropertyChanged事件,对x:Bind与INotifyPropertyChanged的关系理解存在困惑。

Model代码

public class PCCBill : INotifyPropertyChanged
{
    private string importFileName;
    private List<string> lines;
    private string originalFileText;
    //private List<string> formattedLines;

    public PCCBill(string displayFileName)
    {
        this.ImportFileName = displayFileName;
    }

    public string ImportFileName
    {
        get { return importFileName; }
        set
        {
            this.importFileName = value;
            RaisePropertyChanged(nameof(ImportFileName));
        }
    }

    public List<string> Lines
    {
        get { return lines; }
        set
        {
            this.lines = value;
            string txt = "";
            foreach (string line in this.lines)
            {
                txt += line + Environment.NewLine;
            }
            this.OriginalFileText = txt;
        }
    }

    public string OriginalFileText
    {
        get { return originalFileText;  }
        set
        {
            this.originalFileText = value;
            RaisePropertyChanged(nameof(OriginalFileText));
        }
    }
    /*public string FormattedFileText
    {
        get
        {
            string txt = "";
            foreach(string line in this.formattedLines)
            {
                txt += line + Environment.NewLine;
            }
            return txt;
        }
    }*/


    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

View中的x:Bind代码

<TextBlock Grid.Row="1" x:Name="FileNameTxtBlock" VerticalAlignment="Center"
           Margin="15,0" Text="{x:Bind ViewModel.Bill.ImportFileName, Mode=OneWay}"/>
<TextBlock x:Name="OrgPreviewTxtBlock" Text="{x:Bind ViewModel.Bill.OriginalFileText, Mode=OneWay}"/>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 13:36:14