类型解析
程序集类型解析器
// Add all types, which are derived from AvaloniaObject, in the Avalonia.Controls.dll
// to the type resolver.
var resolver = new GenericTypeResolver<Button>();自定义类型解析器
// #1 Use Resolver.
public class CustomTypeResolver1 : Resolver
{
// Nothing to do.
}
// #2 Implement IResolver.
public class CustomTypeResolver2 : IResolver
{
protected Dictionary<string, Type> Types;
protected Resolver()
{
Types = new Dictionary<string, Type>();
}
public bool TryAddType(string name, Type type)
{
if (Types.ContainsKey(name))
{
return false;
}
Types.Add(name, type);
return true;
}
public bool TryAddType<T>(string name)
{
if (Types.ContainsKey(name))
{
return false;
}
Types.Add(name, typeof(T));
return true;
}
public bool TryGetType(string name, out Type? type)
{
return Types.TryGetValue(name, out type);
}
public IEnumerable<Type> GetAllTypes()
{
return Types.Values;
}
}添加类型到解析器
单个类型
注册类型解析器到管理服务
在构建 Avalonia 服务时注册类型
最后更新于