【问题标题】:UWP: How to bind a Textblock to a .txt file?UWP:如何将文本块绑定到 .txt 文件?
【发布时间】:2022-01-11 16:53:17
【问题描述】:

文件“sample.txt”位于 Assets 文件夹中,其中包含一行文本“hello, world”。 我想将其内容绑定到 Textblock 控件。

【问题讨论】:

    标签: c# data-binding uwp


    【解决方案1】:

    TextBlockText属性设置为文件的内容:

    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);
            };
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用File.ReadAllText 从文件中提取文本并将其分配给实现INotifyPropertyChanged 的类的属性。将此属性绑定到TextBlock.Text 属性。

      您的视图模型:

      public class MainPageViewModel : INotifyPropertyChanged
      {
          private string text;
      
          public event PropertyChangedEventHandler PropertyChanged;
      
          public string Text
          {
              get => this.text;
              set
              {
                  this.text = value;
                  this.OnPropertyChanged(nameof(this.Text))
              }
          }
      
          protected void OnPropertyChanged([CallerMemberName] string name = null)
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
          }
      }
      

      你的看法:

      public sealed partial class MainPage : Page
      {
          public MainPage()
          {
              this.InitializeComponent();
              
              this.Loaded += async (s, e) =>
              {
                  var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                  var file = await folder.GetFileAsync(@"Assets\sample.txt");
      
                  this.DataContext = new MainPageViewModel
                  {
                      Text = await File.ReadAllTextAsync(file.Path),
                  };
              };
          }
      }
      

      在 MainPage XAML 中:

      <TextBlock Text="{x:Bind Text}"
      

      要检测 txt 文件中的更改,请使用 FileSystemWatcher

      【讨论】:

      • 嗨,我已经有了读取 .txt 文件的代码。所以我做了你建议的下一步:创建一个实现 INotifyPropertyChanged 的​​类。但是不知道怎么把从txt文件中得到的值赋给实现INotifyPropertyChanged的类中的属性。我正在研究如何做到这一点。
      • 读取.txt文件的代码 private async void HDUsedSpace() { string fname = @"Assets\Space_C.txt"; StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFile 文件 = 等待 InstallationFolder.GetFileAsync(fname); if (File.Exists(file.Path)) { DriveC.Text = File.ReadAllText(fname); } }
      • 类,我不知道是否正确 public class HD_Space : INotifyPropertyChanged { // 这些字段保存公共属性的值。私有字符串 HD_UsedSpaceValue = String.Empty;公共事件 PropertyChangedEventHandler PropertyChanged; // 此方法由每个属性的 Set 访问器调用。 // 应用于可选 propertyName 参数的 CallerMemberName 属性 // 导致调用者的属性名称被替换为参数。
      • private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } // 构造函数是私有的以强制执行工厂模式。私有 HD_Space() { HD_UsedSpaceValue = "测试"; } // 这是公共工厂方法。公共静态 HD_Space CreateNewHD_Space() { return new HD_Space(); }
      • 公共字符串 HD_UsedSpace { get { return this.HD_UsedSpaceValue; } set { if (value != this.HD_UsedSpaceValue) { this.HD_UsedSpaceValue = value; NotifyPropertyChanged(); } } } }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 2021-09-11
      • 1970-01-01
      相关资源
      最近更新 更多