我决定使用反射黑客。您必须在应用程序启动中使用此代码来交换ResourceDictionary
内部 .NET 类中使用的代码SR
:
var assembly = typeof(DocumentViewer).Assembly;
var sr = assembly.GetType("System.Windows.SR");
var field = sr.GetField("_resourceManager", BindingFlags.NonPublic | BindingFlags.Static);
var resourceManager = (ResourceManager)field.GetValue(null);
var wrapper = new ResourceManagerWrapper(resourceManager);
wrapper.SetString("DocumentViewerSearchUpCompleteLabel", "test");
wrapper.SetString("DocumentViewerSearchDownCompleteLabel", "test");
wrapper.SetString("DocumentViewerSearchCompleteTitle", "test");
//And so on... you can find all those messages with dotPeek or similar tool in `DocumentViewer` implementation
field.SetValue(null, wrapper);
我将资源查找转发到 ResourceManager 的包装类非常简单:
public class ResourceManagerWrapper : ResourceManager
{
private Dictionary<string, string> local_ = new Dictionary<string,string>();
private ResourceManager manager_;
public ResourceManagerWrapper(ResourceManager manager)
{
this.manager_ = manager;
}
public override string GetString(string name)
{
string value;
if(local_.TryGetValue(name, out value))
{
return value;
}
else
{
return manager_.GetString(name);
}
}
public void SetString(string key, string value)
{
local_[key] = value;
}
}