我正在尝试将此函数从 Mahapps.Metro 中的示例从 C# 转换为 VB,但我无法弄清楚。我希望有人能帮我一把。
[Export(typeof(StartupTask))]
public void ApplyViewLocatorOverride()
{
var viewLocator = this.serviceLocator.GetInstance<IViewLocator>();
Micro.ViewLocator.GetOrCreateViewType = viewLocator.GetOrCreateViewType;
}
使用转换工具我想出了这个:
<Export(GetType(StartupTask))> _
Public Sub ApplyViewLocatorOverride()
Dim viewLocator = Me.serviceLocator.GetInstance(Of IViewLocator)()
Micro.ViewLocator.GetOrCreateViewType = viewLocator.GetOrCreateViewType
End Sub
看起来它应该可以工作,但我在 GetOrCreateViewType 下得到一个波浪线,因为它期望“viewType As Type”作为参数。
使用 GetOrCreateViewType 的 C# 版本是否像扩展方法?知道如何在 VB 中完成同样的事情吗?
这是 ViewLocator
[Export(typeof(IViewLocator))]
public class ViewLocator : IViewLocator
{
private readonly IThemeManager themeManager;
[ImportingConstructor]
public ViewLocator(IThemeManager themeManager)
{
this.themeManager = themeManager;
}
public UIElement GetOrCreateViewType(Type viewType)
{
var cached = IoC.GetAllInstances(viewType).OfType<UIElement>().FirstOrDefault();
if (cached != null)
{
Micro.ViewLocator.InitializeComponent(cached);
return cached;
}
if (viewType.IsInterface || viewType.IsAbstract || !typeof(UIElement).IsAssignableFrom(viewType))
{
return new TextBlock { Text = string.Format("Cannot create {0}.", viewType.FullName) };
}
var newInstance = (UIElement)Activator.CreateInstance(viewType);
var frameworkElement = newInstance as FrameworkElement;
if (frameworkElement != null)
{
frameworkElement.Resources.MergedDictionaries.Add(this.themeManager.GetThemeResources());
}
Micro.ViewLocator.InitializeComponent(newInstance);
return newInstance;
}
}