【发布时间】:2024-01-13 22:01:01
【问题描述】:
我有一个在 UWP 中工作的 ViewModel,除了我的自定义字典外,所有绑定都在工作,我不知道为什么。什么都没有显示。
我使用的是 FodyWeavers,因此使用了速记符号。如果未找到键,自定义字典将返回带有 * 的键。
在视图模型中
public static TranslationDictionary Translations { get; set; }
在视图中
<TextBlock Text="{Binding Translations[Test_Translation]}" />
自定义词典
public class TranslationDictionary : Dictionary<string, string>
{
public new void Add(string key, string value)
{
if (value == null)
{
return;
}
base.Add(key, value);
}
public new void Remove(string key)
{
if (!ContainsKey(key))
{
return;
}
base.Remove(key);
}
public new string this[string key]
{
get
{
string value;
return TryGetValue(key, out value) ? value : key + "*";
}
set
{
if (value == null)
{
Remove(key);
}
else
{
base[key] = value;
}
}
}
}
【问题讨论】: