2017-09-23 53 views
0

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

enter image description here

這裏用戶只能看到數據。

我在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"/> 

第二頁,我將數據顯示

enter image description here

這裏,如果用戶選擇的項目,他可以修改訂單數量。作爲

<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。如果用戶修改qtypartQty應保持不受影響。

在寫這個問題的時候,我想到了一種方法。我可以製作listview bomItems的副本並在RequestQuote中使用,以便原始的一個保持不變。這會是很好的方法嗎?請讓我知道是否還有其他方法可以實現同樣的目標。

在此先感謝。 (道歉發佈很長的問題)

回答

1

如果我理解你想完成什麼,你可以嘗試這樣的變體:

我在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),通常認爲它是私有的,不公開的。

+0

Thamks @DavidS。我用第一種方法解決了這個問題。我是新手謝謝你的建議。 – luckyShubhra

+0

謝謝。今天我實施了你的第一種方法。它完美的作品。感謝您的見解。 – luckyShubhra

+0

感謝您的最後一件作品。我已將所有存儲字段更改爲私有。 Thankss! – luckyShubhra