【问题标题】:Converting Caliburn.Metro.Demo from C# to VB将 Caliburn.Metro.Demo 从 C# 转换为 VB
【发布时间】: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


【解决方案1】:

我只是想我的评论是一个答案,因为它似乎是正确的:

我不太了解 VB(已经有一段时间了),但我认为它正在使用方法组创建一个隐式委托(GetOrCreateViewType 本身就是一个方法组) - 你试过吗

AddressOf GetOrCreateViewType

在 VB 端?

【讨论】:

    猜你喜欢
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多