Binding in Xamarin.Forms

There is a code, the essence of which is as follows: when you click on the "Go" button, a transition to a new page should be made. This page should display an image and text. Text and image are stored in separate folders and marked as Embedded Resource. But the problem is that when you click on the button, it does not display anything on the new page.

'

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using MDivination.ViewModels;

namespace MDivination.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DivinationResult : ContentPage
    {
       
        public DivinationResult()
        {
            InitializeComponent();
            BindingContext = new MemViewModel();
            
        }
    }

}

`

The page with the binding

'

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:models="clr-namespace:MDivination.Models" 
             xmlns:viewmodels="clr-namespace:MDivination.ViewModels"
             x:DataType="models:Mem"
             x:Class="MDivination.Views.DivinationResult">

    <ContentPage.BindingContext>
        <viewmodels:MemViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        
        <StackLayout>
            
            <Image Source="{Binding  imageSource}" Aspect="AspectFit"  
                   VerticalOptions="Center"
                   HorizontalOptions="Center"
                   HeightRequest="100" WidthRequest="100"/>
            <Label Text="{Binding interpretation}"
                   VerticalOptions="Center"
                   HorizontalOptions="Center"
                   TextColor="Purple"/>

        </StackLayout>
    </ContentPage.Content>

          
              

</ContentPage>

'

The page with ViewModel '

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows.Input;
using MDivination.Models;
using Xamarin.Forms;
using MDivination.Views;
using System.Runtime.InteropServices.ComTypes;
using System.Collections.ObjectModel;
using System.Reflection;

namespace MDivination.ViewModels
{
    public class MemViewModel : INotifyPropertyChanged
    {
       
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
                }
        public ICommand DivinationCommand { get; set; }
        public ICommand BackCommand { get; set; }
        public INavigation Navigation;

        public Mem Mem { get; set; }
        public MemViewModel()
        {
            Mem=new Mem();
            DivinationCommand = new Command(Divination);
            BackCommand = new Command(Back);
        }
        
        void Divination(object sender)
        {
           
            Navigation.PushAsync(new DivinationResult());
        }
        void Back(object sender)
        {
            Navigation.PopModalAsync();
        }

    }

}

' The Model

using MDivination.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Xamarin.Forms;

namespace MDivination.Models
{
    public class Mem : INotifyPropertyChanged
    {
        
        private int Id { get; set; }
        private ImageSource ImageSource { get; set; }
        private string Interpretation { get; set; }
        private string History { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
        public Mem() { }

        public int id
        {
            get { return id; }
            set
            {
                Random random = new Random();
                id = random.Next(0,2);
                OnPropertyChanged(nameof(id));
            }
        }
        public ImageSource imageSource
        {
            get { return imageSource; }
            set {
                var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MemViewModel)).Assembly;
                imageSource = ImageSource.FromResource($"MDivination.Images.{id}.png"); 
                OnPropertyChanged(nameof(imageSource)); }
        }
        public string interpretation
        {
            get { return interpretation; }
            set
            {
                var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MemViewModel)).Assembly;
                Stream streamInterpretation = assembly.GetManifestResourceStream($"MDivination.Interpretation.{id}.txt");
                interpretation = new StreamReader(streamInterpretation).ReadToEnd();
               
                OnPropertyChanged(nameof(interpretation));
            }
        }
        public string history
        {
            get { return history; }
            set
            {
                var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MemViewModel)).Assembly;
                Stream streamInterpretation = assembly.GetManifestResourceStream($"MDivination.History.{id}.txt");
                history = new StreamReader(streamInterpretation).ReadToEnd(); 
                History = history;

                OnPropertyChanged(nameof(History));
            }
        }
    }
}

Upvotes: 0

Views: 54

Answers (1)

Jason
Jason

Reputation: 89082

this is binding to a property interpretation on your VM. But your VM does not have a property named interpretation

<Label Text="{Binding interpretation}"

instead, your VM has a property named Mem that has a child property named interpretation. So your binding expression should be

<Label Text="{Binding Mem.interpretation}"

Upvotes: 1

Related Questions