我正在尝试将此函数从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;
}
}
答案 0 :(得分:1)
我想我的评论是一个答案,因为它似乎是正确的答案:
我不太了解VB(它已经有一段时间了)但我认为它使用方法组(GetOrCreateViewType
就可以创建一个隐式委托&#39 ; s自己将是一个方法组) - 你试过吗
AddressOf GetOrCreateViewType
而不是VB端的?