我是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