我是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
中使用,以便原始的一个保持不变。这会是很好的方法吗?请让我知道是否还有其他方法可以实现同样的目标。
在此先感谢。 (道歉发布很长的问题)
Thamks @DavidS。我用第一种方法解决了这个问题。我是新手谢谢你的建议。 – luckyShubhra
谢谢。今天我实施了你的第一种方法。它完美的作品。感谢您的见解。 – luckyShubhra
感谢您的最后一件作品。我已将所有存储字段更改为私有。 Thankss! – luckyShubhra