0

There should be a bug in DotNetNativeToolChain. In project_name.UWP.csproj file, I have

<UseDotNetNativeToolchain>true</UseDotNetNativeToolchain>

In xaml, I have

<Image x:Name="testimage"></Image>

In code behind file, I have

testimage.Source = ImageSource.FromResource("project_name.testimage1.png");

testimage1.png is entered into project_name.csproj as

<EmbeddedResource Include="testimage1.png" />

Project can compile but crash with an error "Unhandled exception at 0x(Windows.UI.Xaml.dll) in .exe. 0xC000027B: An application-internal exception has occurred."

If I comment out the "UseDotNetNativeToolchain" in project_name.UWP.csproj file, the application runs smoothly.

Tek Mun
  • 97
  • 9
  • Did it work on iOS and Android device ? Your code works fine on my side . The error maybe caused by codes. About Embedded Images you can check https://stackoverflow.com/questions/58969480/how-to-load-the-online-pdf-files-in-xamarin-webview/58974415#58974415 – Lucas Zhang Nov 27 '19 at 07:43
  • Yes. It works on iOS and Android devices. It works on UWP as well if I commented out the UseDotNetNativeToolchain. – Tek Mun Nov 27 '19 at 07:54
  • Lucas, there is no link given. Thanks. – Tek Mun Nov 28 '19 at 10:49
  • https://forums.xamarin.com/discussion/57179/uwp-immediate-crash-in-release-mode – Lucas Zhang Nov 28 '19 at 10:50
  • The issue is due to Reflection in DotNetNativeToolChain for UWP. Eventually, I solved it writing a function shown below. – Tek Mun Nov 29 '19 at 23:47

1 Answers1

0
public ImageSource GetImageSource(string resource)
{
  ImageSource imagesource = null;
  Assembly assembly = GetType().GetTypeInfo().Assembly;
  MemoryStream ms = new MemoryStream();
  using (Stream stream = assembly.GetManifestResourceStream(resource))
  {
    if (stream != null)
    {
      stream.CopyTo(ms);
      imagesource = ImageSource.FromStream(() => new MemoryStream(ms.ToArray()));
    }
  }
  return imagesource;
}

In the code, I use

testimage.Source = GetImageSource("project_name.testimage1.png");
Tek Mun
  • 97
  • 9