静态 属性 未在 UI 内更新
Static property not updating in UI
我花了最后一个小时试图在 google 和 Whosebug 中找到答案。我遵循了不同的意见和建议,但到目前为止没有任何效果。我当前的代码如下所示:
public class GlobalManager : ViewModelBase
{
static object _LockObject_GFS = new object();
static double _GlobalFontSize;
public static double GlobalFontSize
{
get
{
lock (_LockObject_GFS)
{
_GlobalFontSize = GetGlobalResource<double>(LambdaHelper.MemberToString(() => GlobalFontSize));
return _GlobalFontSize;
}
}
set
{
lock (_LockObject_GFS)
{
if (_GlobalFontSize != value)
{
_GlobalFontSize = value;
SetGlobalResource(value, LambdaHelper.MemberToString(() => GlobalFontSize));
NotifyStaticPropertyChanged(() => GlobalFontSize);
}
}
}
}
}
getter & setter 都被调用了。 NotifyStaticPropertyChanged
有效,但我的 UI 没有更新。我添加了一个 TextBlock
来检查它是否更新。显然不是。
<TextBlock Text="{Binding Path=(global:GlobalManager.GlobalFontSize), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
如果我在我的 VM(当前 DataContext)中定义一个 属性,并将其绑定到一个 TextBlock
,它会使用当前值正确更新。
目前 Slider
的 DependencyProperty
Value
绑定到此 属性 以更新字体大小。 (IsSnapToTickEnabled="True"
)
public double GlobalFontSize
{
get { return GlobalManager.GlobalFontSize; }
set { GlobalManager.GlobalFontSize = value; NotifyPropertyChanged(() => GlobalFontSize); }
}
如何使绑定与静态 属性 一起正常工作? StaticPropertyChanged
事件不为空。
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
编辑 1:
public static void NotifyStaticPropertyChanged(string propertyName)
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
public static void NotifyStaticPropertyChanged<T>(Expression<Func<T> > property)
{
var expr = property.Body as MemberExpression;
if (expr == null)
throw new ArgumentException("Lambda does not contain member expression. () => MyClassOrObject.Property");
NotifyStaticPropertyChanged(expr.Member.Name);
}
确保您的 GetGlobalResource
和 SetGlobalResource
方法按预期工作并且您的事件签名正确。
您可以参考下面的工作示例实现并将其与您的进行比较:
public class GlobalManager
{
static object _LockObject_GFS = new object();
static double _GlobalFontSize;
public static double GlobalFontSize
{
get
{
lock (_LockObject_GFS)
{
return _GlobalFontSize;
}
}
set
{
lock (_LockObject_GFS)
{
if (_GlobalFontSize != value)
{
_GlobalFontSize = value;
NotifyStaticPropertyChanged(()=> GlobalFontSize);
}
}
}
}
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
public static void NotifyStaticPropertyChanged(string propertyName)
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
public static void NotifyStaticPropertyChanged<T>(Expression<Func<T>> property)
{
var expr = property.Body as MemberExpression;
if (expr == null)
throw new ArgumentException("Lambda does not contain member expression. () => MyClassOrObject.Property");
NotifyStaticPropertyChanged(expr.Member.Name);
}
}
编辑: 但是,如果事件是在基 class 中定义的,则它不起作用。
public abstract class MyBaseViewModel
{
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
public static void NotifyStaticPropertyChanged(string propertyName)
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
public static void NotifyStaticPropertyChanged<T>(Expression<Func<T>> property)
{
var expr = property.Body as MemberExpression;
if (expr == null)
throw new ArgumentException("Lambda does not contain member expression. () => MyClassOrObject.Property");
NotifyStaticPropertyChanged(expr.Member.Name);
}
}
public class GlobalManager : MyBaseViewModel
{
static object _LockObject_GFS = new object();
static double _GlobalFontSize = 10.0;
public static double GlobalFontSize
{
get
{
lock (_LockObject_GFS)
{
return _GlobalFontSize;
}
}
set
{
lock (_LockObject_GFS)
{
if (_GlobalFontSize != value)
{
_GlobalFontSize = value;
NotifyStaticPropertyChanged("GlobalFontSize");
}
}
}
}
}
StaticPropertyChangedEvent
必须在 属性 所在的相同 class 中定义才能更新绑定:
View is not getting notified when value of static Property Changes
我花了最后一个小时试图在 google 和 Whosebug 中找到答案。我遵循了不同的意见和建议,但到目前为止没有任何效果。我当前的代码如下所示:
public class GlobalManager : ViewModelBase
{
static object _LockObject_GFS = new object();
static double _GlobalFontSize;
public static double GlobalFontSize
{
get
{
lock (_LockObject_GFS)
{
_GlobalFontSize = GetGlobalResource<double>(LambdaHelper.MemberToString(() => GlobalFontSize));
return _GlobalFontSize;
}
}
set
{
lock (_LockObject_GFS)
{
if (_GlobalFontSize != value)
{
_GlobalFontSize = value;
SetGlobalResource(value, LambdaHelper.MemberToString(() => GlobalFontSize));
NotifyStaticPropertyChanged(() => GlobalFontSize);
}
}
}
}
}
getter & setter 都被调用了。 NotifyStaticPropertyChanged
有效,但我的 UI 没有更新。我添加了一个 TextBlock
来检查它是否更新。显然不是。
<TextBlock Text="{Binding Path=(global:GlobalManager.GlobalFontSize), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
如果我在我的 VM(当前 DataContext)中定义一个 属性,并将其绑定到一个 TextBlock
,它会使用当前值正确更新。
目前 Slider
的 DependencyProperty
Value
绑定到此 属性 以更新字体大小。 (IsSnapToTickEnabled="True"
)
public double GlobalFontSize
{
get { return GlobalManager.GlobalFontSize; }
set { GlobalManager.GlobalFontSize = value; NotifyPropertyChanged(() => GlobalFontSize); }
}
如何使绑定与静态 属性 一起正常工作? StaticPropertyChanged
事件不为空。
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
编辑 1:
public static void NotifyStaticPropertyChanged(string propertyName)
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
public static void NotifyStaticPropertyChanged<T>(Expression<Func<T> > property)
{
var expr = property.Body as MemberExpression;
if (expr == null)
throw new ArgumentException("Lambda does not contain member expression. () => MyClassOrObject.Property");
NotifyStaticPropertyChanged(expr.Member.Name);
}
确保您的 GetGlobalResource
和 SetGlobalResource
方法按预期工作并且您的事件签名正确。
您可以参考下面的工作示例实现并将其与您的进行比较:
public class GlobalManager
{
static object _LockObject_GFS = new object();
static double _GlobalFontSize;
public static double GlobalFontSize
{
get
{
lock (_LockObject_GFS)
{
return _GlobalFontSize;
}
}
set
{
lock (_LockObject_GFS)
{
if (_GlobalFontSize != value)
{
_GlobalFontSize = value;
NotifyStaticPropertyChanged(()=> GlobalFontSize);
}
}
}
}
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
public static void NotifyStaticPropertyChanged(string propertyName)
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
public static void NotifyStaticPropertyChanged<T>(Expression<Func<T>> property)
{
var expr = property.Body as MemberExpression;
if (expr == null)
throw new ArgumentException("Lambda does not contain member expression. () => MyClassOrObject.Property");
NotifyStaticPropertyChanged(expr.Member.Name);
}
}
编辑: 但是,如果事件是在基 class 中定义的,则它不起作用。
public abstract class MyBaseViewModel
{
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
public static void NotifyStaticPropertyChanged(string propertyName)
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
public static void NotifyStaticPropertyChanged<T>(Expression<Func<T>> property)
{
var expr = property.Body as MemberExpression;
if (expr == null)
throw new ArgumentException("Lambda does not contain member expression. () => MyClassOrObject.Property");
NotifyStaticPropertyChanged(expr.Member.Name);
}
}
public class GlobalManager : MyBaseViewModel
{
static object _LockObject_GFS = new object();
static double _GlobalFontSize = 10.0;
public static double GlobalFontSize
{
get
{
lock (_LockObject_GFS)
{
return _GlobalFontSize;
}
}
set
{
lock (_LockObject_GFS)
{
if (_GlobalFontSize != value)
{
_GlobalFontSize = value;
NotifyStaticPropertyChanged("GlobalFontSize");
}
}
}
}
}
StaticPropertyChangedEvent
必须在 属性 所在的相同 class 中定义才能更新绑定:
View is not getting notified when value of static Property Changes