如何在Xamarin Forms中将一个var复制到模型中的其他var
我是Xamarin Forms和MVVM模型的新手。在我的应用程序中有一个列表视图。 listview的项目源是通过web api。我从API获取数据并在列表视图中显示。如何在Xamarin Forms中将一个var复制到模型中的其他var
我正在从api中获取数据,并使用JSON Net来解析设置为ListView数据源的数据。
string uriString = "http:*****************";
var response = await httpRequest(uriString) ;
System.Diagnostics.Debug.WriteLine(response);
SecondPage.bomItems = JsonConvert.DeserializeObject<List<BomListModel>>(response);
我BomListModel
是
public class BomListModel : INotifyPropertyChanged
{
public int _PartNo { get; set; }
public bool _isChecked { get; set; }
public int _partQty { get; set; }
public int _qty { get; set; }
public string _partDesignation { get; set; }
public int PartNo
{
get { return _PartNo; }
set
{
_PartNo = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("PartNo"));
}
}
public bool isChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("isChecked"));
}
}
public int qty
{
get { return _qty; }
set
{
_qty = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("qty"));
}
}
public int partQty
{
get { return _partQty; }
set
{
_partQty = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("partQty"));
}
}
public string partDesignation
{
get { return _partDesignation; }
set
{
_partDesignation = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("partDesignation"));
}
}
private void NotifyPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
一切工作正常。数据正在显示。 我需要在2页中显示相同的数据。 首先在BomList
作为
这里用户只能看到数据。
我在RequestQuote
结合数据
<Label Text="{Binding PartNo}" TextColor="WhiteSmoke" FontSize="Small" HorizontalTextAlignment="Center" Grid.Column="0" Grid.Row="0" Margin="2" VerticalTextAlignment="Center"/>
<Label Text="{Binding partDesignation}" TextColor="WhiteSmoke" HorizontalTextAlignment="Start" FontSize="Small" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Margin="2" VerticalTextAlignment="Center"/>
<Label Text="{Binding partQty}" TextColor="WhiteSmoke" HorizontalTextAlignment="Center" FontSize="Small" Grid.Column="3" Grid.Row="0" Margin="2" VerticalTextAlignment="Center"/>
第二页,我将数据显示
这里,如果用户选择的项目,他可以修改订单数量。作为
<Label Text="{Binding PartNo}" TextColor="DodgerBlue" FontSize="Small"
HorizontalTextAlignment="Center" Grid.Column="0" Grid.Row="0" Margin="2" VerticalTextAlignment="Center"></Label>
<Label Text="{Binding partDesignation}" TextColor="DodgerBlue" HorizontalTextAlignment="Start"
FontSize="Small" Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="0" Margin="2" VerticalTextAlignment="Center"></Label>
<common:CustomCheckBox Grid.Column="4" Grid.Row="0" HeightRequest="20" WidthRequest="20"
VerticalOptions="Center" HorizontalOptions="Center" Checked="{Binding isChecked ,Mode=TwoWay}"
CheckedImage="checkbox_checked" UnCheckedImage="checkbox_unchecked"
CommandParameter="{Binding .}"/>
<Entry TextColor="Black" Grid.Column="5" Grid.Row="0" Text="{Binding partQty, Mode=TwoWay}"
FontSize="Small" VerticalOptions="End" HorizontalOptions="Center" IsEnabled="{Binding isChecked ,Mode=TwoWay}" />
问题就出现在这里
数据绑定。由于partQty
绑定在TwoWay
模式,如果用户在这里修改数据我的模型得到更新,因此BomList
也得到更新。 我已使用static
列表视图来分享我的清单,通过了应用程序。我在Second Page
作为
public static List<BomListModel> bomItems { get; set; }
有什么办法来防止数据被更新定义它?我需要TwoWay
模式绑定,因为我需要获得产品用户想要订购的数量。 我在模型中添加了一个var qty
。如何将我从api获取的partQty
复制到qty
。如果用户修改qty
partQty
应保持不受影响。
在写这个问题的时候,我想到了一种方法。我可以制作listview bomItems
的副本并在RequestQuote
中使用,以便原始的一个保持不变。这会是很好的方法吗?请让我知道是否还有其他方法可以实现同样的目标。
在此先感谢。 (道歉发布很长的问题)
如果我理解你想完成什么,你可以尝试这样的变体:
我在model.How增加了一个变种数量能我将我从api获得的partQty 复制到qty。如果用户修改零件数量零件数量 应保持不受影响。
您可以从partQty通过更改到BomListModel复制到数量上的第一需求:
private int? _qty;
public int qty
{
get
{
if (!_qty.HasValue)
_qty = _partQty;
return _qty.Value;
}
set
{
_qty = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("qty"));
}
}
只要数量不是被反序列化JSON的一部分,_qty将保持零直到qty的值是需要的,在这一点上它是用_partQty初始化的。之后,qty和partQty的值是分开的 - 更改为一个不会影响另一个。
你其他的想法:
我可以让列表视图bomItems复印件和RequestQuote使用,使原来保持不变。这会是很好的方法吗?
也可能是个好主意,它的确取决于你想要完成什么。例如,如果BomListModel意味着从服务器获取一次,然后RequestQuote会多次使用这些引用,那么后一种方法看起来好多了。它清楚地将只读数据与可写数据分开,所以您不必担心从一个RequestQuote“溢出”到下一个数据的数据,因为qty设置在静态列表中,并且不会在两者之间进行重置。
顺便说一句,对于实际存储字段(如_qty,_partQty),通常认为它是私有的,不公开的。
Thamks @DavidS。我用第一种方法解决了这个问题。我是新手谢谢你的建议。 – luckyShubhra
谢谢。今天我实施了你的第一种方法。它完美的作品。感谢您的见解。 – luckyShubhra
感谢您的最后一件作品。我已将所有存储字段更改为私有。 Thankss! – luckyShubhra