双向绑定在Xamarin表单中不起作用

时间:2018-04-05 09:59:27

标签: xaml xamarin binding xamarin.forms

我在Xamarin表单项目中工作。输入的表单值未显示在Binding上下文中。绑定上下文始终显示初始值而不是最新值。我已添加我的表单xaml部分和查看相关的View模型类。是否有任何添加配置以启用双向绑定

Xaml Page

 <StackLayout Grid.Row="0" Grid.Column="0">
        <Grid x:Name="grid">
          <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
         </Grid.ColumnDefinitions>
            <Label x:Name="labelName" Grid.Row="0"  Grid.Column="0" Margin="10" FontSize="6"  VerticalOptions="CenterAndExpand"  HorizontalTextAlignment="Start" Text="Name"/>
                    <Entry  x:Name="textName" Grid.Row="0"  Grid.Column="1"  WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding Name,Mode=TwoWay}"  />
            <Label x:Name="labelAge" Grid.Row="1"  Grid.Column="0" Margin="10" FontSize="6"  Text="Age" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                    <Entry  x:Name="textAge" Grid.Row="1"  Grid.Column="1" WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding Age,Mode=TwoWay}" />
            <Label x:Name="labelAddress" Grid.Row="2"  Grid.Column="0" Margin="10" FontSize="6"  Text="Address" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                    <Entry  x:Name="textAddress" Grid.Row="2"  Grid.Column="1" WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start"  Text="{Binding Address,Mode=TwoWay}" />
            <Label x:Name="labelNICNumber" Grid.Row="3"  Grid.Column="0" Margin="10" FontSize="6" Text="NIC" VerticalOptions="CenterAndExpand" HorizontalTextAlignment="Start" />
                    <Entry  x:Name="textNIC" Grid.Row="3"  Grid.Column="1" WidthRequest="100" FontSize="6" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding NIC,Mode=TwoWay}" />
            <Button Grid.Row="4"  Grid.Column="1" HeightRequest = "30"   VerticalOptions="CenterAndExpand" HorizontalOptions="Start" FontSize="6" Text="Save" Clicked="UserSaveClick" />
        </Grid>
    </StackLayout>

视图模型

public event PropertyChangedEventHandler PropertyChanged;

    private string userName ;
    private string name;
    private int age ;
    private bool isBusy;
    private string address;
    private int nic;


    void OnPropertyChanged([CallerMemberName] string name = "")
    {
        PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
    }

    public int NIC
    {
        get { return nic; }
        set
        {
            nic = value;
            OnPropertyChanged();

        }
    }


    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            IsBusy = Name == "aa" ? true : false;
            OnPropertyChanged();
            OnPropertyChanged(nameof(DisplayMessage));
        }
    }



    public int Age
    {
        get { return age; }
        set
        {
            age = value;
            OnPropertyChanged();
        }
    }


    public string Address
    {
        get { return address; }
        set
        {
            address = value;
            OnPropertyChanged();

        }
    }

    public string DisplayMessage
    {
        get
        {
            return "Hi  " + name;
        }

    }




    public bool IsBusy
    {
        get { return isBusy; }
        set { isBusy = value; }
    }


}

2 个答案:

答案 0 :(得分:1)

你能查一下:

1.在输入控件中输入文本时,检查它是否正在查看每个属性的视图模型中的断点。

2.检查INotifyPropertyChanged已实施。

3.通过设置Entry控件的Text属性的硬编码来检查。

答案 1 :(得分:0)

还要检查您正确的设置方法是否为私有。我花了几个小时才能找出这个简单的复制粘贴错误。 我在调试输出Binding:在“ yyyy”上找不到“ xxx”属性,在目标属性:“ Xamarin.Forms.Entry.Text”上遇到此错误。 顺便说一下,此错误消息不是很清楚。