lambkin
lambkin

Reputation: 71

WinUI 3 calling ApplicationData.Current() throws System.TypeInitializationException

I am trying to convert a windows desktop application from WinUI 2 to WinUI 3. However, I am having a problem with the Windows.Storage.ApplicationData class. When I call ApplicationData.Current (so I can access a file in the app's local data store or to get a local setting) a System.TypeInitializationException exception is thrown.

The following screenshot shows the problem:

System.TypeInitializationException

(This is a default application that I created in Visual Studio 2022 using the project template "Blank App, Packaged (WinUI 3 in Desktop)", the only change I made was to add the line to get the current ApplicationData object in the App class's OnLaunched() method:

var appData = Windows.Storage.ApplicationData.Current;)

Any idea what I am doing wrong here? I think maybe there is a trust or cababilities issue, or I should use other methods to get the app's local data folder or settings for a WinUI 3 desktop application.

Upvotes: 6

Views: 2784

Answers (2)

Elden
Elden

Reputation: 81

I encountered the same problem recently. No matter where I made the call in my WinUI 3 application, whenever I invoked a call to the ApplicationData API, I would always encounter the same exact exception (System.TypeInitializationException with an inner COMException). I failed to find an answer in the API documentation, particularly where one would hope to find such an answer. (If it's there, it's buried or perhaps easily overlooked by someone who is not as familiar with .NET programming, such as myself.) The following line of code always threw an exception for me:

ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;

The solution that worked for me was something I learned from a response to an issue on the WindowsAppSDK GitHub repository. To quote the information I found most relevant to the issue I was having:

A client that is not running in an App Container must open ApplicationData using this API: https://learn.microsoft.com/en-us/uwp/api/Windows.Management.Core.ApplicationDataManager?view=winrt-22000, while a client that is running in an App Container must open ApplicationData using this API instead: https://learn.microsoft.com/en-us/uwp/api/windows.storage.applicationdata?view=winrt-22000

Basically, I learned that I needed to open ApplicationData using the ApplicationDataManager class. As noted from the GitHub link, the solution I found was to replace the offending line of code with the following:

var localSettings = ApplicationDataManager.CreateForPackageFamily(Package.Current.Id.FamilyName).LocalSettings;

From there I can save and load app settings as usual, which is suitably explained here:

localSettings.Values["test setting"] = "a device specific setting";
String localValue = localSettings.Values["test setting"] as string;

Hopefully this helps anyone who finds themselves facing the same issue.

Upvotes: 8

lambkin
lambkin

Reputation: 71

Scratch that, the problem only happens if in the debugger I put a breakpoint on the "ApplicationData.Current" line. Very weird. I think I am having the same problem as here:

Getting values of ApplicationData.Current.LocalSettings in static context

Upvotes: 1

Related Questions