Question

I'm trying to convert this function from a sample in Mahapps.Metro from C# to VB and I can't figure it out. I was hoping someone might be able to give me a hand.

[Export(typeof(StartupTask))]
    public void ApplyViewLocatorOverride()
    {
        var viewLocator = this.serviceLocator.GetInstance<IViewLocator>();
        Micro.ViewLocator.GetOrCreateViewType = viewLocator.GetOrCreateViewType;
    }

Using a conversion tool I came up with this:

<Export(GetType(StartupTask))> _
    Public Sub ApplyViewLocatorOverride()
        Dim viewLocator = Me.serviceLocator.GetInstance(Of IViewLocator)()
        Micro.ViewLocator.GetOrCreateViewType = viewLocator.GetOrCreateViewType
    End Sub

It looks like it should work but I'm getting a squiggly under GetOrCreateViewType because it is expecting 'viewType As Type' as a parameter.

Is the C# version using GetOrCreateViewType like an extension method? Any idea how I could accomplish the same thing in VB?

Here is the 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;
    }
}
Was it helpful?

Solution

Just thought I'd make my comment an answer since it seems to be the right one:

I don't really know VB that well (it's been a while) but I think it's creating an implicit delegate using a method group (GetOrCreateViewType on it's own will be a method group) - have you tried

AddressOf GetOrCreateViewType

on the VB side instead?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top