The file "sample.txt" is in the Assets folder, and contains a single line with the text "hello, world". I would like to bind its content to a Textblock control.
CodePudding user response:
Set the Text property of the TextBlock to the contents of the file:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.Loaded = async (s, e) =>
{
const string Filename = @"Assets\sample.txt";
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync(Filename);
textBlock1.Text = await File.ReadAllTextAsync(file.Path);
};
}
}
CodePudding user response:
Extract the text from the file using File.ReadAllText and assign it to a property on a class that implements INotifyPropertyChanged. Bind this property to the TextBlock.Text property. To detect changes in the txt-file, use FileSystemWatcher.
