我正在使用VB.NET做一个VS2013 WPF应用程序,我只是试图找到并使用正确的入口点。
我读过很多关于这个问题的答案,一个说些什么,另一个说相反的话。他们主要说:项目的切入点是您可以在Application.g.vb中找到的自动生成的main()。是的,好的,非常好的but...it是一个生成的文件,不是一个修改它的好主意。因此,我搜索了如何实现自己的main()方法的网络,常见的答案是:
我已经这样做了,但是我不断地得到这样的错误:接口'System.Windows.Markup.IComponentConnector'.的错误3类'Application‘必须实现'Sub InitializeComponent()’
这是它生成的Application.g.i.vb文件:
#ExternalChecksum("..\..\Application.xaml","{406ea660-64cf-4c82-b6f0-42d48172a799}","DB788882721B2B27C90579D5FE2A0418")
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:4.0.30319.42000
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict Off
Option Explicit On
Imports System
Imports System.Diagnostics
Imports System.Windows
Imports System.Windows.Automation
Imports System.Windows.Controls
Imports System.Windows.Controls.Primitives
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Ink
Imports System.Windows.Input
Imports System.Windows.Markup
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Effects
Imports System.Windows.Media.Imaging
Imports System.Windows.Media.Media3D
Imports System.Windows.Media.TextFormatting
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Windows.Shell
'''<summary>
'''Application
'''</summary>
<Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Application
Inherits System.Windows.Application
Implements System.Windows.Markup.IComponentConnector
Private _contentLoaded As Boolean
'''<summary>
'''InitializeComponent
'''</summary>
<System.Diagnostics.DebuggerNonUserCodeAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")> _
Public Sub InitializeComponent()
#ExternalSource("..\..\Application.xaml",4)
Me.StartupUri = New System.Uri("MainWindow.xaml", System.UriKind.Relative)
#End ExternalSource
If _contentLoaded Then
Return
End If
_contentLoaded = True
Dim resourceLocater As System.Uri = New System.Uri("/FatLink;component/application.xaml", System.UriKind.Relative)
#ExternalSource("..\..\Application.xaml",1)
System.Windows.Application.LoadComponent(Me, resourceLocater)
#End ExternalSource
End Sub
<System.Diagnostics.DebuggerNonUserCodeAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0"), _
System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never), _
System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes"), _
System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity"), _
System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")> _
Sub System_Windows_Markup_IComponentConnector_Connect(ByVal connectionId As Integer, ByVal target As Object) Implements System.Windows.Markup.IComponentConnector.Connect
Me._contentLoaded = True
End Sub
End Class
so...as Sub InitializeComponent()存在吗,为什么我总是得到这个错误?
**编辑:**我的Application.xaml.vb只是:
Partial Public Class Application
<System.STAThreadAttribute(), _
System.Diagnostics.DebuggerNonUserCodeAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")> _
Public Shared Sub Main()
Dim app As Application = New Application()
app.InitializeComponent()
app.Run()
End Sub
End Class
发布于 2016-03-06 13:40:09
我设法复制了你的问题:
Main
文件中创建自定义Application.xaml.vb
Enable Application Framework
(启动对象自动成为我们的主子)Build Action
属性为Page
此时,调用app.InitializeComponent()
的行带有红色的下划线,以表示项目找不到InitializeComponent方法。如果此时试图编译,就会得到上述错误。
最后一步:
在Application.xaml文件中,删除
StartupUri="MainWindow.xaml"
现在,切换消失了,编译也正确结束了。
如果您想启动自己的主窗口,可以用
app.Run(New MainWindow())
发布于 2016-03-06 13:11:47
如果您得到了这个错误,您可能还没有声明您的应用程序将从Main()方法开始。所以:
“转到您的项目设置,取消Enable应用程序框架复选框(如果勾选)并选择Sub作为启动对象,然后将您的Main -方法添加到Application.xaml.vb (加上记录器和其他东西)。”
当您这样做时,VS不会创建“它自己的”Main-方法,而是调用您在Application.xaml中定义的方法。
还请检查以下内容:http://ugts.azurewebsites.net/data/UGTS/document/2/3/32.aspx
https://stackoverflow.com/questions/35827046
复制相似问题