【发布时间】:2014-01-17 20:03:59
【问题描述】:
我正在尝试将此函数从 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;
}
}
【问题讨论】:
-
我不太了解 VB(已经有一段时间了),但我认为它正在使用方法组创建隐式委托(
GetOrCreateViewType本身将是一个方法组) - 有你在 VB 端尝试了AddressOf GetOrCreateViewType吗? -
感谢 Charleh 的帮助!
-
@jweaver 如果这解决了您的问题,您可能应该接受 Charleh 的回答。
标签: c# vb.net caliburn.micro mahapps.metro