如何解决xaml UI 未以 xamarin 形式更新
我正在将我的模型与我的 UI 绑定,在我的模型中我做了一些计算,但其他属性与 UI 绑定,但我已经完成计算的一些属性没有与我的 UI 绑定,但在我的 OnPropertyChange 中显示计算事件。请帮助我解决我的代码有问题的地方提前致谢。
-----我的模型----
public class SaleEntryModel
{
[PrimaryKey,AutoIncrement]
public int SaleID { get; set; }
public string CustomerName { get; set; }
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal Quantity { get; set; }
public decimal Rate { get; set; }
public decimal Total => Rate * Quantity;
public decimal Balance => (Total - (discount + Paidamount));
}
----OnPropertyChange 事件 ---
private SaleEntryModel bindSaleEntryModel = new SaleEntryModel();
public SaleEntryModel BindSaleEntryModel
{
get { return bindSaleEntryModel; }
set
{
bindSaleEntryModel = value;
OnPropertyChanged(nameof(BindSaleEntryModel));
}
}
---我的xaml代码---
<StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand" Padding="10">
<Label Text="Rate" Margin="2,-10" FontAttributes="Bold" />
<Entry x:Name="Rate" Margin="2,-5,2,5" Text="{Binding BindSaleEntryModel.Rate,Mode=TwoWay}"
HorizontalOptions="FillAndExpand" Keyboard="Numeric" ReturnType="Next" />
<Label x:Name="RateError" Margin="2,-10,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />
<Label Text="Quantity" Margin="2,-10" FontAttributes="Bold" />
<Entry x:Name="Quantity" Margin="2,5" Text="{Binding BindSaleEntryModel.Quantity,Mode=TwoWay}"
HorizontalOptions="FillAndExpand" Keyboard="Numeric" ReturnType="Next" />
<Label x:Name="QuantityError" Margin="2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />
<Label Text="Total" Margin="2,-10" FontAttributes="Bold" />
<Entry x:Name="Total" Margin="2,5" IsEnabled="False"
Text="{Binding BindSaleEntryModel.Totals,Mode=TwoWay}"
HorizontalOptions="FillAndExpand" ReturnType="Next"/>
<Label Text="discount (Rs)" Margin="2,-10" FontAttributes="Bold" />
<Entry x:Name="discount" Margin="2,5" Text="{Binding BindSaleEntryModel.discount,Mode=TwoWay}"
HorizontalOptions="FillAndExpand"
Keyboard="Numeric" ReturnType="Next"/>
<Label x:Name="discountError" Margin="2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />
<Label Text="Paid Amount" Margin="2,-10" FontAttributes="Bold" />
<Entry x:Name="Paidamount" Margin="2,5" Text="{Binding BindSaleEntryModel.Paidamount,Mode=TwoWay}"
HorizontalOptions="FillAndExpand" Keyboard="Numeric" ReturnType="Next"/>
<Label x:Name="PaidamountError" Margin="2,5" TextColor="Red" IsVisible="false" FontAttributes="Italic" />
<Label Text="Balance" Margin="2,-10" FontAttributes="Bold" />
<Entry x:Name="Balance" Margin="2,5" IsEnabled="False"
Text="{Binding BindSaleEntryModel.Balance,Mode=TwoWay}"
HorizontalOptions="FillAndExpand" />
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDeFinitions>
<ColumnDeFinition Width="1*" />
<ColumnDeFinition Width="1*" />
</Grid.ColumnDeFinitions>
<Button Text="Save" x:Name="btnSave" HorizontalOptions="FillAndExpand"
CornerRadius="10" BorderWidth="2" BackgroundColor="#ff6633" TextColor="#fff" Margin="2"
Grid.Column="0" Grid.Row="0" Command="{Binding SaveCommand}" />
<Button Text="CLEAR" x:Name="btnClear" HorizontalOptions="FillAndExpand"
CornerRadius="10" BorderWidth="2" BackgroundColor="#bfbfbf"
TextColor="#fff" Margin="2" Grid.Column="1" Grid.Row="0" Command="{Binding ClearCommand}" />
</Grid>
</StackLayout>
在上面的图像计算中,我的 UI 中显示但未绑定。
解决方法
您需要为 SaleEntryModel 中的每个绑定属性提升属性更改。请参考以下代码。
public partial class CalculationQ : ContentPage
{
public CalculationQ()
{
InitializeComponent();
this.BindingContext = this;
GetSaleEntry();
}
private SaleEntryModel bindSaleEntryModel = new SaleEntryModel();
public SaleEntryModel BindSaleEntryModel
{
get { return bindSaleEntryModel; }
set
{
bindSaleEntryModel = value;
OnPropertyChanged(nameof(BindSaleEntryModel));
}
}
private void GetSaleEntry()
{
BindSaleEntryModel.SaleID = 1;
BindSaleEntryModel.CustomerName = "Murugan";
BindSaleEntryModel.ProductID = 1;
BindSaleEntryModel.ProductName = "Toy";
BindSaleEntryModel.Quantity = 5;
BindSaleEntryModel.Rate = 150;
BindSaleEntryModel.Discount = 5;
BindSaleEntryModel.PaidAmount = 250;
}
}
public class SaleEntryModel : INotifyPropertyChanged
{
public int SaleID { get; set; }
private string _customerName;
public string CustomerName
{
get { return _customerName; }
set
{
_customerName = value;
OnPropertyChange(nameof(CustomerName));
}
}
public int ProductID { get; set; }
private string _productName;
public string ProductName
{
get { return _productName; }
set
{
_productName = value;
OnPropertyChange(nameof(ProductName));
}
}
private decimal _quantity;
public decimal Quantity
{
get { return _quantity; }
set
{
_quantity = value;
OnPropertyChange(nameof(Quantity));
OnPropertyChange(nameof(Total));
OnPropertyChange(nameof(Balance));
}
}
private decimal _rate;
public decimal Rate
{
get { return _rate; }
set
{
_rate = value;
OnPropertyChange(nameof(Rate));
OnPropertyChange(nameof(Total));
OnPropertyChange(nameof(Balance));
}
}
public decimal Total => Rate * Quantity;
public decimal Balance => (Total - (Discount + PaidAmount));
private int _discount;
public int Discount
{
get => _discount;
set
{
_discount = value;
OnPropertyChange(nameof(Discount));
OnPropertyChange(nameof(Balance));
}
}
private int _paidAmount;
public int PaidAmount
{
get => _paidAmount;
set
{
_paidAmount = value;
OnPropertyChange(nameof(PaidAmount));
OnPropertyChange(nameof(Balance));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChange(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(propName));
}
}
}
我的建议是为视图模型绑定保留单独的模型,而不是使用实体模型。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。