【问题标题】:Xamarin text binding from variable changing in separate class/namespaceXamarin 文本绑定来自单独类/命名空间中的变量更改
【发布时间】:2022-11-25 02:04:46
【问题描述】:

我正在为我的 WPF 应用程序中的文本绑定而苦苦挣扎。

  1. 假设我有另一个工作应用程序(例如 Windows 服务),其中包含一些数据。
  2. 在我的 WPF 应用程序中,我希望文件夹“DATA”包含引入数据的类,并在同一文件夹中包含另一个类,该类将包含一个空值,它将查询我的 Windows 服务
  3. 我想在我的 WPF 窗口中显示此数据。

    为了使它更简单 - 一个带有数据的类,一个带有数据更改的类和显示此数据的 WPF 窗口。

    不幸的是我无法实现这一点......当我执行下面的代码时,我的窗口显示 0 而不是 123。

    我想实现我的窗口将显示值 123。

    1. 项目“示例”中“数据”文件夹中的文件“Database.cs”
      namespace example.Data
      {
          public class Database
          {
              private int _testInt = 0;
      
              public int testInt
              { 
                  get { return _testInt; } 
                  set { _testInt = value; } 
              }
          }
      }
      
      1. 项目“示例”中“数据”文件夹中的文件“Query.cs”
      namespace example.Data
      {
          public class Query
          {
              public Database _database;
              public void execute()
              {
                 _database = new Database();
                 _database.testInt = 123;
              }
          }
      }
      
      1. 项目“示例”中的文件“MainWindow.xaml.cs”
      namespace example
      {
          public partial class MainWindow : Window
          {
              public Data.Database _database;
              public Data.Query _query;
      
              public int testInt
              {
                  get { return _database.testInt; }
                  set { _database.testInt = value; OnPropertyChanged(); }
              }
              public MainWindow()
              {
                  InitializeComponent();
                  DataContext = this;
                  _database = new Data.Database();
                  _query = new Data.Query();
      
                  _query.execute();
              }
      
              #region INotifyPropertyChanged Members
      
              public event PropertyChangedEventHandler PropertyChanged;
      
              protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
              {
                  PropertyChangedEventHandler handler = this.PropertyChanged;
                  if (handler != null)
                  {
                      var e = new PropertyChangedEventArgs(propertyName);
                      handler(this, e);
                  }
              }
              #endregion
          }
      }
      
      1. 文件 MainWindow.xaml
      <Window>
      <TextBlock Text="{Binding testInt}"
                             Foreground="White"
                             FontSize="15"
                             VerticalAlignment="Top"
                             HorizontalAlignment="Left"
                             Margin="20,10,10,0" />
      </Window>
      

      附言如果我将

      _database.testInt = 987;
      

      对于 MainWindow.xaml.cs,它工作正常 - 窗口在文本块中显示值 987。

【问题讨论】:

  • 这似乎是基本的 WPF,而不是 Xamarin

标签: c# wpf


【解决方案1】:

你需要实施INotifyPropertyChanged

public partial class MainWindow : Window, INotifyPropertyChanged

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多