簡體   English   中英

使用MVVM在Windows Phone 8.1應用程序上進行TwoWay綁定

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

我正在使用MVVM開發銷售管理系統,但是我在保存視圖中輸入的數據方面存在問題。 我假設TwoWay Binding意味着在視圖上輸入的所有值都應傳遞給ViewModel,反之亦然,我是對的嗎?

查看

 <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使用SaveProduct命令保存在視圖上輸入的所有值,並從以下位置調用:

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

我在codebehind上建立了DataContext

public AddProduct()
    {
        InitializeComponent();
        DataContext = new 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

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
        };
    }
}

執行SaveProduct時Product類顯示為null,我的視圖值如何? 我的代碼出了什么問題?

感謝所有幫助我的人,我在這里放置我的存儲庫鏈接,也許有人想深入了解我的代碼: https//github.com/adoibarra/BillingShop

編輯 :這是我的產品類

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; }


}

解決方案是在ProductListViewModel構造函數中初始化Product屬性:

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

感謝Weimar Yamit Moreno Perez的幫助,以及Archana的干預。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM