Search code examples
c#mvvmwindows-runtimeuwpbackground-task

Unable to access methods and classes from my project in Windows Runtime Component in UWP


I Create background task for synchronize data with server for Background task using Windows Runtime Component in UWP App.

but, unable to access methods and classes from my project in Windows Runtime Component in UWP.

Is there any alternate way to create background task without Windows Runtime Component? or how can I able to access those classes.


Solution

  • You can create class library and add reference to it from WinMD and from your project class. Code of class library could be something like:

        public class BridgeClass
    {
      public static event Action<string> MessageReceived;
    
      public static void Broadcast(string message)
      {
          if (MessageReceived != null) MessageReceived(message);
      }
    }
    

    Inside your project class you can subscribe to this event

    BridgeClass.MessageReceived += ShowMessage;
    

    And make realization:

    void ShowMessage(string msg)
    {
    }   
    

    Now from WinMD class call it:

    BridgeClass.Broadcast("some value");