Optional Packages UWP C#
Hi, I'm trying to create an optional package for my UWP application.
I followed the guide here: https://learn.microsoft.com/en-us/windows/msix/package/optional-packages-with-executable-code#c-optional-packages-with-executable-code-1.
I referenced the winmd file created by my optional package, however when I install both the main Application and the optional package I get a class not registered error.
I'm unclear on how to load code from the optional package to execute inside of my main app.
Also does this allow loading of xaml as well?
Thanks,
Daniel
Universal Windows Platform (UWP)
C#
-
Daniel Breaux 1 Reputation point
2022-08-19T22:04:02.61+00:00 I was able to figure out that if I use related sets the code compiles and gets the correct results with no exceptions but this is a less than ideal solution as it tightly couples the release version of the app with the versions of its optional packages which makes an update to the optional package an update to the main app. My existing question still stands but I would like to know if anyone knows a way to use the optional packages without related sets in a way that allows me to update the Optional package without redeploying the Main Application
-
Roy Li - MSFT 31,766 Reputation points • Microsoft Vendor
2022-08-22T06:15:24.433+00:00 Does the optional package that you created contains executable code? If it is, then the optional package must be part of a related set. This is mentioned in the document that you are following. It's a read note in the Prerequisites Part.
-
Roy Li - MSFT 31,766 Reputation points • Microsoft Vendor
2022-08-26T09:00:44.987+00:00 Any updates about this?
-
Daniel Breaux 1 Reputation point
2022-08-29T13:16:32.623+00:00 I was able to get it to work in a small sample project, using related sets. Testing on my larger project though is giving me some issues.
-
Roy Li - MSFT 31,766 Reputation points • Microsoft Vendor
2022-08-30T06:20:45.957+00:00 @Daniel Breaux Do you mean after using the related sets, your application will still have a class not register error?
-
Daniel Breaux 1 Reputation point
2022-08-30T13:11:14.337+00:00 No on my larger project I ran into a method not found exception. So we are trying to break it down and incrementally add our components to the optional package build/test to ensure the code we were adding to the package wasn't causing the issue.
-
Daniel Breaux 1 Reputation point
2022-08-30T21:45:59.053+00:00 @Roy Li - MSFT The exact issue we are running into now is that when we have a UWP Class library that holds some views (pages) that we want to expose or load in the main application. When using a view from the optional package directly the InitializeComponent() gets replaced with something to the effect of
Uri resourceLocator = new Uri("ms-appx://{AppIdentity}/Page.xaml");
Application.LoadComponent(this, resourceLocator, ComponentResourceLocation.Nested);and is successful in loading the xaml.
However when referencing xaml from the uwp class library thats referenced by the Optional Package like so:
Uri resourceLocator = new Uri("ms-appx://{AppIdentity}/{ProjectName}/Page.xaml");
Application.LoadComponent(this, resourceLocator, ComponentResourceLocation.Nested);Throws an exception: System.ArgumentException: 'Unrecognized custom TypeName'
I'm just confused as to why it doesn't work when coming from the Class library referenced by the optional package as opposed to directly in the optional package.
-
Roy Li - MSFT 31,766 Reputation points • Microsoft Vendor
2022-08-31T02:51:24.95+00:00 Could you please share the detailed steps about how to create a small sample project to reproduce this behavior?
-
Daniel Breaux 1 Reputation point
2022-08-31T13:33:11.79+00:00 @Roy Li - MSFT
Create a UWP project
Add an Optional Code Package UWP project to the solution.
Add a UWP Class library to the solutionIn the class library add a Page to the project
Reference the class library in the optional package
In the Optional Package's class create a method/property that returns your page's type from the class library project.Make your optional package a related set to your main project and add the winmd reference.
Add a button to your main page which calls Frame.Navigate({OptionalPackageClassName}.{MethodNameToGetType});
When the button is pressed you will receive the exception and no UI will load.Another thing I tried was creating a Resource Dictionary in the optional package and returning the data template via the c# code.
It does require you have a code component to the Resource Dictionary (So I initially added it as a page and just changed the type to Resource Dictionary so I could have the backing c# class/constructor) and that in the constructor its loaded like so:
Uri resourceLocator = new Uri("ms-appx://{AppIdentity}/Page.xaml");
Application.LoadComponent(this, resourceLocator, ComponentResourceLocation.Nested);
but it does allow you to return it to the main app like so:
new ResourceDictionary()["DataTemplateKey"];But I get the same result either way.
-
Daniel Breaux 1 Reputation point
2022-08-31T14:01:10.55+00:00 This is a link to a sample I put together if you want to review that as well.
-
Daniel Breaux 1 Reputation point
2022-09-07T13:07:29.787+00:00 @Roy Li - MSFT Any Updates or thoughts on this?
-
Roy Li - MSFT 31,766 Reputation points • Microsoft Vendor
2022-09-08T03:12:51.787+00:00 @Daniel Breaux I've asked other engineers about this issue via our internal channel. There might be some time delay.
-
Daniel Breaux 1 Reputation point
2022-11-08T15:18:38.97+00:00 @Roy Li - MSFT Any updates on this?
Sign in to comment