Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added doc/assets/appdata-linkbase-solution-explorer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 26 additions & 3 deletions doc/external/StorageFile.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ uid: Uno.Recipes.StorageFile

# Loading App Data

> **UnoFeatures:** `Serialization` (add to `<UnoFeatures>` 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.
Expand All @@ -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
<ItemGroup>
<Content Include="AppData\*.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
```

- To also display these files in your Solution Explorer, add `LinkBase="AppData"`:

```xml
<ItemGroup>
<Content Include="AppData\*.json" />
<Content Include="AppData\*.json" CopyToOutputDirectory="PreserveNewest" LinkBase="AppData" />
</ItemGroup>
```

- 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<BaseMockEndpoint> _logger)
Expand All @@ -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<TData>(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
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"msbuild-sdks": {
"Uno.Sdk": "6.3.28"
"Uno.Sdk": "6.4.42"
},
"sdk": {
"allowPrerelease": false
Expand Down
Loading