question

MarcoEnxuto avatar image
0 Votes"
MarcoEnxuto asked MarcoEnxuto commented

[UWP] Windows Runtime component wiring up events outside its project

Hi,
I'm using a BackgroundTask which is a Windows Runtime Component, and i figured out that i cannot wire up events.

My intent is, using a static class that has an event called OnCompleted to be subscribed outside the component. In this case, by a class that is inside in an UWP project.

Here's the code i implemented:

 public static class Class1
 {
    
             public delegate EventHandler<BackgroundTaskProgressEventArgs> BackgroundTaskInProgress();
             public delegate EventHandler<BackgroundTaskCompletedEventArgs> BackgroundTaskCompleted();
             public static event BackgroundTaskInProgress InProgress;
             public static event BackgroundTaskCompleted OnCompleted;
 .....
    
         private static void BackgroundTask_Progress(BackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs args)
         {
             InProgress();
         }
    
         private static void BackgroundTask_Completed(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
         {
             OnCompleted();
         }
    
 }

Meanwhile in UWP Project

....
Class1.InProgress += Class1_InProgress;

With this implementation, i get the following error: Nested Types cannot be exported to the Windows Runtime.
I took a look on this article, but i think i understood this could be done. Am i right?



windows-uwp
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@MarcoEnxuto The error message indicates that this issue is related to Nested Types, you could check whether your code has Nested Types and avoid using it.

0 Votes 0 ·

Hello, thanks for asking.
No, there aren't any.
Is the delegate a nested type?
I don't think so, but if i remove that codeblock, it compiles.

 namespace App1
 {
      public static class Class1
      {
            
                  public delegate EventHandler<BackgroundTaskProgressEventArgs> BackgroundTaskInProgress();
                  public delegate EventHandler<BackgroundTaskCompletedEventArgs> BackgroundTaskCompleted();
                  public static event BackgroundTaskInProgress InProgress;
                  public static event BackgroundTaskCompleted OnCompleted;
     }
 }
0 Votes 0 ·

@MarcoEnxuto You could refer to know the limitation of Declaring types in Windows Runtime components , for example, your class should be sealed other than static.

1 Vote 1 ·
Show more comments

0 Answers