diff --git a/doc/assets/appdata-linkbase-solution-explorer.png b/doc/assets/appdata-linkbase-solution-explorer.png new file mode 100644 index 000000000..1a8bde000 Binary files /dev/null and b/doc/assets/appdata-linkbase-solution-explorer.png differ diff --git a/doc/external/StorageFile.md b/doc/external/StorageFile.md index ea95c5aae..1d84b610a 100644 --- a/doc/external/StorageFile.md +++ b/doc/external/StorageFile.md @@ -4,6 +4,8 @@ uid: Uno.Recipes.StorageFile # Loading App Data +> **UnoFeatures:** `Serialization` (add to `` in your `.csproj`). + ## Problem You need to load app data files on all platforms. Traditional file access methods (`System.IO.File.Read*`, `EmbeddedResource`) do not work for Content files on WASM. @@ -12,15 +14,30 @@ You need to load app data files on all platforms. Traditional file access method Use the `Windows.Storage.StorageFile` API to read files from your app package. This works the same way on all Uno Platform targets, including WASM. -- Move your data files to be included as `Content` in your project, and use `StorageFile.GetFileFromApplicationUriAsync` to load them by path. +- Move your data files to be included as `Content` in your project's output directory, and use `StorageFile.GetFileFromApplicationUriAsync` to load them by path. To ensure that you will always have the latest changes included, set the appropriate Property: `CopyToOutputDirectory="PreserveNewest"`. + + ```xml + + + + ``` + +- To also display these files in your Solution Explorer, add `LinkBase="AppData"`: ```xml - + ``` -- Loading a JSON File Using StorageFile + ![VS Code Solution Explorer showing linked AppData files with 'external file links' note](../assets/appdata-linkbase-solution-explorer.png) + + > [!TIP] + > Using `LinkBase` allows multiple projects in your solution to reference the same files without duplication while keeping them synchronized across all projects. + > + > See [MSBuild LinkBase documentation](https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#linkbase) for details. + +- Loading a JSON File Using `StorageFile` ```csharp public abstract class BaseMockEndpoint(ISerializer serializer, ILogger _logger) @@ -42,6 +59,12 @@ Use the `Windows.Storage.StorageFile` API to read files from your app package. T } ``` + > [!NOTE] + > This sample utilizes `ISerializer` from Uno Extensions Serialization to deserialize the JSON content into a strongly-typed object. + > See the [Serialization documentation](xref:Uno.Recipes.Serialization) for more details and the [Walkthrough: Serialize JSON with Source Generators](xref:Uno.Extensions.Serialization.HowTo) for guidance on setting it up. + > [!TIP] + > Alternatively to `StorageFile`, you can use [`IStorage` from Uno Extensions Storage](xref:Uno.Extensions.Storage.Overview), which not only allows you to replace it with only one simple call to `IStorage.ReadPackageFileAsync(string path)` to get the same result with build-in serialization support, but also to provide your own `ISerializer` implementation if needed. + - Using `LoadData` in `MockRecipeEndpoints` ```csharp diff --git a/global.json b/global.json index 7f65a2cc1..63858d527 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "msbuild-sdks": { - "Uno.Sdk": "6.3.28" + "Uno.Sdk": "6.4.42" }, "sdk": { "allowPrerelease": false