2017-09-23 7 views
0

私はXamarin FormsとMVVMモデルの初心者です。私のアプリにリストビューがあります。リストビューのアイテムソースはWeb API経由です。私はAPIからデータを取り出し、リストビューで表示しています。Xamarinフォームで1つのvarをモデル内の他のvarにコピーする方法

APIからデータを取得してJSON Netを使用して、ListViewのデータソースとして設定されたデータを解析しています。マイBomListModel

string uriString = "http:*****************"; 
var response = await httpRequest(uriString) ; 
System.Diagnostics.Debug.WriteLine(response); 
SecondPage.bomItems = JsonConvert.DeserializeObject<List<BomListModel>>(response); 

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に私はここで

enter image description here

としてデータを表示しています

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

2ページ目としてデータをバインドしています。

<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}" /> 

問題はここに応じて

データがバインドされます。 partQtyTwoWayモードでバインドされているため、ここでユーザーがデータを変更するとモデルが更新され、BomListも更新されます。 staticリストビューを使って私のリストをアウトアプリから共有しました。私は、更新されたデータを防ぐためにどのような方法があります私のSecond Page

public static List<BomListModel> bomItems { get; set; } 

ようにそれを定義しています? TwoWayモードバインドが必要です。注文する製品ユーザーの数量を取得する必要があります。 私のモデルにもう1つ、var qtyを追加しました。 partQtyをにコピーしています。ユーザーが変更した場合 qty partQtyは影響を受けません。

この質問を書いているうちに、1つのアプローチが私の心に打ち勝っています。私はlistview bomItemsの複製を作成して、RequestQuoteに使用することができます。元のものは元のままです。これは良いアプローチだろうか?同じことを達成するための他の方法があるかどうか私に教えてください。

ありがとうございます。 (非常に長い質問を投稿するための謝罪)

答えて

1

私はあなたが達成しようとしているのか理解した場合、あなたはこののバリエーションを試みることができる:

私は私のmodel.Howに1つの以上のvar数量を追加していることができます私はapiから数量になるpartQty をコピーします。ユーザーが数を変更した場合partQty は影響を受けません。

あなたはBomListModelに変更を加えることにより、最初の要求に応じて数量にpartQtyからコピーすることができます。

private int? _qty; 

public int qty 
{ 
    get 
    { 
     if (!_qty.HasValue) 
      _qty = _partQty; 
     return _qty.Value; 
    } 

    set 
    { 
     _qty = value; 
     NotifyPropertyChanged(new PropertyChangedEventArgs("qty")); 
    } 
} 

限り、数量はデシリアライズされますJSONの一部ではないとして、_qtyはまではnullのままになりますqtyの値が必要です。この時点で、_partQtyで初期化されます。その後、qtyとpartQtyの値は別々になります。一方への変更は他方には影響しません。

あなたの他のアイデア:

オリジナルのものがそのまま残るように私は、リストビューbomItemsの複製コピーを作成し、RequestQuoteで使用することができます。これは良いアプローチだろうか?

良い考えでもありますが、これは実際に達成しようとしていることによります。たとえば、BomListModelがサーバーから一度フェッチされ、RequestQuoteが異なる引用符で複数回使用する場合、この後者の方がはるかに優れているようです。読み取り専用のデータと書き込み可能なデータは明確に分かれているため、qtyは静的リストに設定されていて間にリセットされていないため、1つのRequestQuoteのデータが次のデータに移動することを心配する必要はありません。

ところで実際には、_qty、_partQtyなどの格納フィールドは非公開で、公開ではありません。

+0

Thamks @DavidS。私はあなたの最初のアプローチでそれを働かせました。私はあなたのアドバイスの初心者のおかげです。 – luckyShubhra

+0

ありがとうございます。今日私はあなたの最初のアプローチを実装しました。それは完全に動作します。あなたの洞察に感謝します。 – luckyShubhra

+0

最後の作品をありがとう。私はすべてのストレージフィールドをプライベートとして変更しました。ありがとう!! – luckyShubhra

関連する問題