静态属性未在UI中更新 [英] Static property not updating in UI
问题描述
我花了最后一个小时试图在google和stackoverflow中找到答案.我遵循了不同的建议的建议,但到目前为止没有任何效果.我当前的代码如下:
I spent the last hour(s) trying to find an answer in google and stackoverflow. I followed different advices & suggestions, but nothing worked so far. My current code looks like this:
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);
}
}
}
}
}
吸气剂二传手都被称为. NotifyStaticPropertyChanged
可以使用,并且我的UI不会更新.我添加了TextBlock
来检查它是否更新.显然不是.
The getter & setter are both called. NotifyStaticPropertyChanged
works and my UI does not update. I've added a TextBlock
to check if it updates. Apparently it does not.
<TextBlock Text="{Binding Path=(global:GlobalManager.GlobalFontSize), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
如果我在VM(当前DataContext)中定义一个属性,并将其绑定到TextBlock
,它将使用当前值正确更新.
If I define a property in my VM (current DataContext), and bind it to a TextBlock
, it updates correctly with the current value.
当前,将Slider
的DependencyProperty
Value
绑定到此属性,以更新字体大小. (IsSnapToTickEnabled="True"
)
Currently the DependencyProperty
Value
of a Slider
is bound to this property in order to update the font size. (IsSnapToTickEnabled="True"
)
public double GlobalFontSize
{
get { return GlobalManager.GlobalFontSize; }
set { GlobalManager.GlobalFontSize = value; NotifyPropertyChanged(() => GlobalFontSize); }
}
如何使绑定与static属性一起正常工作? StaticPropertyChanged
事件不为空.
How do I get the binding to work correctly with the static property ? The StaticPropertyChanged
event is not null.
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
Edit 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
方法能够按预期工作,并且事件签名正确.
Make sure that your GetGlobalResource
and SetGlobalResource
methods work as expected and that your event signature is correct.
您可以参考以下工作示例实现并将其与您的实现进行比较:
You could refer to the below working sample implementation and compare it to yours:
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);
}
}
编辑:如果该事件是在基类中定义的,则无法使用.
It doesn't work if the event is defined in a base class though.
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
必须在属性所在的同一类中定义,以便绑定进行更新:
The StaticPropertyChangedEvent
must be defined in same class where property resides for the binding to get updated:
这篇关于静态属性未在UI中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!