0

I'm getting an issue I don't understand. I have a Button with an image defined with the following code :

<Button Image="SearchFilterIcon.png"
              Grid.Row="0"
              Grid.Column="1"
              Clicked="OnButtonFilterClicked" />

This works well on Android. The image is displayed on my button but when I launch the Windows Phone application, I get a XamlParseException which says that : No Property of name Image found.

How is it possible? The Button widget isn't the same on Android and Windows Phone?

Alexandre D.
  • 711
  • 1
  • 9
  • 28
  • I'm not able to reproduce this issue on windows-phone. could you please take the time to report it ? Thanks – Stephane Delcroix Oct 27 '14 at 11:18
  • Hi Alexandre, as noted from @StephaneDelcroix, there was a typo in my XAML when I re-checked. Can I ask that you could reproduce a small example and send to me on email so I can investigate your particular issue and hopefully get a correct resolution for you as there may be some other issue you had experienced? My contact details are on my Profile. Thanks. – Pete Oct 27 '14 at 14:56

4 Answers4

1

If you do the following:-

Button objButton1 = new Button();
objButton1.Image = (FileImageSource)ImageSource.FromFile("testImage1.png");
objStackLayout.Children.Add(objButton1);
this.Content = objStackLayout;

Then it will work (via code-behind).

The Button control always had the Image property, even in Xamarin.Forms v1.2.2x, so this is not a new property introduced and nothing to do with having the latest packages installed.

As a workaround perhaps you should consider giving the XAML Button a x:Name as in:-

<Button x:Name="myButton1"/>

And then assign the image from code-behind:-

myButton1.Image = (FileImageSource)ImageSource.FromFile("testImage1.png");

Update 1

This was a case of very old libraries being used (v1.0.6186). Once the project is reupdated to the latest binaries for v1.2.3x, then this works fine.

Pete
  • 4,746
  • 2
  • 15
  • 21
  • Works like a charm ! As you said, it's likely a bug with the XAML. Assigning the ImageSource on the code behind solved my problem. Thanks you Pete. That said, I solved my problem before by using the ImageButton of the Xamarin Forms Labs : https://github.com/XLabs/Xamarin-Forms-Labs/wiki/ImageButton – Alexandre D. Oct 16 '14 at 12:24
  • "The Button control always had the Image property" not true – Stephane Delcroix Oct 16 '14 at 12:48
  • I'm not saying **always** in my answer - I am referring to a **specific** version from. In **Xamarin.Forms.Core** **v1.2.2x**, navigating to the **Button** **View**, it does indeed show `Image` as a property returning a `FileImageSource`. I did check the v1.2.2x binary to confirm this. – Pete Oct 16 '14 at 13:01
  • @Pete I can't repro this on WP. Could you open a bugzilla ticket with your test code ? Thanks. – Stephane Delcroix Oct 27 '14 at 11:19
  • either there's a copy/paste issue, or the issue is a missing 't' in `Buton` "Position 17:6. Type Buton not found" – Stephane Delcroix Oct 27 '14 at 11:20
  • @StephaneDelcroix - Your right. I relooked at the XAML and I still had the version that 'was not working' commented out. It did indeed only have one 't' as in 'Buton' - so typo there. It does work on my example if I put in the correct tag of 'Button'. I'm going to message Alexandre for more information and a test project to try and get a version from him to reproduce the error and investigate his scenario. Afterwards I'll re-update things here. – Pete Oct 27 '14 at 14:54
  • so at least I'm not totally insane – Stephane Delcroix Oct 27 '14 at 15:13
  • confirmed your not insane :-) - Visual Studio always shows underlines for any type and lists lots of errors in the XAML editor, so I just closed the .xaml file and thought nothing of it. I would had expected Type checking at least done on compile though, but obviously its not and only done at runtime. – Pete Oct 27 '14 at 15:23
  • I've taken out my XAML part of the answer as it was a typo in my xaml that wasn't flagged by the Xamarin Compiler and allowed the application to still launch and trip up at runtime. Once I receive more info from @AlexandreD then I can further investigate his particular issue in more depth and re-update the answer. – Pete Oct 28 '14 at 13:41
  • Sorry the late guys. I tried to create a little project to reproduce the problem. The thing is I'm getting the error on Android too this time. And WP launch a TargetInvocationException. – Alexandre D. Oct 29 '14 at 15:59
  • If you want I will take a look at both issues? – Pete Oct 29 '14 at 16:08
  • Yes, why not. It would be great ! I send you the project. I thing the problem comes from me but not sure. – Alexandre D. Oct 29 '14 at 16:10
  • 1
    I didn't get the exception that you last mention, but was able to resolve the issue in the files you sent. You was still referencing v1.0.6186 of the Xamarin libraries. I've removed, and reupdated all projects to the latest v1.2.3x versions. Check your email for files. I've tested it on both Android and WindowsPhone. – Pete Oct 29 '14 at 17:23
  • Updated answer (see *Update 1*) – Pete Oct 29 '14 at 17:26
  • It works well. Good job Pete ! However, can you explain me how did you change the Xamarin libraries versions via Visual Studio? – Alexandre D. Oct 30 '14 at 08:23
  • 1
    Go Tools > NuGet Manager > Manage NuGet Packages. Then in this case I had to uninstall, so remove Xamarin.Forms from your project from here, and let it uninstall, and then go back in, find Xamarin.Forms from the Online tab, and click to Install, and select the projects to install it to. Note - you will not have to do this again. To update in future, still go to Manage NuGet Packages, but now in the Updates tab, you will see 'Update' against any NuGet package that there are updates for. – Pete Oct 30 '14 at 13:30
  • Perfect explanation. Thanks you for your help Pete. – Alexandre D. Oct 30 '14 at 15:03
