2

现在可以在 C# 中创建可选包。但是,尚不清楚如何从 C# UWP 主应用程序调用可选包中的代码,尤其是当我们需要以通用方式调用它时。

比如说,我在可选包中有一个插件,它们都实现IPlugin了带有Execute 方法和Name属性的接口。我想Name在菜单中显示所有插件的 s 并Execute在用户单击它时调用插件的方法。

我们可以遍历主应用程序的所有可选包,例如

    // 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}");
        }
    }

如何调用这些可选包中存在的代码。

4

1 回答 1

0

如何从用 C# 编写的 UWP 应用调用 C# 可选包中的代码

请检查C# 步骤 8 中的创建可选包,我们需要将 .winmd 文件添加到主项目中。如果我们执行此步骤,我们可以optional package直接调用 api。

将主应用程序项目中的引用添加到此文件夹中的 .winmd 文件。每次更改可选包项目中的 API 表面区域时,都必须更新此 .winmd 文件。

于 2020-06-01T14:53:06.650 回答