简体   繁体   English

使用MVVM在Windows Phone 8.1应用程序上进行TwoWay绑定

[英]TwoWay Binding on Windows Phone 8.1 app, using MVVM

I'm developing a sales management system using MVVM , but I have problems to save data entered on view. 我正在使用MVVM开发销售管理系统,但是我在保存视图中输入的数据方面存在问题。 I'm assuming that TwoWay Binding mean that all values entered on views should be passed to the ViewModel and vice-versa, am I right? 我假设TwoWay Binding意味着在视图上输入的所有值都应传递给ViewModel,反之亦然,我是对的吗?

View : 查看

 <TextBox x:Name="NameText"
             Grid.ColumnSpan="2"
             Grid.Row="2"
             Header="Nombre:"
             Style="{StaticResource RegisterTextBoxStyle}"
             Text="{Binding Product.Name, Mode=TwoWay}"/>
    <ComboBox x:Name="UnitCombo"
              Grid.Row="3"
              Grid.ColumnSpan="2"
              Header="Unidad:"
              PlaceholderText="Elige la medida"
              Style="{StaticResource RegisterComboBoxStyle}"
              ItemsSource="{Binding Path=UnitsSource, Mode=OneWay}"
              SelectedValue="{Binding SelectedUnit, Mode=TwoWay}"/>
    <TextBox x:Name="CostText"
             Grid.Column="0"
             Grid.Row="4"
             Header="Costo:"
             Style="{StaticResource RegisterTextBoxStyle}"
             Text="{Binding Product.Cost, Mode=TwoWay}"/>
    <TextBox x:Name="PriceText"
             Grid.Column="1"
             Grid.Row="4"
             Header="Precio:"
             Style="{StaticResource RegisterTextBoxStyle}"
             Text="{Binding Product.Price, Mode=TwoWay}"/>
    <ToggleSwitch x:Name="ActiveToggle"
                  Grid.Column="1"
                  Grid.Row="5"
                  Style="{StaticResource RegisterToggleSwithStyle}"
                  IsOn="{Binding Product.Active, Mode=TwoWay}"/>

View uses SaveProduct command to save all the values entered on the view, and it's called from: View使用SaveProduct命令保存在视图上输入的所有值,并从以下位置调用:

        <AppBarButton Icon="Accept" Label="Añadir" Command="{Binding Path=SaveCommand}"/>

I stablished DataContext on codebehind: 我在codebehind上建立了DataContext

public AddProduct()
    {
        InitializeComponent();
        DataContext = new ProductListViewModel();
    }

ProductListViewModel : ProductListViewModel

namespace BillingShop.ViewModel
{
    public class ProductListViewModel : ViewModelBase, INavigable
    {
        public ObservableCollection<ProductViewModel> Items { get; private set; }
    private DelegateCommand _saveProduct;
    public bool IsUpdating { get; set; }
    public ProductViewModel Product { get; set; }
    public Visibility UpdatingVisibility => (IsUpdating || Items == null || Items.Count == 0) ? Visibility.Visible : Visibility.Collapsed;

    public ProductListViewModel()
    {
        if (IsInDesignMode)
        {
            return;
        }
        _saveProduct = new DelegateCommand(SaveCommand_Executed);
    }

    #region Product Members

    private string _unit;
    public string SelectedUnit
    {
        get { return _unit; }
        set
        {
            _unit = value;
            OnPropertyChanged();
        }
    }

    #endregion


    public IEnumerable<RegisteredUnits> UnitsSource => Enum.GetValues(typeof(RegisteredUnits)).Cast<RegisteredUnits>();

    public ICommand SaveCommand => _saveProduct;

    private void SaveCommand_Executed()
    {
        var product = new Product
        {
            Name = Product.Name,
            Unit = Product.Unit,
            Cost = Convert.ToDouble(Product.Cost),
            Price = Convert.ToDouble(Product.Price),
            Active = Convert.ToBoolean(Product.Active)
        };
        ProductManager.SaveProduct(product);
    }

    public void PopulateProductViewModel(ProductViewModel entry)
    {
        Product = entry;
        OnPropertyChanged("Product");
    }
    public Product GetProduct()
    {
        return Product?.GetProduct();
    }
}

} }

ProductViewModel : ProductViewModel

public class ProductViewModel : ViewModelBase
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Unit { get; set; }
    public double Cost { get; set; }
    public double Price { get; set; }
    public bool Active { get; set; }
    public List<SalesDetails> SalesDetail { get; set; }

    public ProductViewModel()
    {
    }

    public ProductViewModel(Product item)
    {
        Update(item);
    }
    public void Update (Product item)
    {
        Id = item.ID;
        Name = item.Name;
        Unit = item.Unit;
        Cost = item.Cost;
        Price = item.Price;
        Active = item.Active;
        SalesDetail = item.SalesDetail;
    }

    public Product GetProduct()
    {
        return new Product
        {
            ID = Id,
            Name = Name,
            Unit = Unit,
            Cost = Cost,
            Price = Price,
            Active = Active,
            SalesDetail = SalesDetail
        };
    }
}

When SaveProduct is executed, Product class shows as null, what about my view values? 执行SaveProduct时Product类显示为null,我的视图值如何? What is wrong with my code? 我的代码出了什么问题?

Thanks to everyone who helps me, and I'm putting here my repository link, maybe someone wants to look deep on my code: https://github.com/adoibarra/BillingShop 感谢所有帮助我的人,我在这里放置我的存储库链接,也许有人想深入了解我的代码: https//github.com/adoibarra/BillingShop

Edit : Here's my Product class : 编辑 :这是我的产品类

public class Product : IBusinessEntity
{
    /// <summary>
    /// Represents a Product.
    /// </summary>
    public Product()
    {
    }


    /// <summary>
    /// Get or sets the product identifier.
    /// </summary>
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    /// <summary>
    /// Get or sets the product name.
    /// </summary>
    [MaxLength(40)]
    public string Name { get; set; }
    /// <summary>
    /// Get or sets if the product can be measured in units or kilograms.
    /// </summary>
    public string Unit { get; set; }
    /// <summary>
    /// Get or sets the product cost.
    /// </summary>
    public double Cost { get; set; }
    /// <summary>
    /// Get or sets the product price.
    /// </summary>
    public double Price { get; set; }
    /// <summary>
    /// Get or sets the product state.
    /// </summary>
    public bool Active { get; set; }
    /// <summary>
    /// One-To-Many relationship with SalesDetails.
    /// </summary>
    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public List<SalesDetails> SalesDetail { get; set; }


}

The solution was initialize Product property inside ProductListViewModel constructor: 解决方案是在ProductListViewModel构造函数中初始化Product属性:

public class ProductListViewModel
{
   public ProductListViewModel ()
   {
      Product = new ProductViewModel();
   }
   .
   .
   .
}

Thanks to Weimar Yamit Moreno Perez for his help, and Archana for his/her intervention. 感谢Weimar Yamit Moreno Perez的帮助,以及Archana的干预。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM