question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] ImageResourceConverter

See original GitHub issue

ImageResourceConverter

  • Proposed
  • Prototype
  • Implementation
    • iOS Support
    • Android Support
    • macOS Support
    • Windows Support
  • Unit Tests
  • Sample
  • Documentation

Summary

Converts embedded image resource ID to it ImageSource

Detailed Design

ImageResourceConverter.shared.cs

public class ImageResourceConverter : IValueConverter
{
  public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture)
  {
	if (value == null)
		return null;

	if (value is not string imageId)
		throw new ArgumentException("Value is not a string", nameof(value));

	return ImageSource.FromResource(imageId, Application.Current.GetType().GetTypeInfo().Assembly);
  }

  public object ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture) => throw new NotImplementedException();
}

Usage Syntax

XAML Usage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             xmlns:myEnum="MyLittleApp.Models"
             x:Class="MyLittleApp.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <xct: ImageResourceConverter x:Key="ImageResourceConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout>

        <Image Source="{Binding ImageResource, Converter={StaticResource ImageResourceConverter}}" />

    </StackLayout>
</ContentPage>

C# Usage

class MyPage : ContentPage
{
  public MyPage()
  {
    Content = new StackLayout
    {
      Children = 
      {
          new Image().Bind(Image.SourceProperty, nameof(ViewModel.ImageResource), converter: new ImageResourceConverter()),
      };
    }
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
CalvinAllencommented, Nov 22, 2021

Ah, gotcha. Okay, I’ll pivot to the sample app instead 😃

1reaction
brminnickcommented, Nov 22, 2021

My concern is that using Assembly.GetCallingAssembly() could break functionality for folks using multiple csproj files, like so:

solution
│
└───MyMauiApp.csproj
    │   CompanyLogo.png
│   
└───MyLoginPage.csproj

When using Assembly.GetCallingAssembly(),MyLoginPageProject.LoginPage would fail, because ImageResourceConverter would be searching for CompanyLogo.png in the wrong assembly.

You may need to mock-out Application for your tests to get the unit tests working.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CommunityToolkit/docs/maui/converters/image-resource- ...
The ImageResourceConverter is a converter that converts embedded image resource ID to its ImageSource. An embedded image resource is when an ...
Read more >
ImageResourceConverter - .NET MAUI Community Toolkit
The ImageResourceConverter is a converter that converts embedded image resource ID to its ImageSource.
Read more >
New Feature Proposals
NET MAUI developer easier - New Feature Proposals · CommunityToolkit/Maui. ... [Proposal] ImageResource Markup Extension. 1 of 10 tasks.
Read more >
Load Image Resource on ViewAdapter - java
I have a ReciclerView which is using an Adapter called "SimpleItemRecyclerViewAdapter". Each item in the ReciclerView is in the project's ...
Read more >
Providing the image resource - 4Js
This search procedure using a proposal of file extensions was implemented to allow different type of front-ends to pass the type of image...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found