Skip to content

Commit

Permalink
Merge pull request unoplatform#18600 from ajpinedam/docs/update.worki…
Browse files Browse the repository at this point in the history
…ng.with.assets

docs: update working with assets
  • Loading branch information
ajpinedam authored Oct 28, 2024
2 parents 3a628b8 + dd0be9e commit 1f23f40
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions doc/articles/features/working-with-assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ uid: Uno.Features.Assets

Uno Platform automatically processes assets from your app's project and makes them available on all platforms.

Support for the automatic generation of assets in multiple resolutions from SVG or PNG is also provided using [Uno.Resizetizer](xref:Uno.Resizetizer.GettingStarted).
Support for automatically generating assets in multiple resolutions from SVG or PNG is also provided using [Uno.Resizetizer](xref:Uno.Resizetizer.GettingStarted).

Platform-specific assets such as [`BundleResource`](https://learn.microsoft.com/xamarin/ios/user-interface/controls/image) and [`AndroidAssets`](https://learn.microsoft.com/xamarin/android/app-fundamentals/resources-in-android/android-assets) are also supported on the heads, when required.

Expand Down Expand Up @@ -49,19 +49,58 @@ See the examples below for XAML:
<Image Source="ms-appx:///[MyApp]/Assets/MyImage.png" />
```

> [!NOTE]
> For [single project solutions](https://platform.uno/docs/articles/migrating-to-single-project.html#project-structure), the `[MyApp]` qualifier is not required when explicitly qualifying the asset.
```xml
<Image Source="ms-appx:///Assets/MyImage.png" />
```

Or

```xml
<Image Source="ms-appx:///Assets/Images/MyImage.png" />
```

For Uno's latest project template where images are saved in the `Images` folder.

### Referencing an asset from code

You can also reference assets from code using the `ms-appx:///` scheme. For example:

```xaml
<Image x:Name="MyImage" />
```

```csharp
// from XAML code behind
MyImage.Source = "ms-appx:///Assets/MyImage.png";
```

or using bindings:

```csharp
// from a ViewModel
public string ImagePath { get; set ;} = "ms-appx:///Assets/MyImage.png";
```

```xaml
<Image Source="{x:Bind ImagePath}" />
```

You can also get assets directly using [StorageFile.GetFileFromApplicationUriAsync](xref:Uno.Features.FileManagement#support-for-storagefilegetfilefromapplicationuriasync).

## Qualify an asset

When developing Windows apps, developers can load different assets at runtime based on attributes that qualify their visibility in the app UX. This allows you to tailor your app to different contexts to better suit users' hardware or language preferences.
When developing Windows apps, developers can load different assets at runtime based on attributes that qualify their visibility in the app's UX. This allows you to tailor your app to different contexts to better suit users' hardware or language preferences.

For instance, such **qualifiers** can selectively load different assets depending on scale, language, theme preferences, etc. This feature is notably useful when supporting high DPI screens or the norms of differing regions.

> [!NOTE]
> To become more familiar with qualifiers, check out Microsoft's documentation for a conceptual overview of the feature:
> [Microsoft: Tailor your resources for language, scale, high contrast, and other qualifiers](https://learn.microsoft.com/windows/apps/windows-app-sdk/mrtcore/tailor-resources-lang-scale-contrast)
Uno Platform allows you to use this same feature on multiple platforms. However, a subset of those qualifiers currently has support.
Uno Platform allows you to use the same feature on multiple platforms. However, only a subset of those qualifiers currently have support.

### Table of scales

Expand Down Expand Up @@ -143,7 +182,7 @@ Sometimes, you might want to use a different asset depending on the platform. Be
| iOS | `ios` |
| Android | `android` |

Because the `custom` qualifier doesn't have any special meaning on WinUI/UWP, we have to interpret its value manually.
Because the `custom` qualifier has no special meaning on WinUI/UWP, we have to interpret its value manually.

On iOS and Android, Uno.UI's `RetargetAssets` task automatically interprets these values and excludes unsupported platforms.

Expand All @@ -169,10 +208,10 @@ On UWP, you must add the following code to your `App.cs` or `App.xaml.cs` constr

## Android: setting a custom image handler

On Android, to handle the loading of images from a remote url, the Image control has to be provided a
ImageSource.DefaultImageLoader such as the [Android Universal Image Loader](https://github.com/nostra13/Android-Universal-Image-Loader).
On Android, to handle the loading of images from a remote URL, the Image control has to be provided a
`ImageSource.DefaultImageLoader`, such as the [Android Universal Image Loader](https://github.com/nostra13/Android-Universal-Image-Loader).

This package is installed by default when using the [Uno Cross-Platform solution templates](https://marketplace.visualstudio.com/items?itemName=unoplatform.uno-platform-addin-2022). If not using the solution template, you can install the [nventive.UniversalImageLoader](https://www.nuget.org/packages/nventive.UniversalImageLoader/) NuGet package and call the following code from your application's App constructor:
This package is installed by default when using the [Uno Cross-Platform solution templates](https://marketplace.visualstudio.com/items?itemName=unoplatform.uno-platform-addin-2022). If not using the solution template. You can install the [nventive.UniversalImageLoader](https://www.nuget.org/packages/nventive.UniversalImageLoader/) NuGet package and call the following code from your application's App constructor:

```csharp
private void ConfigureUniversalImageLoader()
Expand Down

0 comments on commit 1f23f40

Please sign in to comment.