【发布时间】:2018-10-07 03:36:45
【问题描述】:
这是我现在拥有的一个非常简化的示例:
public static class Settings
{
public static TH th;
}
public partial class PhrasesFrame
{
private void SetC1Btn()
{
var a = (int)Settings.th;
vm.C1BtnLabelTextColor = phrase.C1 == true ?
Styles.A[(int)Settings.th] :
Styles.A[(int)Settings.th];
}
我想将其替换为:
public partial class PhrasesFrame
{
// The value of Settings.th can change at any time
// I want the value of id to change when the
// value of (int)Setting.th changes. The way
// it's coded now I realize it's just a one
// time assignment
var id = (int)Settings.th;
private void SetC1Btn()
{
var a = (int)Settings.th;
vm.C1BtnLabelTextColor = phrase.C1 == true ?
Styles.A[id] :
Styles.A[id];
}
【问题讨论】:
-
public int Id; public function PhrasesFrame() { Id = (int) Settings.th; }? -
如果
Settings(类?)可以实现INotifyPropertyChange,PhrasesFrame可以订阅它并在 ACK 时更改a的值。 -
@FrankerZ - 抱歉,我不了解那里发生的事情。您正在创建一个函数,但我将在哪里使用它? PhrasesFrame 类是一次性创建的。在初始创建之后,Id 会不断更改以具有 (int) Settings.th 的值吗?
-
啊,不。我没有注意到
Settings是static。无法在此处实现接口。我会看看我能不能想出一些可行的办法。 -
实现 INotifyPropertyChanged 或添加执行相同操作的自定义事件处理程序。这就是 WPF 和数据绑定的亮点。