Share via

How can we use C# code to load code in the optional package?

Barry Wang 61 Reputation points
2020-10-10T05:21:54.137+00:00

I've read the doc here https://learn.microsoft.com/en-us/windows/msix/package/optional-packages-with-executable-code and understand that we can use optional package to divide our app to different packages. However, how we can load the code in the optional package in C#? The doc wrote here https://learn.microsoft.com/en-us/archive/blogs/appinstaller/loading-code-from-an-optional-package is related to C++ dll. However in C# it is not a dll. By the way, if I understand correctly, when using C# and optional package, I can get two appx installed and I can write code to use my main appx to load my code from another optional package appx, am I right?

Developer technologies | Universal Windows Platform (UWP)
0 comments No comments

Answer accepted by question author

Richard Zhang-MSFT 6,936 Reputation points Microsoft Employee Moderator
2020-10-12T03:34:38.813+00:00

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:

  1. Create a new UWP application with the minimum version set to the Windows 10 Fall Creators Update SDK (Build 16299) or higher.
  2. 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.
  3. If you plan to submit your apps to the Store, right click on both projects and select Store -> Associate App with the Store...
  4. 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.
  5. Open the optional app package's Package.appxmanifest file and find the uap3:MainAppPackageDependency Name value. Update the uap3:MainAppPackageDependency Name 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

  1. Deploy the main application first
  2. Deploy optional package application
  3. Run the main application, click the button to navigate
  4. 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.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-12-14T21:27:15.48+00:00

    @Richard Zhang-MSFT ; thanks for the explanation. I have followed all steps 1- 8 but unfortunately I cannot find the path "..\MyOptionalPackage\bin[architecture][configuration]\Reference\MyOptionalPackage.winmd". Please is there anything I need to do? The missing path is from the Reference folder, I can not find it in the Optional Package directory.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.