Single Instanced WPF applications issue
-
Monday, January 30, 2012 4:41 PM
Afternoon all,
I've recently gone through the steps in this article to create a single instanced wpf application http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx . After I tested and closed the first test of this I'm now constantly getting
Class 'Application' must implement 'Sub InitializeComponent()' for interface 'System.Windows.Markup.IComponentConnector'.
some times clicking rebuild will resolve the issue and I can run the code other times it just continues to fail. Has anyone else come across this and if so any ideas on what im doing wrong ?
contents of my Application.xaml.vb
Imports System.Collections.Generic Imports System.Configuration Imports System.Data Imports System.Linq Imports System.Windows Imports System.Windows.Markup ''' <summary> ''' Interaction logic for App.xaml ''' </summary> Partial Public Class App Inherits Application Implements ISingleInstanceApp, IComponentConnector Private Const Unique As String = "My_Unique_Application_String" Private _contentLoaded As Boolean <STAThread()> _ Public Shared Sub Main() If SingleInstance(Of App).InitializeAsFirstInstance(Unique) Then Dim a = New App() a.InitializeComponent() a.Run() ' Allow single instance code to perform cleanup operations SingleInstance(Of App).Cleanup() End If End Sub #Region "ISingleInstanceApp Members" Public Function ISingleInstanceApp_SignalExternalCommandLineArgs(ByVal args As IList(Of String)) As Boolean Implements ISingleInstanceApp.SignalExternalCommandLineArgs Dim win As MainWindow = My.Application.MainWindow win.newCommandLineArgs(args) Return True End Function #End Region End Class
and the application.xaml is pretty empty with<Application x:Class="Application" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" ShutdownMode="OnMainWindowClose"> <Application.Resources> </Application.Resources> </Application>
any help is appreciated*EDIT*
I forgot to mention the files that are throwing the error are Application.g.vb and Application.g.i.vb
the contents of those files are
#ExternalChecksum("..\..\..\Application.xaml","{406ea660-64cf-4c82-b6f0-42d48172a799}","F5BE6FD6DCB7C43A14B51A47D8190D35") '------------------------------------------------------------------------------ ' <auto-generated> ' This code was generated by a tool. ' Runtime Version:4.0.30319.239 ' ' 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.Forms.Integration 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(), _ System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")> _ Partial Public Class Application Inherits System.Windows.Application Implements System.Windows.Markup.IComponentConnector Private _contentLoaded As Boolean '''<summary> '''InitializeComponent '''</summary> <System.Diagnostics.DebuggerNonUserCodeAttribute()> _ Public Sub InitializeComponent() #ExternalSource("..\..\..\Application.xaml",2) 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("/LogReader;component/application.xaml", System.UriKind.Relative) #ExternalSource("..\..\..\Application.xaml",1) System.Windows.Application.LoadComponent(Me, resourceLocater) #End ExternalSource End Sub <System.Diagnostics.DebuggerNonUserCodeAttribute(), _ 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 Classand
#ExternalChecksum("..\..\..\Application.xaml","{406ea660-64cf-4c82-b6f0-42d48172a799}","B23A0EBE5EE6C637AB236986A35F74DB") '------------------------------------------------------------------------------ ' <auto-generated> ' This code was generated by a tool. ' Runtime Version:4.0.30319.239 ' ' 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.Forms.Integration 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(), _ System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")> _ Partial Public Class Application Inherits System.Windows.Application Implements System.Windows.Markup.IComponentConnector Private _contentLoaded As Boolean '''<summary> '''InitializeComponent '''</summary> <System.Diagnostics.DebuggerNonUserCodeAttribute()> _ 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("/LogReader;component/application.xaml", System.UriKind.Relative) #ExternalSource("..\..\..\Application.xaml",1) System.Windows.Application.LoadComponent(Me, resourceLocater) #End ExternalSource End Sub <System.Diagnostics.DebuggerNonUserCodeAttribute(), _ 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
- Edited by New_Coder Monday, January 30, 2012 5:17 PM
All Replies
-
Monday, January 30, 2012 6:06 PM
You can contact the developer of this code on discussion forum on codeproject
http://www.codeproject.com/Articles/84270/WPF-Single-Instance-Application
Else you could check running process and use following code to check for single instance
Private Sub CheckSingleInstance() Dim prc As Process = Process.GetCurrentProcess Dim processes() As Process = Process.GetProcessesByName(prc.ProcessName) If processes.Count > 1 Then MsgBox("Application is already running") 'Application.Current.Shutdown() End If End Sub
You can also use Mutex to check for single instancehttp://odetocode.com/blogs/scott/archive/2004/08/20/the-misunderstood-mutex.aspx
Gaurav Khanna | Microsoft VB.NET MVP -
Tuesday, January 31, 2012 10:08 AM
Thanks for the suggestions Khanna,
although the code snippet wouldnt work for me as this wouldn't allow me to pass in subsequent command line args which is what I need, in regards to asking the original developer I have since posted the question to him.
My question remains though, how do you resolve Class 'Application' must implement 'Sub InitializeComponent()' for interface 'System.Windows.Markup.IComponentConnector'. which are thrown in Application.g.vb and Application.g.i.vb ?
-
Tuesday, January 31, 2012 4:54 PM
I have managed to build the code again now and when ever it breaks (which is often) I can resolve.
The temporary fix until I can work out why it is occuring is to change the designer generated code (Application.g.vb and Application.g.i.vb), the fix is to add "Implements IComponentConnector.InitializeComponent" next to the sub routine InitializeComponent() that is always present in these files.If anyone can assist in why these files are not automaticaly adding this, it would save my sanity :)
-
Tuesday, January 31, 2012 5:06 PMInitializeComponent is a fundamental and required call in the code behind. You have either renamed the namespace or class in the code behind or in the XAML without synching both. DO NOT change code in the "g" code. The "g" stands for generated and it will be overwritten. I have found no problems in generated code in my three years of using WPF. Chances are high it's either in the XAML or Code Behind for that view.
JP Cowboy Coders Unite! -
Tuesday, January 31, 2012 5:10 PMFor single instance applications you can use a static GetInstance method which returns the saved static instance of the control from the CTOR. Not thread safe put pretty good solution and quick.
JP Cowboy Coders Unite! -
Saturday, March 31, 2012 4:25 PM
I have the same problem right now and I was just wondering if you ever managed to solve it? If so it would be a great solution to have in the public domain here.
Thanks,
Sam.
-
Saturday, March 31, 2012 9:59 PM
Afternoon all,
I've recently gone through the steps in this article to create a single instanced wpf application http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx . After I tested and closed the first test of this I'm now constantly getting
Class 'Application' must implement 'Sub InitializeComponent()' for interface 'System.Windows.Markup.IComponentConnector'.
some times clicking rebuild will resolve the issue and I can run the code other times it just continues to fail. Has anyone else come across this and if so any ideas on what im doing wrong ?
contents of my Application.xaml.vb
Imports System.Collections.Generic Imports System.Configuration Imports System.Data Imports System.Linq Imports System.Windows Imports System.Windows.Markup ''' <summary>''' Interaction logic for App.xaml''' </summary>PartialPublicClass App Inherits Application Implements ISingleInstanceApp, IComponentConnector PrivateConst Unique AsString = "My_Unique_Application_String"Private _contentLoaded AsBoolean
Hi
afaics you shouldn't implement IComponentConnector in your own App.xaml.vb
file (not the generated App.g.vb).
IComponentConnector is "added" and explicitly implemented inside
App.g.vb by the Code generator.
So having a second Implements statement might cause this conflict
and removing "Implements IComponentConnector" from your own code
might solve the issue (not sure but worth a try)
Also "_contentLoaded" is a generated field of the App.g.vb file, that should not be explicitly
declared once again in your own code.
Seems part of the generated file have merged into your own code.
Chris

