Image.Source with OnPlatform in Xaml

KevinFordKevinFord USUniversity, Certified XTC Partners ✭✭✭
edited June 2014 in Xamarin.Forms

I have been having a Dickens of a time setting the source property on the image in conjunction with Xaml. I'm trying to use the guide here developer.xamarin.com/guides/cross-platform/xamarin-forms/Xaml-for-Xamarin-forms/essential_xaml_syntax/ but it blows up every time.

        <Image>
            <Image.Source>
                <OnPlatform x:TypeArguments="ImageSource">
                    <OnPlatform.iOS>icon_twitter.png</OnPlatform.iOS>
                    <OnPlatform.Android>icon_twitter.png</OnPlatform.Android>
                    <OnPlatform.WinPhone>Images/icon_twitter.png</OnPlatform.WinPhone>
                </OnPlatform>
            </Image.Source>
        </Image>

Works fine setting it through code. Any ideas?

Best Answer

Answers

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    Could you define "it blows up" ?

    There could be a type mismatch as the TypeArguments is ImageSource, but the conversion from string returns a FileImageSource

    You should probably be able to work around this by using FileImageSource as TypeArguments. If that's not enough, bypass the type conversion too by doing <OnPlatform.iOS><FileImageSource File="icon_twitter.png"/></OnPlatform.iOS/>

  • KevinFordKevinFord USUniversity, Certified XTC Partners ✭✭✭

    @StephaneDelcroix‌ ,
    Are you saying that "blows up" isn't as good as an error message? :)

    This is the error I receive:

    System.Reflection.TargetInvocationException was unhandled
    Message: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll
    Additional information: Exception has been thrown by the target of an invocation.

    I tried the type as a FileImageSource, that didn't solve it. When I don't use the OnPlatform syntax in Xaml and just pass a string the Source property uses the ImageSourceConverter to create a ImageSource of the right type. When I use the OnPlatform syntax in Xaml the ImageSourceConverter doesn't get called.

  • KevinFordKevinFord USUniversity, Certified XTC Partners ✭✭✭

    @StephaneDelcroix‌,
    That fixed it.

    For anyone reading the quotes in the answer should all be single quotes of course, but looks like they got doubled up when Stephane pasted in the answer. :)

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    @KevinFord the quotes are doubled because I copied that from an unit test where the xaml is a @... string, and the quote have to be doubled to be escaped, that's all.

  • KevinFordKevinFord USUniversity, Certified XTC Partners ✭✭✭

    @StephaneDelcroix‌,
    Makes sense. Thanks for your help!

  • kumardsinghkumardsingh INMember ✭✭
    edited June 2016

    See Below code

  • kumardsinghkumardsingh INMember ✭✭
                 <Image>
             <Image.Source>
               <OnPlatform  x:TypeArguments="ImageSource">
               <OnPlatform.iOS>icon.png</OnPlatform.iOS>
               <OnPlatform.Android>icon.png</OnPlatform.Android>
               <OnPlatform.WinPhone>Assets/SmallLogo.scale-240_transparent.png</OnPlatform.WinPhone>
               </OnPlatform>
             </Image.Source>
           </Image>
    
  • KevinFordKevinFord USUniversity, Certified XTC Partners ✭✭✭

    @kumardsingh said:
    See Below code

    Thanks. The question had to do with a bug that existed in the 1.0 version. It's been fixed.

Sign In or Register to comment.