0

Here is a picture of my file structure and MainPage.Xaml file MainPage.xaml Here is the code in that file

<?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:local="clr-namespace:AppXam"
             x:Class="AppXam.MainPage">
    <ContentPage.BindingContext>
        <local:MainPage/>
    </ContentPage.BindingContext>
    <Grid>
        <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="2*"/>
        <RowDefinition Height=".5*"/>
        <RowDefinition Height="2*"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    
    <Image Source="https://s2.dmcdn.net/v/AmZGT1VXLk5NDRThE/x1080" BackgroundColor="red"
           Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2"/>
    
    <Editor Grid.Column ="0" Grid.ColumnSpan="2" Grid.Row="1" 
            Placeholder="Enter Note Here" Text="{Binding TheNote}" />
    
    <Button Grid.Row="2" Grid.Column="0" Text="Save" Command="{Binding SaveCommand}"   />
    
    <Button Grid.Row="2" Grid.Column="1" Text="Erase"    Command="{Binding EaseCommand}"/>
    
    <CollectionView ItemsSource="{Binding AllNotes}" Grid.Row="3" Grid.ColumnSpan="2">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Frame>
                        <Label Grid.Row="3" Grid.Column="0" Text="{Binding .}" FontSize="Large" /> 
                    </Frame>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
    
</Grid>

Here is a picture of the MainPage.XAML.cs which is the namespace that contains the MVVM code. MainPage.xaml.cs

The code is as follows :

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace AppXam
{
    public partial class MainPage : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public MainPage()
        {
            EraseCommand = new Command(() =>
            {
                TheNote = string.Empty;
            });
            
           SaveCommand = new Command(() =>
           {
               AllNotes.Add(TheNote);

               TheNote = string.Empty;
           });
        }
        public ObservableCollection<string> AllNotes {get; set;}
        
        
        
        string theNote;
        public string TheNote
         {
             get => theNote;
             set
             {
                 theNote = value;
                 var args = new  PropertyChangedEventArgs(nameof(TheNote));
                 PropertyChanged?.Invoke(this,args);
             }
         }

        public Command SaveCommand { get; }
        public Command EraseCommand { get; }
    }
}

The screenshots contain the output on the screen, which is a blank white page. The project directory can be found here

https://drive.google.com/drive/folders/1sp4qh3Wzse1KVZN1IKdteaGcgncCG4p4?usp=sharing

AshLove
  • 1
  • 2
  • 1
    Please don't post code as an image [ask] [mre] – Cfun Nov 03 '20 at 18:51
  • what do the logs show when deploying? – Jason Nov 03 '20 at 20:07
  • @AshLove I try Shaw's reply, it works. There is one doc about Xamarin.Forms data binding mvvm, you can take a look:[Data Bindings to MVVM](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm) – Cherry Bu - MSFT Nov 04 '20 at 02:48

1 Answers1

0

Remove the binding in the XAML and use this:

    public MainPage()
    {
        InitializeComponent();  //1. no init, no page
        BindingContext = this;  //2. must bind after init

        //other things to do
    }
  • Please, clean the solution (remove the bin & obj folders) before uploading next time.
Shaw
  • 907
  • 1
  • 8
  • 20