私は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
初 ここ
私は、ユーザーが項目を選択した場合、彼は注文数量を変更することができ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"/>
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}" />
問題はここに応じて
データがバインドされます。 partQty
はTwoWay
モードでバインドされているため、ここでユーザーがデータを変更するとモデルが更新され、BomList
も更新されます。 static
リストビューを使って私のリストをアウトアプリから共有しました。私は、更新されたデータを防ぐためにどのような方法があります私のSecond Page
public static List<BomListModel> bomItems { get; set; }
ようにそれを定義しています?
TwoWay
モードバインドが必要です。注文する製品ユーザーの数量を取得する必要があります。 私のモデルにもう1つ、var
qty
を追加しました。
partQty
をにコピーしています。ユーザーが変更した場合
qty
partQty
は影響を受けません。
この質問を書いているうちに、1つのアプローチが私の心に打ち勝っています。私はlistview bomItems
の複製を作成して、RequestQuote
に使用することができます。元のものは元のままです。これは良いアプローチだろうか?同じことを達成するための他の方法があるかどうか私に教えてください。
ありがとうございます。 (非常に長い質問を投稿するための謝罪)
Thamks @DavidS。私はあなたの最初のアプローチでそれを働かせました。私はあなたのアドバイスの初心者のおかげです。 – luckyShubhra
ありがとうございます。今日私はあなたの最初のアプローチを実装しました。それは完全に動作します。あなたの洞察に感謝します。 – luckyShubhra
最後の作品をありがとう。私はすべてのストレージフィールドをプライベートとして変更しました。ありがとう!! – luckyShubhra