Bug 60731 - ImageSource.FromResource() does not work in UWP
Summary: ImageSource.FromResource() does not work in UWP
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.5.0
Hardware: PC Windows
: Normal major
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-11-17 11:50 UTC by José Antonio
Modified: 2018-01-17 15:17 UTC (History)
7 users (show)

Tags: build 16299, uwp, targetexception, ac
Is this bug a regression?: ---
Last known good build:


Attachments
Testcase (280.88 KB, application/x-zip-compressed)
2017-11-20 09:22 UTC, José Antonio
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 60731 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description José Antonio 2017-11-17 11:50:49 UTC
# Steps to reproduce
1. Create an empty "Cross Platform App (Xamarin)" Project and name it 'ErrorTest'
2. Update all NuGet packages to their most recent versions.
3. Copy the Xamarin's file 'icon.png' from Android Project under ./Resources/Drawable to the UWP Project under its root, and set the build action to 'Embedded Resource' (any other file with the same name would do)
4. Modify the default MainPage.xaml.cs file and add the lines:
      // See https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Embedded_Images
      var embeddedImage = new Image { Source = ImageSource.FromResource("ErrorTest.icon.png") }; 
   right below 'InitializeComponent()', in creator method 'public MainPage()'
5. Set a breakpoint in the line just added.
6. Run the Android project and see how it runs just fine.
7. Run the UWP project and see how an exception occurs:
System.Reflection.TargetException: Exception of type 'System.Reflection.TargetException' was thrown.
   at System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException()
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Xamarin.Forms.ImageSource.FromResource(String resource, Assembly sourceAssembly)
   at ErrorTest.MainPage..ctor()
   at ErrorTest.App..ctor()
   at ErrorTest.UWP.MainPage..ctor()
   at ErrorTest.UWP.ErrorTest_UWP_XamlTypeInfo.XamlTypeInfoProvider.Activate_4_MainPage()
   at ErrorTest.UWP.ErrorTest_UWP_XamlTypeInfo.XamlUserType.ActivateInstance()

# Expected behavior
All platforms (Android & UWP) should load the resource using the same name

# Actual behavior
UWP version just crashes.

# Supplemental info (logs, images, videos)
https://forums.xamarin.com/discussion/106554/is-imagesource-fromresource-working-for-uwp
https://github.com/akgulebubekir/Xamarin.Forms.DataGrid/issues/50

# Test environment (full version information)
Windows 10 (Version 1709) and Visual Studio Community 2017 (Version 15.4.4)
Tested flaw with Xamarin.Forms versions 2.4.0.38779 through 2.5.0.91635.
Comment 1 Paul DiPietro [MSFT] 2017-11-19 03:12:08 UTC
What OS build is your machine, specifically? An app does not crash for me (copy the file, change the build action, add the line of code) but I'm on 17035 at the moment. Any other info about your environment would be helpful as well. Maybe even attaching your own project that does crash to see if any small differences exist between projects?
Comment 2 José Antonio 2017-11-20 09:13:44 UTC
We are running Windows 10 v.1709 build 16299.64. The Project has been made using Visual Studio Community 2017 version 15.4.4. Both the OS and Visual Studio are in Spanish language.

Other user that has been able to reproduce the issue too (as he tolds in https://github.com/akgulebubekir/Xamarin.Forms.DataGrid/issues/50 ) is probably also using a non-English OS (my guess), since he is from Istambul.

Please see the attached Project/Solution. This was the 2nd Project made following the former steps, so it was clearly reproducible on my machine.

Additional info after some tests:

If the UWP is made using the default options, i.e.:
  * target: Windows 10 Fall Creators Update,
  * mínimum: Windows 10 November Update
everything works fine.

However if the mínimum version is changed to Windows 10 Fall Creators Update (target=mínimum), the exception appears.
Comment 3 José Antonio 2017-11-20 09:22:33 UTC
Created attachment 25758 [details]
Testcase

Zipped file of a Solution containing a single Cross Platform App (Xamarin) Project.
Comment 4 Paul DiPietro [MSFT] 2017-11-20 15:46:01 UTC
Thank you for the extra info as that's pretty important for reproducing this. It also only seems to affect shared projects, I think. I'm not 100% sure if this is explicitly on the Forms side of things, but I'll mark it as confirmed for now and it may be something that simply needs forwarding to the UWP team. In the meantime I would simply suggest lowering your minimum target.
Comment 5 siggiullrich 2017-11-27 12:33:42 UTC
But using .NET Standard 2.0 in some ClassLibraries don't allow me to lower my minimum target. Unfortunately I'm stuck here with this bug :(
Comment 6 johnmangam 2017-12-02 13:56:39 UTC
I'm also stuck at this point. Thank you for doing the needful.
Comment 7 DesenEnterprises 2017-12-23 15:47:07 UTC
Not sure if this is related...

I am trying to use .net standard instead of PCL since Microsoft is depreciating PCL.

To resolve IServiceProvider not working I had to install system.ComponentModel v4.3.0

Installing system.ComponentModel v4.3.0 broke everything... had to uninstall it. Now I can run my app without error as long as I don't try to embed images.

I can't use embedded images in a .net standard.

Stuck as well.
Comment 8 vrykov 2018-01-15 13:16:14 UTC
There is a workaround: use an overload of ImageSource.FromResource method like this:

ImageSource.FromResource(resourceName, Assembly.GetExecutingAssembly());

Source:

https://forums.xamarin.com/discussion/106554/is-imagesource-fromresource-working-for-uwp

Works for me in net standard 2.0. In addition, Assembly.GetExecutingAssembly() can be cached as static field reducing costs of method calls.
Comment 9 Michel Moorlag 2018-01-17 14:58:32 UTC
I have the same issue here and I can confirm that the work around from vrykov does work for me.
Shared lib needs to be higher then .net standard 1.4 because you will be needing the Assembly.GetExecutingAssembly method for this work around.
Comment 10 Michel Moorlag 2018-01-17 15:17:07 UTC
An other work around (for me) is to use ffimageloading for all embedded images which support embedded resources out of the box. No need for ImageSource.FromResource() anymore (in my case).