0

The Button.Image is available on WP, just as it is on iOS and Android. You probably don't have the latest nuget (1.2.3) installed for WP, or you have multiple versions installed.

Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
-1

The buttons in XAML for Windows Phone simply do not provide an Image property. Thus, you cannot add an image to the button as the API doesn't support this. What you have to do is to create a control template that contains the text and the image.

Button documentation

Stephan
  • 1,791
  • 1
  • 27
  • 51
  • Thanks for your reply. The thing is I am using the Xamarin.Forms technology so it is not the Windows Phone Button but the Xamarin.Forms button which provides an Image property as it is shown at the following URL : http://iosapi.xamarin.com/?link=P%3aXamarin.Forms.Button.Image – Alexandre D. Oct 16 '14 at 07:53
  • 1
    Yep. But as far as I understand (but I may be mistaken here as I'm not 100% sure though) Xamarin.Forms is to some extend compatible with Windows Phone XAML but it doesn't run on Windows Phone like on Android, MacOS and iOS. In the documentation that you refer to in the link you can see that you can just select those three systems in the drop down on the top left, not Windows Phone. So I think Windows Phone XAML ~ Xamarin.Forms but they are not equal. So you can run most of Xamarin.Forms code on Windows Phone but not all. Please correct me if I'm wrong. – Stephan Oct 16 '14 at 07:58
  • Your reasoning seems totally logical. I didn't think about that and I think you are right. This is why I' am getting compatible problems between my Android app and my Windows Phone app. So if I well understand, to developp a cross-platform application with the Xamarin.Forms technology which was compatible with Windows Phone, I have to write XAML code which works on a normal Windows Phone app, isn't it? Concerning the control template you spoke about, will this control be used on each platform? ie, I don't have to use the Image property of the Xamarin.Forms button for Android and iOS? – Alexandre D. Oct 16 '14 at 08:08
  • Yes, you have to write XAML code that works on Windows Phone. Regarding the control template, I honestly don't know. A quick Google search didn't provide me any answer on that and currently I don't have Xamarin installed to test it out.. – Stephan Oct 16 '14 at 08:15
-1

Try some like:

<Button Click="OnButtonFilterClicked">
                    <Button.Background>
                        <ImageBrush Stretch="Fill" ImageSource="/SearchFilterIcon.png"/>
                    </Button.Background>
                </Button>

instead of "Clicked" and "image" properties.

iakwvina
  • 83
  • 1
  • 8