[Proposal] ImageResourceConverter
See original GitHub issueImageResourceConverter
- 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:
- Created 2 years ago
- Comments:16 (11 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah, gotcha. Okay, I’ll pivot to the sample app instead 😃
My concern is that using
Assembly.GetCallingAssembly()
could break functionality for folks using multiplecsproj
files, like so:When using
Assembly.GetCallingAssembly()
,MyLoginPageProject.LoginPage
would fail, becauseImageResourceConverter
would be searching forCompanyLogo.png
in the wrong assembly.You may need to mock-out
Application
for your tests to get the unit tests working.