Back To Basics: Xamarin Forms Binding

时间:2019-02-18 00:40:51

标签: xaml xamarin data-binding xamarin.forms

I need some help with the basics of Xamarin.Forms in Visual Studio 2017, please. I have written a class to hold certain properties that I can bind to on my content page. I have read a number of examples but each time I still can not get it right!

When I compile the page comes up with the label

<Label 
Text="TestBinding"  
Grid.Row="0" 
Grid.Column="0" 
HorizontalOptions="Start" 
WidthRequest="100" 
VerticalOptions="Center"/>

correctly, but the

<Label 
Text="{Binding TestBinding}"  
Grid.Row="0" 
Grid.Column="1" 
HorizontalOptions="Start" 
WidthRequest="100" 
VerticalOptions="Center"/>

comes up empty instead of the text Test binding so clearly I have got something wrong

Please can someone suggest what I am missing

I have stripped back the code to something simple here So I believe my view class is

 using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Xamarin.Forms;

namespace FitRestults_Dev1 
{
    public class AddStudentView : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        string _testtext="Test binding";
       public string TestBinding
        {
            get=> _testtext;

            set
            {
                if (string.Equals(_testtext, value))
                    return;

                _testtext = value;

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TestBinding)));
            }
        }
        public ObservableCollection<GradeCollection> GradeCollection { get; set; }

        public AddStudentView()
        { }
    }

}

And my content page is

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="FitRestults_Dev1.AddStudent"
             xmlns:local="clr-namespace:FitRestults_Dev1.AddStudentView;assembly=FitRestults_Dev1"
             BindingContext="x:local "
           >


    <ContentPage.Content>

        <StackLayout Padding="10" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Grid>
                <Label Text="TestBinding "  Grid.Row="0" Grid.Column="0" HorizontalOptions="Start" WidthRequest="100" VerticalOptions="Center"/>
                <Label Text="{Binding TestBinding} "  Grid.Row="0" Grid.Column="1" HorizontalOptions="Start" WidthRequest="100" VerticalOptions="Center"/>
             </Grid>
            <Button Text="Save" HorizontalOptions="FillAndExpand" BackgroundColor="Blue" TextColor="White" Clicked="Save_Clicked" />
            <Button Text="Cancel" HorizontalOptions="FillAndExpand" BackgroundColor="Blue" TextColor="White" Clicked="Cancel_Clicked" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

And finally behind page code is

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

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace FitRestults_Dev1
{
    [XamlCompilation(XamlCompilationOptions.Compile)]


    public partial class AddStudent : ContentPage
    {
        public AddStudentView Source = new AddStudentView();
        public AddStudent ()
        {
            InitializeComponent ();
            BindingContext = Source;

        }
        async void Save_Clicked(object sender, System.EventArgs e)
        {
            var personItem = (Student)BindingContext;
            await App.Database.SaveStudentAsync(personItem);
            await Navigation.PopAsync();
        }
        async void Cancel_Clicked(object sender, System.EventArgs e)
        {
            await Navigation.PopAsync();
        }

    }

}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码,如果此代码有任何问题,请告诉我。谢谢。

Xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="FitRestults_Dev1.AddStudent"
         xmlns:local="clr 
namespace:FitRestults_Dev1.AddStudentView;assembly=FitRestults_Dev1"
         BindingContext="x:local ">

<ContentPage.Content>
    <StackLayout Padding="10" HorizontalOptions="FillAndExpand" 
VerticalOptions="FillAndExpand">
        <Grid>
            <Label Text="TestBinding "  Grid.Row="0" Grid.Column="0" 
HorizontalOptions="Start" WidthRequest="100" VerticalOptions="Center"/>
            <Label Text="{Binding TestBinding} "  Grid.Row="0" Grid.Column="1" 
HorizontalOptions="Start" WidthRequest="100" VerticalOptions="Center"/>
         </Grid>
        <Button Text="Save" HorizontalOptions="FillAndExpand" BackgroundColor="Blue" 
TextColor="White" Clicked="SaveCommand" />
        <Button Text="Cancel" HorizontalOptions="FillAndExpand" 
BackgroundColor="Blue" TextColor="White" Clicked="CancelCommand" />
    </StackLayout>
</ContentPage.Content>
</ContentPage>

Xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]

public partial class AddStudent : ContentPage
{
    public AddStudentView Source = new AddStudentView(this.Navigation);
    public AddStudent ()
    {
        InitializeComponent ();
        BindingContext = Source;

    }       
}

ViewModel:

public class AddStudentView : INotifyPropertyChanged
{
    private ICommand _navigation;
    public AddStudentView(INavigation navigation)
    {
        _navigation = naviation;
        SaveCommand = new Command(SaveCommandHandler);

        CancelCommand = new Command(CancelCommandHandler)
    }

    public string TestBinding
    {
        get {return _testBinding;}
        set
        {
            _testBinding = value;
            OnPropertyChanged();
        }
    }

    public Command SaveCommand {get;set;}

    public Command CancelCommand {get;set;}

    public void SaveCommandHandler()
    {
        var value = TestBinding

        _navigation.PopAsync();
    }

    public void CancelCommandHandler()
    {
        _navigation.PopAsync();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}