Kann mir jemand bei folgendem C# Code helfen?

20/04/2010 - 16:43 von Stefan Reinmers | Report spam
Hi zusammen, ich habe folgenden C# Code
event LocationChangedEventHandler locationChanged;

/// <summary>
/// Event that is raised when the GPS locaction data changes
/// </summary>
public event LocationChangedEventHandler LocationChanged
{
add
{
locationChanged += value;

// create our event thread only if the user decides to
listen
CreateGpsEventThread();
}
remove
{
locationChanged -= value;
}
}


event DeviceStateChangedEventHandler deviceStateChanged;

/// <summary>
/// Event that is raised when the GPS device state changes
/// </summary>
public event DeviceStateChangedEventHandler DeviceStateChanged
{
add
{
deviceStateChanged += value;

// create our event thread only if the user decides to
listen
CreateGpsEventThread();
}
remove
{
deviceStateChanged -= value;
}
}




wenn ich den nach VB.Net konvertiere, bekomme ich

Private Event locationChanged As LocationChangedEventHandler

Public Custom Event LocationChanged As LocationChangedEventHandler
AddHandler(ByVal value As LocationChangedEventHandler)
locationChanged += value

' create our event thread only if the user decides to listen
CreateGpsEventThread()
End AddHandler
RemoveHandler(ByVal value As LocationChangedEventHandler)
locationChanged -= value
End RemoveHandler
End Event


Private Event deviceStateChanged As DeviceStateChangedEventHandler

Public Custom Event DeviceStateChanged As
DeviceStateChangedEventHandler
AddHandler(ByVal value As DeviceStateChangedEventHandler)
deviceStateChanged += value

' create our event thread only if the user decides to listen
CreateGpsEventThread()
End AddHandler
RemoveHandler(ByVal value As DeviceStateChangedEventHandler)
deviceStateChanged -= value
End RemoveHandler
End Event



bekomme aber denn folgende Fehler
Die RaiseEvent-Definition fehlt für das LocationChanged-Ereignis.
Private Event locationChanged(sender As Object, args As
LocationChangedEventArgs)" ist ein Ereignis und kann nicht direkt aufgerufen
werden. Verwenden Sie eine RaiseEvent-Anweisung, um ein Ereignis aufzurufen.

Was muss ich nun tun?
 

Lesen sie die antworten

#1 Elmar Boye
20/04/2010 - 19:14 | Warnen spam
Hallo Stefan,

"Stefan Reinmers" schrieb ...
Hi zusammen, ich habe folgenden C# Code
event LocationChangedEventHandler locationChanged;

/// <summary>
/// Event that is raised when the GPS locaction data changes
/// </summary>
public event LocationChangedEventHandler LocationChanged
{
add
{
locationChanged += value;

// create our event thread only if the user decides to listen
CreateGpsEventThread();
}
remove
{
locationChanged -= value;
}
}




Das wàre in Visual Basic über einen Custom Event Handler zu lösen,
siehe u. a. in
http://msdn.microsoft.com/de-de/lib...90%29.aspx
"Gewusst wie: Deklarieren von Ereignissen zum Vermeiden einer Blockierung"


wenn ich den nach VB.Net konvertiere, bekomme ich




Nee, nee, wer so was "kompliziertes" möchte, darf sich einen Wolf tippen ;-)

Private Event locationChanged As LocationChangedEventHandler

Public Custom Event LocationChanged As LocationChangedEventHandler
AddHandler(ByVal value As LocationChangedEventHandler)
locationChanged += value

' create our event thread only if the user decides to listen
CreateGpsEventThread()
End AddHandler
RemoveHandler(ByVal value As LocationChangedEventHandler)
locationChanged -= value
End RemoveHandler
End Event



... als Beispiel für das LocationChanged sàhe es so aus
(für das andere àquivalent):

///
Public Class CustomEvents
' ggf. anpassen, dann auch bei RaiseEvent unten
Public Delegate Sub LocationChangedEventHandler(ByVal sender As Object, ByVal e As EventArgs)
' + _ für Visual Basic zum Auseinanderhalten
Private Event _locationChanged As LocationChangedEventHandler

Public Custom Event LocationChanged As LocationChangedEventHandler
AddHandler(ByVal value As LocationChangedEventHandler)
AddHandler Me._locationChanged, value
CreateGpsEventThread()
End AddHandler
RemoveHandler(ByVal value As LocationChangedEventHandler)
RemoveHandler Me._locationChanged, value
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent _locationChanged(sender, e)
End RaiseEvent
End Event

Private Sub CreateGpsEventThread()
Console.WriteLine("CreateGpsEventThread called")
End Sub

Public Sub CallEvent()
RaiseEvent LocationChanged(Me, EventArgs.Empty)
End Sub

''' <summary>Beispiel für den Einsatz</summary>
Public Shared Sub Test()
Dim c As New CustomEvents
AddHandler c.LocationChanged, AddressOf MyLocationChanged
c.CallEvent()

RemoveHandler c.LocationChanged, AddressOf MyLocationChanged
c.CallEvent()
End Sub

Private Shared Sub MyLocationChanged(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine("Event Called")
End Sub

End Class
\\\

Einfacher kann es sein, die beiden (oder noch mehr) Ereignisse
wie im Online Beispiel zusammenzufassen und eine EventHandlerList zu verwenden.

Gruß Elmar

Ähnliche fragen