WPF Form Designer Problems

Tool: Visual Studio, Visual Studio Code, PSScriptPad, PowerShell Module
Version: 

I’m trying Pro Tools now and having issues with the WPF designer. The first issue is that when I try to subscribe to event handlers, the property window tells me there’s no code-behind file.

image

I tried manually typing out all of the methods in this article including Add-ControlVariables and Set-EventHandler. The methods run and the button instance is obtained but the event can’t be subscribed. Instead this error is generated:

[ERROR] Method invocation failed because [System.Windows.Controls.Button] does not 
[ERROR] contain a method named 'addClick'.
[ERROR] At 
[ERROR] C:\Users\xxxx\Code\xxxx\TrialAutomation\TAGui\NewUserWindow.xaml.ps1:22 
[ERROR] char:2
[ERROR] +     $AddUserButton.addClick({
[ERROR] +     ~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
[ERROR]     + FullyQualifiedErrorId : MethodNotFound
[ERROR]  
[ERROR] The pipeline has been stopped.
[ERROR] + CategoryInfo          : OperationStopped: (:) [], PipelineStoppedException
[ERROR] + FullyQualifiedErrorId : PipelineStopped

The error message is accurate in that System.Windows.Button does not have an addClick method. It does inherit the Click event, but that class also doesn’t have an addClick method.

  1. Is there a reason why the WPF designer is not finding the code-behind file?
  2. What is the proper way to subscribe to the click event without help from the designer?

I realized that I was missing two important pieces.

  1. It appears the designer automatically generates a line of code that looks for the button and removes the ‘Click’ attribute. I had missed that line and needed to type it in.
  2. The method was actually add_Click not addClick.

I think both of these would have been done correctly if the designer was correctly working to generate code in the code-behind. Would love to figure out what’s wrong and get that working.

The reason this is happening, I think, is because the control you are editing doesn’t have a name defined. Try adding a name to and then see if the event handlers can be generated correctly.

For example:

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" x:Name="test" Click="ClickMe"/>

I’m aware that x:name is required. In fact all of my controls are named. This does not address the issue with the designer.

Hmm ok. I swear I’ve seen this issue before. Let me look through my notes to see if I can remember what resolved it.

I’m also seeing the same issue, would like to know how to resolve it.

Can either of you share your Pssproj file? Can you also check out this thread to see if installing these optional features helps at all?

Got it working. This was starting from a fresh install of Visual Studio Community 2019:

First install had .NET desktop app and UWP development workloads selected, but compared to the feature set listed in the above thread, I was missing F# desktop language support and SQL Server Express 2016 LocalDB. .NET also had the ML.NET Model Builder included by default. This setup produced the error listed.

My 2nd attempt was to add the two missing features, F# support and the SQL LocalDB. This still produced the same error, but I also tried to launch the WPF window with F5 and received an error stating “powershell tools for visual studio host has not started” - not sure if that was related.

Final attempt was to mirror the feature list exactly, including removing the UWP workload and the ML.NET Model Builder from the .NET workload. Once that install was done, I did see the event list show up for WPF controls.

Ok. Thanks. Glad you have it working. I’ll include this information into our pre reqs issue that we’re working on.

Glad to see @swoot got it working, but I’m confused what the missing dependency is. It looks like things didn’t start working for swoot until he removed the UWP workload entirely and removed ML.Net Model Builder.

I don’t have the ML.Net Model Builder installed:

And since I do HoloLens development, I can’t uninstall the entire UWP workload. That will be something that a lot of developers won’t be able to uninstall.

So what is the actual missing dependency?

I’m not 100% sure yet. The .NET Development workload used to be enough to cover all of PoshTools features but it looks like something has changed there and we need to identify the exact dependencies required here.

I don’t know why UWP’s removal would cause it to start working. That seems counterintuitive.

1 Like

for me, if I would copy and paste the button I would get the same issue.

how I got around this was I would need to remove the action

Click=“CloseButton_Click” from the XAML, then in the Properties I would need to cut and paste the name back

after it would work… not sure why though.

<Button Content="Close"
                    x:Name="CloseButton"
                    Foreground="White"
                    FontSize="10"
                    FontWeight="Medium"
                    Grid.Column="12"
                    Grid.ColumnSpan="2"
                    Click="CloseButton_Click">
                <Button.Style>
                    <Style TargetType="Button">
                        <Setter Property="TextElement.FontFamily"
                                Value="Fonts/#Dosis" />
                        <Setter Property="Background"
                                Value="#FE6584" />
                        <Setter Property="Cursor"
                                Value="Hand" />
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver"
                                     Value="True">
                                <Setter Property="Background"
                                        Value="#FF4C70" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Border Width="75"
                                Height="15"
                                CornerRadius="8"
                                Background="{TemplateBinding Background}">
                            <ContentPresenter VerticalAlignment="Center"
                                              HorizontalAlignment="Center" />
                        </Border>
                    </ControlTemplate>
                </Button.Template>
</Button>

also if I had anything in the code behind that referenced the button would result in an issue

so I would need to remove 3 parts of the code behind, then repeated the first step

Thanks for the info here. I’m going to open a separate issue for this behavior. It seems like it’s not related to pre-reqs.