【问题标题】:How to copy one var to other var in model in Xamarin Forms如何在 Xamarin Forms 中将一个 var 复制到模型中的另一个 var
【发布时间】: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}" />

问题出现在这里。由于partQtyTwoWay 模式下绑定,如果用户在此处修改数据,我的模型会更新,因此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


    【解决方案1】:

    如果我了解您要完成的工作,您可以尝试以下变体:

    我在模型中又添加了一个 var qty。如何复制 partQty 我从 api 到 qty。如果用户修改 qty partQty 应该不受影响。

    您可以通过更改 BomListModel 来按需从 partQty 复制到 qty:

    private int? _qty;
    
    public int qty
    {
        get
        {
            if (!_qty.HasValue)
                _qty = _partQty;
            return _qty.Value;
        }
    
        set
        {
            _qty = value;
            NotifyPropertyChanged(new PropertyChangedEventArgs("qty"));
        }
    }
    

    只要 qty 不是被反序列化的 Json 的一部分,_qty 将保持为空,直到需要 qty 的值,此时它使用 _partQty 进行初始化。之后,qty 和 partQty 的值是分开的——改变一个不会影响另一个。

    你的其他想法:

    我可以制作 listview bomItems 的副本并在 RequestQuote 中使用,以便原始副本保持不变。这会是个好方法吗?

    这也是一个好主意,这实际上取决于您要完成的工作。例如,如果 BomListModel 打算从服务器获取一次,然后 RequestQuote 将多次使用它来获取不同的报价,那么后一种方法似乎要好得多。它清楚地将只读数据与可写数据分开,因此您无需担心来自一个 RequestQuote 的数据会“溢出”到下一个,因为 qty 是在静态列表中设置的,并且不会在两者之间重置。

    顺便说一句,将实际存储字段(如 _qty、_partQty)设为私有而不是公开通常被认为是一种很好的做法。

    【讨论】:

    • 感谢@DavidS。我用你的第一种方法解决了。我是新手谢谢你的建议。
    • 谢谢。今天我实现了你的第一种方法。它完美地工作。感谢您的见解。
    • 感谢您的上一篇文章。我已将所有存储字段更改为私有。谢谢!!
    猜你喜欢
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 2022-10-25
    • 2011-04-29
    • 2019-02-08
    相关资源
    最近更新 更多