paulpatarinski / Xamarin.Forms.Plugins

Xamarin Forms Plugins
http://paulpatarinski.github.io/Xamarin.Forms.Plugins
MIT License
276 stars 141 forks source link

SVGImage not compatible work with Shared Projects #72

Open anujb opened 8 years ago

anujb commented 8 years ago

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

paulpatarinski commented 8 years ago

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

frijj2k commented 8 years ago

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

frijj2k commented 8 years ago

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!

ghost commented 8 years ago

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

frijj2k commented 8 years ago

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

ghost commented 8 years ago

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?

frijj2k commented 8 years ago

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!

ghost commented 8 years ago

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?

xinmeng1 commented 8 years ago

+1

xinmeng1 commented 8 years ago

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 commented 8 years ago

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~

gmenezesg commented 8 years ago

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.

xjrcode commented 8 years ago

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.

CNIAngel commented 8 years ago

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?