【发布时间】:2021-02-01 01:01:21
【问题描述】:
我的产品页面只显示“产品名称”和“数量”,数量被显示/绑定到选择器。
出于测试目的,仅从 VM 加载了 2 个产品。葡萄酒 1 和葡萄酒 2。
应用程序加载时,为什么选择器是空的,没有选择任何值。当每个项目的数量设置为 1 时,从 VM 加载时
数量设置为 1,选择器在最初加载时没有更新,我知道这是因为如果我单击空选择器并选择 1。没有任何反应,因为代码命中
if (quantity != value)
//其中选择的数量1在后面的代码中已经是1,所以不会在数量中从setter调用propertyChanged,
还有... 如果我选择选择器并选择另一个数字,例如 4。数量上的setter被击中,OnPropertyChanged被击中,picker上显示4。 (如果选择了 3,我什至测试了更改产品名称) 这行得通。
我知道这一切都有效,因为我已经逐步完成了代码。 但是现在发生的另一个问题是,由于某种原因,代码在每次单击后再次达到 get/set AGAIN 的数量并将值设置为 0。
例如,如果单击 4,则选择器将在屏幕上更新为 4,然后如果我通过单步执行代码,再次调用数量设置器,将值设置为 0,因此什么都不选择在选择器中。将其留空,就像它最初加载时一样。
任何有助于解决此问题的帮助将不胜感激,感谢 Y
感谢 Y 提供的任何帮助
public class ProductModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int quantity;
private string productName;
public ProductModel()
{ }
[PrimaryKey, AutoIncrement]
public int ProductId { get; set; }
[MaxLength(50)]
public string ProductName
{
get
{
return productName;
}
set
{
productName = value;
OnPropertyChanged();
}
}
public int Quantity
{
get
{
return quantity;
}
set
{
if (quantity != value)
{
quantity = value;
OnPropertyChanged();
//test to check if binding works, successfully changed ProductName to "test" if 3 is picked from picker
if (quantity == 3)
ProductName = "test;";
}
}
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(name));
}
}
public class ProductPageViewModel : BindableObject
{
public ObservableCollection<ProductModel> WineList { get; set; }
public ProductPageViewModel ()
{
WineList = new ObservableCollection<ProductModel>();
WineList.Add(new ProductModel { ProductId = 1, ProductName = "Wine 1", Quantity = 1});
WineList.Add(new ProductModel { ProductId = 2, ProductName = "Wine 2", Quantity = 1});
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ScrollApp2.Views.ProductPage">
<ContentPage.Content>
<StackLayout>
<ListView x:Name="producttablelist" IsVisible="True" VerticalOptions="FillAndExpand" HasUnevenRows="True" ItemsSource="{Binding WineList}" HeightRequest="1500">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HeightRequest="120" BackgroundColor="Green" HorizontalOptions="StartAndExpand">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding ProductName}" TextColor="Black" VerticalOptions="Start"></Label>
<Picker Grid.Column="1" Grid.Row="0" SelectedItem="{Binding Quantity,Mode=TwoWay}">
<Picker.Items>
<x:String>0</x:String>
<x:String>1</x:String>
<x:String>2</x:String>
<x:String>3</x:String>
<x:String>4</x:String>
<x:String>5</x:String>
<x:String>6</x:String>
</Picker.Items>
</Picker>
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
namespace ScrollApp2.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ProductPage : ContentPage
{
public ProductPage()
{
InitializeComponent();
BindingContext = new ProductPageViewModel();
}
}
}
【问题讨论】:
-
您不需要在数量选择器上使用“SelectedIndexChanged="QuantityChanged"”,因为您已绑定到属性,您可以在属性更改时简单地处理该代码。您需要将以下代码添加到空白的“ProductModel”构造函数中。 “this.PropertyChanged += OnPropertyChanged”,并重写捕获事件的方法。然后在属性发生变化时处理所需的代码 == nameof(Quantity)
-
感谢您的回答,我已尽力按照您的建议进行操作,但遇到了困难,请查看更新后的 Q 任何帮助将不胜感激,谢谢 Y
-
是的,你现在很接近了。我将转到一个实际的帖子,以便更轻松地格式化代码。
标签: c# xaml xamarin xamarin.forms picker