Core: [.NET Core 3][VB.NET][WPF] error BC30149: Class 'Application' must implement 'Sub InitializeComponent()' for interface 'IComponentConnector'.

Created on 27 Nov 2018  路  9Comments  路  Source: dotnet/core

When building a WPF application using VB.NET on .NET Core 3, you get the following error

obj\Debug\net462\Application.g.vb(45,16): error BC30149: Class 'Application' must implement 'Sub InitializeComponent()' for interface 'IComponentConnector'.

This is due to VB.NET applications using Application.xaml as the ApplicationDefinition item by default, instead of App.xaml like C#.

The workaround is to add the following into the .vbproj file:

<ItemGroup>
  <ApplicationDefinition Include="Application.xaml" Condition="'$(EnableDefaultApplicationDefinition)' != 'false' And Exists('$(MSBuildProjectDirectory)/Application.xaml')" />
  <Page Remove="@(ApplicationDefinition)" />
</ItemGroup>
area-wpf documentation

Most helpful comment

I agree. Supporting both App.xaml and Application.xaml for both languages seems sensible.

All 9 comments

cc @diverdan92 @fabiant3 @vatsan-madhavan

This is related to https://github.com/dotnet/cli/issues/10106. We should consider updating the default logic in the SDK for VB.NET to incorporate Application.xaml as the default for Preview 2.

cc @dotnet/wpf-developers

I expected the following to work, but it does not:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <OutputType>WinExe</OutputType>
    <UseWPF>true</UseWPF>
    <!-- Don't make ApplicationDefinition for me -->
    <EnableDefaultApplicationDefinition>false</EnableDefaultApplicationDefinition>
  </PropertyGroup>

  <ItemGroup>
    <!-- This is my application definition -- I should be able to call it whatever I want -->
    <ApplicationDefinition Include="Application.xaml" />
  </ItemGroup>
</Project>

So there are two issues here:

  1. Default application definition for VB doesn't match VB tradition
  2. If you opt out of EnableApplicationDefinition, but not EnabledDefaultPageItems, you have to remove your own ApplicationDefinition from pages yourself.

I think this is a simpler workaround if you're going to paste it into your project and not a targets file trying to light-up on Application.xaml presence and different values of EnableDefaultApplicationDefinition.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <OutputType>WinExe</OutputType>
    <UseWPF>true</UseWPF>
+    <EnableDefaultApplicationDefinition>false</EnableDefaultApplicationDefinition>
  </PropertyGroup>

  <ItemGroup>
+    <ApplicationDefinition Include="Application.xaml" />
+    <Page Remove="@(ApplicationDefinition)" />
  </ItemGroup>
</Project>

It is basically what I expected to work + the Page remove as a workaround for bug (2).

I wonder if it's worth making the targets have different defaults for VB or C#. Another solution would be for default to try App.xaml first and fall back Application.xaml (based on existence), irrespective of language. That feels less surprising to me. If I were porting something from VB to C# or vice versa, I'd get very confused otherwise.

I like the idea of treating both App.xaml and Application.xaml as default candidates for ApplicationDefinition.

I agree. Supporting both App.xaml and Application.xaml for both languages seems sensible.

  1. In App.xaml, remove the StartupURI element in tag.
  2. Change App.xaml.vb as below.
Imports System.Windows
''' <summary>
''' Interaction logic for App.xaml
''' </summary>
Partial Public Class App
    'Inherits Application
    Public Shared Sub Main()
        Dim app As Application = New Application()
        app.Run(New MainWindow())
    End Sub
End Class
  1. Add "StartupObject" in the vbproj file as below.
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
    <StartupObject>Sub Main</StartupObject>
  </PropertyGroup>
</Project>

Error BC30149 will go off and the project will compile fine.

This issue was moved to dotnet/wpf#1971

Was this page helpful?
0 / 5 - 0 ratings