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

Viewed 342

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.

1 Answers

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.

Related