A Microsoft platform for building and publishing apps for Windows devices.
Hello,
Welcome to Microsoft Q&A.
The process of using C# to create an optional package project described in the document is relatively clear, but it may lack the actual project describe. Here I will make some additions.
For the first to fifth steps, this is relatively clear:
- Create a new UWP application with the minimum version set to the Windows 10 Fall Creators Update SDK (Build 16299) or higher.
- Add a new Optional Code Package (Universal Windows) project to the solution. Ensure the Minimum Version and Target Version match that of your main app.
- If you plan to submit your apps to the Store, right click on both projects and select Store -> Associate App with the Store...
- Open the Package.appxmanifest file of the main app and find the Identity Name value. Make a note of this value for the next step.
- Open the optional app package's Package.appxmanifest file and find the
uap3:MainAppPackageDependencyName value. Update theuap3:MainAppPackageDependencyName value to match the Identity Name value of the main app package from the previous step.
---
6. Add a Bundle.mapping.txt file to the main app. Follow the steps in this Related sets section to create a related set containing both apps.
In this step, the contents of Bundle.Mapping.txt are as follows:
[OptionalProjects]
"..\MyOptionalPackage\MyOptionalPackage.csproj"
7,8. Regenerate the optional package project, find the generated winmd file, and reference it in the main project. The file path is as follows
..\MyOptionalPackage\bin\[architecture]\[configuration]\Reference\MyOptionalPackage.winmd
The current architecture of the selected winmd file should be the same as the target architecture of the main project.
---
9. In the main app project, navigate to the project build properties and select Compile with .NET Native tool chain. Currently, only debugging in .NET Native is supported for optional code package creation in C#. Go to the project debug properties and select Deploy optional packages. This will ensure that both packages are in sync whenever you deploy the main app project.
---
After completing the above steps, as an example, our purpose is to create a page in the optional package, and then navigate to that page in the main project.
MyOptionalPackage/TestPage.xaml.cs
public sealed partial class HalloPage : Page
{
public HalloPage()
{
// Do not use InitializeComponent method in optional packages
Uri resourceUri = new Uri("ms-appx://[MyOptionalPackage Identity Name]/HalloPage.xaml");
Application.LoadComponent(this, resourceUri, ComponentResourceLocation.Nested);
}
}
MainApp/MainPage.xaml
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel HorizontalAlignment="Center">
<Button FontSize="24" Click="Button_Click">Show page from optional package</Button>
</StackPanel>
<Frame x:Name="ContentFrame" Grid.Row="1"/>
</Grid>
MainApp/MainPage.xaml.cs
using MyOptionalPackage;
// ...
private async void Button_Click(object sender, RoutedEventArgs e)
{
var optionalPackage = Package.Current.Dependencies.FirstOrDefault(p => p.IsOptional);
if (optionalPackage is object && optionalPackage.Id.Name == "[MyOptionalPackage Identity Name]" && optionalPackage.Status.VerifyIsOK())
{
ContentFrame.Navigate(typeof(TestPage));
}
else
{
var dialog = new MessageDialog("Optional package is not installed.", "Can't show page");
await dialog.ShowAsync();
}
}
It should be noted that the winmd file is referenced in the main project instead of the optional package project
---
installation steps
- Deploy the main application first
- Deploy optional package application
- Run the main application, click the button to navigate
- You can also run the main application project in debug mode
In subsequent application updates, since the main project contains the winmd file of the optional package, if the optional package updates the new API, the winmd file in the main application also needs to be updated synchronously.
Thanks.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.