2

It is now possible to create optional package in C#. However, it is not clear how to invoke the code in the optional package from a C# UWP main app, especially if we need to invoke it in a generic fashion.

Say, I have a plugins within the optional package and they all implement IPlugin interface with an Execute method and Name property. I would like to display the Names of all the plugins in a menu and invoke the Execute method of plugin when user clicks on it.

We can iterate over all the optional packages of the main app, something like

    // Obtain the app's package first to then find all related packages
    var currentAppPackage = Windows.ApplicationModel.Package.Current;

    // The dependencies list is where the list of optional packages (OP) can be determined
    var dependencies = currentAppPackage.Dependencies;

    for (var package in dependencies)
    {
        //  If it is optional, then add it to our results vector
        if (package.IsOptional)
        {
            WriteLine("Optional Package found - {package.Id.FullName}");
        }
    }

How do I invoke the code that is present in these optional packages.

resp78
  • 1,414
  • 16
  • 37

1 Answers1

0

How to invoke code in C# optional package from a UWP app written in C#

Please check create optional package in C# step 8, we need add the .winmd file to the main project. If we do this step we could call the optional package api directly.

Add a reference from the main app project to the .winmd file found in this folder. Every time you change the API surface area in the optional package project, this .winmd file must be updated.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • If we do that, we would have to compile the main application with it and every time we release a new plugin we need to compile the main app too. This restriction is not present in C++, the linked example on the page shows that in C++ we can use LoadLibrary and there is no compile time dependency. – resp78 Jun 01 '20 at 22:43
  • 1
    Yep, But for C#, we can only use the document way to execute the code, and currently we have not such way could execute the code like c++ within C# project. – Nico Zhu Jun 02 '20 at 09:05