【发布时间】:2017-09-23 05:27:24
【问题描述】:
我也是 Xamarin Forms 和 MVVM 模型的新手。我的应用程序中有一个列表视图。列表视图的项目来源是通过 web api。我正在从 api 获取数据并在列表视图中显示。
我正在从 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作为
这里用户只能看到数据。
我将数据绑定为
<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"/>
RequestQuote 中的第二页我将数据显示为
这里如果用户选择商品,他可以修改订单数量。
数据绑定为
<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 中使用,以便原始副本保持不变。这会是个好方法吗?请让我知道是否有其他方法可以完成相同的操作。
提前致谢。 (很抱歉发布很长的问题)
【问题讨论】:
标签: listview xamarin mvvm xamarin.forms