简体   繁体   English

使用数据绑定在文本框中显示文本文件数据

[英]Show text file data in textbox with Data binding

I am trying to read the text file in textbox with the help of data binding. 我正在尝试借助数据绑定读取文本框中的文本文件。

Here is the code of class where i used get and set properties 这是我使用get和set属性的类代码

public class FileData : INotifyPropertyChanged
{
    public string data;
    public string Data
    {
        get { return data; }
        set
        {
            data = value;
            OnPropertyChanged();
        }
    }

    public FileData(string data)
    {
        Data = data;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string caller = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
        }
    }
}

} }

And this is the code of Mainpage.Xaml.cs 这是Mainpage.Xaml.cs的代码

     private async void Button_Click_1(object sender, RoutedEventArgs e)
    {

        display.DataContext = fd;
        StorageFile file = await      ApplicationData.Current.LocalFolder.GetFileAsync(filenamebox.Text + ".txt");
        fd.Data = await FileIO.ReadTextAsync(file);
    }

And when i read the file this exception comes out 当我读取文件时,出现此异常

"Object reference not set to an instance of an object." “你调用的对象是空的。”

please help me :( 请帮我 :(

Check if file return is null or not 检查文件返回是否为空

StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filenamebox.Text + ".txt");
if ( file != null)
{
     // Do what you want
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM