Provide Intellisense for Custom MarkupExtension in XAML
Asked Answered
C

1

17

I've created a custom MarkupExtension that allows an easy way to to forward events from FrameworkElements to methods on the view-model. Everything works perfectly, except Visual Studio doesn't provide any Intellisense when filling in the XAML. Is there any way that I can provide Visual Studio with the information it needs to provide Intellisense?

The MarkupExtension:

public class VmCall : MarkupExtension
{
    public string ActionName { get; }

    public VmCall(string actionName)
    {
        ActionName = actionName;
    }
    //.....
}

The view-model:

class TestWindowVm : ViewModel
{
    public void Click()
    {
        MessageBox.Show("Click!");
    }
}

Using the MarkupExtension in XAML:

<Window
    x:Class="WpfLib.Tests.VmCallMarkupExtension.TestWindow"   
    xmlns:wpfLib="clr-namespace:AgentOctal.WpfLib;assembly=WpfLib"
    //Many xmlns and attributes left out of example for brevity.
    d:DataContext="{d:DesignInstance TestWindowVm}"
    mc:Ignorable="d">
    <Button Click="{wpfLib:VmCall Click}"/>
</Window>

Ideally, when the user presses the space-bar after "VmCall", Intellisense should present them with a list of all the methods on the current element's DataContext (Just Click on TestWindowVm in this specific example).

If there is some way to make this work, based on previous experience with WinForms, I would expect some kind of Attribute I could place on the arguments of the constructor for VmCall that informs Visual Studio how to generate the intellisense for that argument. So far, I haven't been able to find anything like that. I'm not sure if I'm actually looking for the wrong thing, or if this in't possible to do at all. If it matters, I'm mainly interested in Visual Studio 2017.

I've looked at all the attributes listed in System.Windows.Markup, and nothing obvious jumped out at me.

Calefacient answered 18/10, 2017 at 18:48 Comment(7)
I don't think its possible (well, without writing something like VS extension that is). Whole intellisense seems to be hardcoded (for example, Binding constructor also accepts string and does not have any attributes, but intellisense knows to show VM properties for it).Haphazardly
I have just now create my own MarkupExtension that takes a Enum Type as parameter. VisualStudio 2017 does correctly show the expected Type when you MouseOver my Markup in the Editor, but alas, does not provide Intellisense in this case, either. Hope to hear from you if you find anything.Aloisia
Unfortunately I have no solution yet. People using my library just have to know how to call it correctly, just like in the dark ages of programming, before visual studio. If I ever find a solution, I will leave a comment here.Calefacient
Hi! Did you find any solution? Thanks in advanceMacrogamete
No, no good solution. The best I've been able to accomplish is to use a Roslyn code analyser to provide design-time errors if the MarkupExtension is incorrect.Calefacient
Visual Studio by default will show intellisense in xaml. At times this happens. You can restart VS or compile it and tryHazaki
@VimalCK this isn't about regular intellisense, this is about customizing the results for custom markup extensions in libraries.Calefacient
P
1

you have created a MarkupExtension with the string input. how can Intellisense know about which string you want? but if you, for example, use an enum, the Intellisense suggested you many things.

public enum MyEnum
    {
        first , second 
    }

public class VmCall : MarkupExtension
{
    public MyEnum ActionName { get; }

    public VmCall(MyEnum actionName)
    {
        ActionName = actionName;
    }
    //.....
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}
Prelature answered 4/12, 2019 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.