Xamarin.Forms.Plugins icon indicating copy to clipboard operation
Xamarin.Forms.Plugins copied to clipboard

SVGImage not compatible work with Shared Projects

Open anujb opened this issue 9 years ago • 14 comments

In fact, Build Action is Embedded Resource is defined :-)

System.Exception: Error retrieving ic_call_black_24px.svg make sure Build Action is Embedded Resource at SVG.Forms.Plugin.iOS.SvgImageRenderer.OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs1 e) [0x00046] in <filename unknown>:0 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].SetElement (Xamarin.Forms.Platform.iOS.TElement element) [0x00118] in :0 at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00000] in :0 at Xamarin.Forms.Platform.iOS.RendererFactory.GetRenderer (Xamarin.Forms.VisualElement view) [0x0001b] in :0 at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) [0x00006] in :0 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () [0x0001f] in :0

anujb avatar Nov 06 '15 00:11 anujb

@anujb Can you throw a quick project together that demonstrates the issue? It would help speed up the fix

paulpatarinski avatar Jan 04 '16 03:01 paulpatarinski

Same problem here... I will see if I can whip up a quick sample project to demonstrate this tomorrow...

frijj2k avatar Jan 05 '16 00:01 frijj2k

OK, Im using Xamarin Studio 5.10.1 (build 6) with Xamarin.Android 6.0.0.34 and Xamarin.iOS 9.2.1.54.

I create a Xamarin.Forms App (called SvgTest for this test) from the 'Create New Solution' menu, then I create a folder under the SvgTest shared project called 'Images' which contains one SVG called 'github10.svg' that has its Build Action set to 'EmbeddedResource'.

I add your plugin to both Android and iOS projects via the NuGet package manager and change the main application source in 'SvgTest.cs' (under shared project) to the following:

using System;
using System.Reflection;

using Xamarin.Forms;
using SVG.Forms.Plugin.Abstractions;

namespace SvgTest
{
    public class App : Application
    {
        public App ()
        {
            SvgImage img = new SvgImage { 
                SvgPath = "SvgTest.Images.github10.svg",
                SvgAssembly = typeof(App).GetTypeInfo().Assembly, 
                HeightRequest = 75,
                WidthRequest = 75
            };

            // The root page of your application
            MainPage = new ContentPage {
                Content = new StackLayout {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        img,
                        new Label {
                            XAlign = TextAlignment.Center,
                            Text = "Welcome to Xamarin Forms!"
                        }
                    }
                }
            };
        }

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }
    }
}

I also add SvgImageRenderer.Init(); to both MainActivity.cs and AppDelegate.cs in the Android and iOS projects respectively, just after the Forms.Init() method.

Now when I try to run the app on my Android device, I get the following exception...

System.AggregateException: One or more errors occurred. ---> System.Exception: Error retrieving SvgTest.Images.github10.svg make sure Build Action is Embedded Resource
  at SVG.Forms.Plugin.Droid.SvgImageRenderer+<<OnElementChanged>b__3_0>d.MoveNext () [0x00042] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at at (wrapper dynamic-method) System.Object:4c883487-a828-4fb6-a2c8-c077b76f58a2 (intptr,intptr)
  at at (wrapper native-to-managed) System.Object:4c883487-a828-4fb6-a2c8-c077b76f58a2 (intptr,intptr)

Any help would be greatly appreciated.

Thanks!

UPDATE

If I copy the Images folder that contains my SVG to the Resources folder under SvgTest.Droid instead of the shared folder and change the SvgPath in the above code to: SvgPath = "SvgTest.Droid.Images.github10.svg" the exception goes away and the app runs just fine but I still do not see the image!?!? :-/

Also, is there a way to share the resources so I dont need to create copies of the same files in each of the platform projects?

Thanks!

frijj2k avatar Jan 05 '16 09:01 frijj2k

Not sure if still relevant but you need to set the build action for your svg file to "embedded resource".

ghost avatar Feb 24 '16 19:02 ghost

Thanks for the advice but I have already set the build type to embedded resource so it must be something else.

frijj2k avatar Feb 25 '16 09:02 frijj2k

I came across your comment just because I had the same problem. And from your exception message I read that the setting was missing and it worked for me. Did you find another way around it or do you need an example?

ghost avatar Feb 25 '16 16:02 ghost

In the end I used a different library that allowed the use of Font Awesome and Material Icons etc. Suited my needs but I would like to get to the bottom of the SVG problem so a working example would be very much appreciated if you don't mind?

Thanks!

frijj2k avatar Feb 25 '16 19:02 frijj2k

I've just set up a simple project displaying one image but I am not sure how to share it with you :) I could upload the whole solution and send you the link by mail?

ghost avatar Feb 28 '16 13:02 ghost

+1

xinmeng1 avatar Apr 01 '16 10:04 xinmeng1

I solve my problem which is the reason for this exception maybe(not sure), need set the svg image resource to Embedded Resource (for build action), in VS2015, this is set in the Property. Image of Yaktocat

See this demo https://github.com/xinmeng1/SvgPluginDemo

xinmeng1 avatar Apr 01 '16 11:04 xinmeng1

Actually, I found this problem base on this https://developer.xamarin.com/guides/ios/application_fundamentals/working_with_resources/

which tell us that the resource files should be put into the folder of project. Set it to Embedded Resource which means that put this files embed into the device~

xinmeng1 avatar Apr 01 '16 11:04 xinmeng1

I had the same issue and in my case I've found out that I had to set the BindingContext to the ViewModel in the xaml.cs page file (code behind). Setting it in the xaml the error rises.

gmenezesg avatar Apr 05 '16 12:04 gmenezesg

Even setting build action to embedded resource you have to write the resource ID "SvgTest.Images.github10.svg" in the properties pad as specified here

Now I'm getting the error "Invalid parameters to context creation" in iOS and "width and height must be > 0" in android.

xjrcode avatar Jun 06 '16 07:06 xjrcode

Ok I've been having a go at this for a few days now and I have no clue why I can't get this to work. I made a completely new Shared project, installed the NuGet package, and just added a SvgImage to the generated main content page and my app is crashing after launch.

I thought it might have been the svg files I made so I grabbed the ones from the SampleApp and it's still breaking.

I've tried putting the .svg inside the Resources/Drawable and making a folder in the main project. I set the .svg to EmbeddedResource. The IDs referring to them is correct.

Has anyone successfully gotten this package to work with Shared Project and would be willing to tell me what I'm doing wrong here?

  • EDIT - After banging my head on a wall all night I've got the SvgTest application to load a .svg file, the tiger one. Now I have to re-format the .svg files I made in InkScape so that they work as well. I only tested it in C# code and not in XAML tho so I don't know if it works there.

CNIAngel avatar Jun 18 '16 00:06 CNIAngel