【问题标题】:Back To Basics: Xamarin Forms Binding回归基础:Xamarin 表单绑定
【发布时间】:2019-07-11 08:27:24
【问题描述】:

我需要一些有关 Visual Studio 2017 中 Xamarin.Forms 基础知识的帮助。 我编写了一个类来保存可以在内容页面上绑定的某些属性。我已经阅读了许多示例,但每次我仍然无法正确!

当我编译页面时会出现标签

<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,所以很明显我有问题

请有人建议我缺少什么

我在这里将代码简化为简单的东西 所以我相信我的视图类是

 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>

最后是页面代码

    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();
        }

    }

}

【问题讨论】:

  • 我将首先删除绑定表达式中的 } 和 " 空格。运行时可能对语法非常严格,额外的空格可能是个问题。Save_Clicked 方法,虽然你没有问一下,不会起作用。在构造函数中,您将 Bi​​ndingContext 设置为 AddStudentView,并在 Save_Clicked 中尝试将其强制转换为 Student。也许在调试时发生了一些变化,但需要注意。
  • 您还在 XAML 和后面的代码中设置 BindingContext。选择其中一个以减少混乱
  • 虽然您的代码中存在一些问题,但它按预期对我有用。您确定这是您使用的确切代码吗?
  • 代码是从项目中剪切和粘贴的。我将添加在三星 S7 上运行 Android 时的屏幕截图。

标签: xaml xamarin data-binding xamarin.forms


【解决方案1】:

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

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;

    }       
}

视图模型:

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));
    }
}

【讨论】:

  • 恕我直言,这根本不是 OPs 问题的答案。虽然这是更严格的 MVVM,这可能是个好主意,但这无助于解决 OP 遇到的问题。
猜你喜欢
  • 1970-01-01
  • 2019-08-13
  • 2019-09-08
  • 1970-01-01
  • 2016-10-10
  • 2018-12-16
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
相关资源
最近更新 更多