TextBlock 应该有来自代码的输入 (xaml, c#)
TextBlock should have an input from code (xaml, c#)
在 .XAML 我写了
<TextBlock> x words found </TextBlock>
在我的 .cs 中我写了这样的东西:
public int number(A a, B b) {
[…]
return resultsCount;
}
在这种情况下,我希望 x 是 resultsCount。
我如何 link 使 x 成为结果数?
最简单的解决方案:
您可以这样设置 TextBlock
:
<TextBlock >
<Run x:Name="MyRun" Text="0"/> // place for the 'X' of your code
<Run Text="words found"/>
</TextBlock>
并且在代码隐藏中,您可以像这样在 int number(A a, B b)
方法中更改 MyRun 的 Text
:
public int number(A a, B b)
{
[…]
MyRun.Text = resultsCount.ToString();
return resultsCount;
}
涉及数据绑定的解决方案:
在这种情况下,您应该定义一个 属性 引发 PropertyChanged
事件,如下所示:
public class CodeBehind : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string simpleString;
public string SimpleString
{
get{ return simpleString; }
set
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SimpleString"));
simpleString = value;
}
}
public int number(A a, B b)
{
[…]
return resultsCount;
}
}
然后只需将 MyRun
的 Text
属性 与此 SimpleString
属性:
绑定
<TextBlock >
<Run x:Name="MyRun" Text="0"/>
<Run Text="words found"/>
</TextBlock>
每当您需要更新 "X" 时,在后面的代码中,只需执行以下操作:
SimpleString = number(a,b).ToString();
您可以使用 binding
机制来实现。
为此,您需要创建一个 ViewModel
,它实现了 INotifyPropertyChanged
接口。
一个例子:
namespace TestBind
{
public class ExampleModel : INotifyPropertyChanged
{
private string counter;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public ExampleModel()
{
this.Counter = "0 words found";
}
public string Counter
{
get { return this.counter; }
set
{
this.counter = value;
this.OnPropertyChanged();
}
}
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
拥有模型后,将其连接到您的页面:
<Page.DataContext>
<local:ExampleModel/>
</Page.DataContext>
在您的 TextBlock
中,您现在可以将内容绑定到 Counter
属性:
<TextBlock Text="{Binding Path=Counter}"/>
在您的 Page.xaml.cs 内部,您应该使用视图模型创建一个成员参数:
private ExampleModel exampleModel;
public MainPage()
{
this.InitializeComponent();
// Do stuff
exampleModel = this.DataContext as ExampleModel;
}
现在您可以通过调用更新文本:
exampleModel.Counter = "100 words found";
有关绑定的详细说明,请阅读 official documentation。
在 .XAML 我写了
<TextBlock> x words found </TextBlock>
在我的 .cs 中我写了这样的东西:
public int number(A a, B b) {
[…]
return resultsCount;
}
在这种情况下,我希望 x 是 resultsCount。 我如何 link 使 x 成为结果数?
最简单的解决方案:
您可以这样设置 TextBlock
:
<TextBlock >
<Run x:Name="MyRun" Text="0"/> // place for the 'X' of your code
<Run Text="words found"/>
</TextBlock>
并且在代码隐藏中,您可以像这样在 int number(A a, B b)
方法中更改 MyRun 的 Text
:
public int number(A a, B b)
{
[…]
MyRun.Text = resultsCount.ToString();
return resultsCount;
}
涉及数据绑定的解决方案:
在这种情况下,您应该定义一个 属性 引发 PropertyChanged
事件,如下所示:
public class CodeBehind : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string simpleString;
public string SimpleString
{
get{ return simpleString; }
set
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SimpleString"));
simpleString = value;
}
}
public int number(A a, B b)
{
[…]
return resultsCount;
}
}
然后只需将 MyRun
的 Text
属性 与此 SimpleString
属性:
<TextBlock >
<Run x:Name="MyRun" Text="0"/>
<Run Text="words found"/>
</TextBlock>
每当您需要更新 "X" 时,在后面的代码中,只需执行以下操作:
SimpleString = number(a,b).ToString();
您可以使用 binding
机制来实现。
为此,您需要创建一个 ViewModel
,它实现了 INotifyPropertyChanged
接口。
一个例子:
namespace TestBind
{
public class ExampleModel : INotifyPropertyChanged
{
private string counter;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public ExampleModel()
{
this.Counter = "0 words found";
}
public string Counter
{
get { return this.counter; }
set
{
this.counter = value;
this.OnPropertyChanged();
}
}
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
拥有模型后,将其连接到您的页面:
<Page.DataContext>
<local:ExampleModel/>
</Page.DataContext>
在您的 TextBlock
中,您现在可以将内容绑定到 Counter
属性:
<TextBlock Text="{Binding Path=Counter}"/>
在您的 Page.xaml.cs 内部,您应该使用视图模型创建一个成员参数:
private ExampleModel exampleModel;
public MainPage()
{
this.InitializeComponent();
// Do stuff
exampleModel = this.DataContext as ExampleModel;
}
现在您可以通过调用更新文本:
exampleModel.Counter = "100 words found";
有关绑定的详细说明,请阅读 official documentation。