返回基础:Xamarin表单绑定 [英] Back To Basics: Xamarin Forms Binding
问题描述
请在Visual Studio 2017中了解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"/>
正确,但
<Label
Text="{Binding TestBinding}"
Grid.Row="0"
Grid.Column="1"
HorizontalOptions="Start"
WidthRequest="100"
VerticalOptions="Center"/>
显示为空,而不是文本 Test binding
显然我出了错
comes up empty instead of the text Test binding
so clearly I have got something wrong
请有人可以建议我所缺少的内容
Please can someone suggest what I am missing
我已经脱身了代码简单到这里
所以我相信我的视图类是使用System的
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()
{ }
}
}
我的内容页面是
<?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();
}
}
}
推荐答案
您可以尝试以下代码,如果此代码有任何问题,请告诉我。谢谢。
You can try the following code and let me know if you have any issues with this code. Thank you.
<?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
Xaml.cs
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AddStudent : ContentPage
{
public AddStudentView Source = new AddStudentView(this.Navigation);
public AddStudent ()
{
InitializeComponent ();
BindingContext = Source;
}
}
ViewModel:
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));
}
}
这篇关于返回基础:Xamarin表单绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!