0

I'm trying to translate this C# code to VB.Net

public event EventHandler<OnBackgroundEnteringEventArgs> OnBackgroundEntering;

public async Task SaveStateAsync()
{
    var suspensionState = new SuspensionState()
    {
        SuspensionDate = DateTime.Now
    };

    var target = OnBackgroundEntering?.Target.GetType();
    var onBackgroundEnteringArgs = new OnBackgroundEnteringEventArgs(suspensionState, target);

    OnBackgroundEntering?.Invoke(this, onBackgroundEnteringArgs);

    await ApplicationData.Current.LocalFolder.SaveAsync(stateFilename, onBackgroundEnteringArgs);
}

This code is part of the Windows Template Studio. The original file can be found here: SuspendAndResumeService.cs

The translated code in VB.Net:

Public Event OnBackgroundEntering As EventHandler(Of OnBackgroundEnteringEventArgs)

Public Async Function SaveStateAsync() As Task
    Dim suspensionState As New SuspensionState With {.SuspensionDate = DateTime.Now}
    Dim target = OnBackgroundEntering?.Target.GetType()
    Dim onBackgroundEnteringArgs = New OnBackgroundEnteringEventArgs(suspensionState, target)

    RaiseEvent OnBackgroundEntering(Me, onBackgroundEnteringArgs)

    Await ApplicationData.Current.LocalFolder.SaveAsync(stateFilename, onBackgroundEnteringArgs)
End Function

I have problem with this part:

Dim target = OnBackgroundEntering?.Target.GetType()

The Intellisense show the following error:

Error BC32022 'Public Event OnBackgroundEntering As EventHandler(Of OnBackgroundEnteringEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

I would appreciate if anyone could help me. Thanks.

SZL
  • 805
  • 8
  • 12
  • Thanks, meanwhile I found a solution too here: https://stackoverflow.com/questions/4108163/how-can-i-get-an-actual-eventhandler-delegate-instance-from-an-event-in-vb-net?rq=1 – SZL Aug 13 '17 at 18:32
  • The first solution is not good because there are no Application.DoEvents in an UWP app (sorry I forget the UWP tag). Finally this translation is not good? Dim target = OnBackgroundEnteringEvent.Target.GetType() – SZL Aug 13 '17 at 18:48
  • 1
    The `Application.DoEvents()` has nothing to do with the real answer. It's just from the original code. What you care about is `RaiseEvent`, and I'm sure that _is_ in the UWP VB.NET language. – Peter Duniho Aug 13 '17 at 18:53
  • Sorry, I thought you were having problems with the event-raising. You are right, the other solution you found is right. You can get the delegate instance that way. That said, the code you are translating seems really broken. The _receiver_ of the event should already know its type. It seems dumb to me to have to pass that to the receiver. Furthermore, the code you have won't work for multiple event receivers. – Peter Duniho Aug 13 '17 at 18:56
  • No problem, can you please explain your last sentence? How can I fix it to work in every cases? – SZL Aug 13 '17 at 19:03
  • Fix what? You can use the marked duplicate to get the delegate instance, from which you can then get the `Target` property and the rest is the same as the code you already have. My point is simply that the code you're trying to convert doesn't make sense. I don't know why they think it's useful to pass the target delegate type back to the receiver of the event, so I can't help rectify that issue. If you're going to use that code example, you'll need to figure out yourself why they think that's useful and consider what other alternatives there might be. – Peter Duniho Aug 13 '17 at 19:12
  • Unfortunately, searching that repo, I don't see anything that shows any code actually _using_ the `OnBackgroundEntering` event, so it's not clear how they meant it to be used. – Peter Duniho Aug 13 '17 at 19:13
  • I not use the repo that I linked into the post. The reason of linking, that the file is exactly the same, than in my solution. The original code is coming from the Windows Template Studio extension auto-generated code. I will debug this section to see why is created in that way. Thanks! – SZL Aug 13 '17 at 19:21
  • 1
    If you can provide an example of usage, that could help clarify why the event is declared the way it is. Otherwise, I would just remove the `Type` property from the `OnBackgroundEnteringEventArgs` class altogether. It's a weird property and if you're not using, it's complicating the code for no good reason. – Peter Duniho Aug 13 '17 at 19:52

0 Answers0