0

I am new to Xamarin Forms and C# too. Kindly help me to get rid of the above issue.

I am getting

Xamarin.Forms.Xaml.XamlParseException

while I am trying to add the string format of my selected image source to CandidateDetails.cs.

The pages are as follow:

CandidateDetails.cs

public event PropertyChangedEventHandler PropertyChanged; private string imageBase64; public string ImageBase64 { get { return imageBase64; } set { imageBase64 = value; OnPropertyChanged("ImageBase64"); CandImage = Xamarin.Forms.ImageSource.FromStream( () => new MemoryStream(Convert.FromBase64String(imageBase64))); } } private Xamarin.Forms.ImageSource _candImage; public Xamarin.Forms.ImageSource CandImage { get { return _candImage; } set { _candImage = value; OnPropertyChanged("CandImage"); } } public string _candName; public string CandName { get { return _candName; } set { if (_candName == value) return; _candName = value; OnPropertyChanged("CandName"); } } public string _candInst; public string CandInst { get { return _candInst; } set { if (_candInst == value) return; _candInst = value; OnPropertyChanged("CandInst"); } } public string _candEmailId; public string CandEmailId { get { return _candEmailId; } set { if (_candEmailId == value) return; _candEmailId = value; OnPropertyChanged("CandEmailId"); } } public string _candMob; public string CandMob { get { return _candMob; } set { if (_candMob == value) return; _candMob = value; OnPropertyChanged("CandMob"); } } private void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } 

CandidateDetailsModalPage.xaml.cs

public partial class CandidateDetailsModalPage : ContentPage { Stream input; string imageAsString; public static ObservableCollection<CandidateDetails> _candidate = new ObservableCollection<CandidateDetails>(); async void OnDoneClicked(object sender, System.EventArgs e) { _candidate.Add(new CandidateDetails { CandName = (string)candNameEntry.Text, CandEmailId = (string)candEmailId.Text, CandMob = (string)candMobNumber.Text, ImageBase64 = imageAsString }); await Navigation.PopModalAsync(); } public CandidateDetailsModalPage() { InitializeComponent(); pickPhoto.Clicked += async (sender, args) => { if (!CrossMedia.Current.IsPickPhotoSupported) { await DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK"); return; } var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions { PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium }); if (file == null) return; input = file.GetStream(); byte[] buffer = new byte[16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } imageAsString = Convert.ToBase64String(ms.ToArray()); } //image.Source = ImageSource.FromStream(() => new MemoryStream(Convert.FromBase64String(imageAsString))); image.Source = ImageSource.FromStream(() => { var stream = file.GetStream(); file.Dispose(); return stream; }); }; } } 

CandidateDetailsModalPage.xaml

<StackLayout Orientation="Vertical" BackgroundColor="#3B4371" Padding="0"> <Button Text="Done" Clicked="OnDoneClicked" /> <ic:CircleImage x:Name="image"/> <Button x:Name="pickPhoto" Text="Pick Photo"/> <StackLayout VerticalOptions="FillAndExpand"> <Entry x:Name="candNameEntry" Placeholder="Candidate Name" /> <Entry x:Name="candInst" Placeholder="Candidate Institution / Party"/> <Entry x:Name="candEmailId" Placeholder="Candidate Email Id" /> <Entry x:Name="candMobNumber" Placeholder="Candidate Mobile Number"/> </StackLayout> </StackLayout> 

The following is the backend of the xaml page where I am displaying the data CandidateDisplayPage.xaml.cs

candidateListView.ItemsSource = CandidateDetailsModalPage._candidate; 

CandidateDisplayPage

<ListView x:Name="candidateListView"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Orientation="Horizontal"> <Image x:Name="candImage" ImageSource="{Binding Path=CandImage}"/> <Label Text="{Binding CandName}" x:Name="candName"/> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> 
8
  • 1
    A XamlParseException is an error in your XAML, not C#. Please post the relevant XAML. Commented Mar 6, 2017 at 15:39
  • Try removing "Path=" from your binding expression, it's unnecessary. Otherwise, see developer.xamarin.com/guides/xamarin-forms/xaml/xamlc for how to enable XAML compilation which should help narrow down the cause of the error. Commented Mar 6, 2017 at 17:16
  • @Jason The error Xamarin.Forms.Xaml.XamlParseException is being thrown during runtime when I hit the done button. This error occured when I tried adding ImageSource to my ObservableCollection. Earlier when I was working with just text entries the code was working fine. Commented Mar 6, 2017 at 17:43
  • as I suggested before, enabling XAML compilation will probably help you narrow this down. Commented Mar 6, 2017 at 18:24
  • 1
    @AjayNair the property it's not ImageSource but just Source change this in the ListView ItemTemplate. Commented Mar 6, 2017 at 18:52

1 Answer 1

1

You say in a comment (that should have been part of the original report):

This is the error that's causing the exception Cannot assign property "ImageSource": Property does not exists, or is not assignable, or mismatching type between value and property

Indeed, Image doesn't have an ImageSource property, but instead a Source property of type ImageSource.

public ImageSource Source { get; set; } 

So your Xaml should look like

Image x:Name="candImage" Source="{Binding Path=CandImage}"/> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.