【问题标题】:Can someone tell me where I'm going wrong with Binding in Xamarin有人可以告诉我 Xamarin 中的绑定哪里出了问题
【发布时间】:2019-11-19 14:36:13
【问题描述】:

下面我附上了我的 xaml 和 cs 文件的 sn-ps。我正在尝试将输入框绑定到正确的属性,并且标签将绑定到总属性。我显然在某个地方出错了,我无法确定在哪里。使用 WPF 时,我以相同的方式布置绑定。我试过用谷歌搜索这个和几个视频,但似乎都没有解决我的问题。

XAML

 <Label Text="{Binding Total}"
               TextColor="Black"
               FontSize="50"
               Grid.Column="1"
               Grid.ColumnSpan="3"
               Grid.Row="4"
               Grid.RowSpan="1"
               VerticalOptions="Center"
               HorizontalOptions="Center"/>

        <Label Text="APR Amount"
               TextColor="Black"
               FontSize="16"
               Grid.Column="1"
               Grid.ColumnSpan="3"
               Grid.Row="5"
               Grid.RowSpan="1"
               VerticalOptions="Center"
               HorizontalOptions="Center"/>

        <Label Text="£APR"
               TextColor="Black"
               FontSize="30"
               Grid.Column="1"
               Grid.ColumnSpan="3"
               Grid.Row="6"
               Grid.RowSpan="1"
               VerticalOptions="Center"
               HorizontalOptions="Center"/>

         <BoxView Color="CornflowerBlue"
                CornerRadius="0"
                Grid.Column="0"
                Grid.ColumnSpan="5"
                Grid.Row="7"
                Grid.RowSpan="1"   
                WidthRequest="700"
                HeightRequest="5"
                VerticalOptions="Center"
                HorizontalOptions="Center" />

        <Label Text="Loan Amount"
               TextColor="Black"
               FontSize="20"
               Grid.Column="0"
               Grid.ColumnSpan="3"
               Grid.Row="9"
               Grid.RowSpan="2"
               VerticalOptions="Center"
               HorizontalOptions="Center"
               Margin="0,0,0,0" />

        <Entry Grid.Row="9"
               Grid.Column="2"
               Grid.ColumnSpan="3"
               Grid.RowSpan="2"
               WidthRequest="120"
               VerticalOptions="Center"
               HorizontalOptions="End"
               Text="{Binding loanLength}"
               Margin="0,0,45,0" />

         <Label Text="Interest Rate"
               TextColor="Black"
               FontSize="20"
               Grid.Column="0"
               Grid.ColumnSpan="3"
               Grid.Row="11"
               Grid.RowSpan="2"
               VerticalOptions="Center"
               HorizontalOptions="Center"
               Margin="0,0,0,0" />

        <Entry Grid.Row="11"
               Grid.RowSpan="2"
               Grid.Column="2"
               Grid.ColumnSpan="3"
               WidthRequest="120"
               VerticalOptions="Center"
               HorizontalOptions="End"
               Text="{Binding intRate}"
               Margin="0,0,45,0" />

        <Label Text="Loan Length (Years)"
               TextColor="Black"
               FontSize="20"
               Grid.Column="0"
               Grid.ColumnSpan="3"
               Grid.Row="13"
               Grid.RowSpan="2"
               VerticalOptions="Center"
               HorizontalOptions="Center"
               Margin="0,0,0,0" />

        <Entry Grid.Row="13"
               Grid.RowSpan="2"
               Grid.Column="2"
               Grid.ColumnSpan="3"
               WidthRequest="120"
               VerticalOptions="Center"
               HorizontalOptions="End"
               Text="{Binding loanLength}"
               Margin="0,0,45,0"/>

C#

  private int loanAmount { get; set; }
        public int Loan
        {
            get => loanAmount;
            set
            {

                loanAmount = value;
                OnPropertyChanged("Loan");
                CalculateAPR();
            }

        }


        private int intRate { get; set; }
        public int Interest
        {
            get => intRate;
            set
            {

                intRate = value;
                OnPropertyChanged("Interest");
                CalculateAPR();
            }

        }

        private int loanLength { get; set; }
        public int Length
        {
            get => loanLength;
            set
            {

                loanLength = value;
                OnPropertyChanged("Length");
                CalculateAPR();
            }

        }


        private string total { get; set; }
        public string Total
        {
            get => total;
            set
            {

                total = value;
                OnPropertyChanged("Total");
            }
        }



        public void CalculateAPR()
        {
            if (Interest != 0)
            {
                Total = "£" + (Loan * Length) / Interest;
            }
            else
            {
                Total = "£ -";
            }
        }

【问题讨论】:

  • Text="{Binding loanLength}" - loanLength 是私有的。 Length 是公共属性的名称。同样适用于intRate。并且loanLength 绑定到两个不同的条目

标签: c# xaml xamarin


【解决方案1】:

您需要绑定到“Length”或“Interest”等公共属性 并调用 PropertyChanged(nameof(loanLength));例如(内部有私有)

如果需要,请勾选 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-binding-basics

【讨论】:

  • 把它更新为你所说的伙计,它仍然没有改变标签
  • 如何设置页面的bindingcontext?
  • 我在 xaml 中的网格中添加了 但仍然没有我想要的一种方式,所以当人们使用应用程序类型时,它会计算并显示总数在标签中
【解决方案2】:

我创建了一个演示来模拟您的代码,它运行正常。 您的代码中有几个问题:

1.我们应该绑定一个公共属性,比如你可以这样绑定:

      <Entry 
           Text="{Binding Loan}"
           Margin="0,0,45,0" />

而不是

      <Entry 
           Text="{Binding loanLength}"
           Margin="0,0,45,0" />

其他领域也是如此。

&lt;Entry Text="{Binding intRate }"/&gt; 替换为&lt;Entry Text="{Binding Interest}"/&gt;

&lt;Entry Text="{Binding loanLength }"/&gt; 替换为&lt;Entry Text="{Binding Length}"/&gt;

2.试试下面的 bindingContext 代码:

   <Grid >
    <Grid.BindingContext>
        <local:ViewModel></local:ViewModel>
    </Grid.BindingContext>
    <Grid.RowDefinitions>

    </Grid.RowDefinitions>

    <!--other code-->

 </Grid>

【讨论】:

  • 谢谢杰西!我应该把 & 放在哪里?在我的 之后?还是在 之后?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-30
相关资源
最近更新 更多