Xamarin Forms with UWP Refresh 导致值消失

Xamarin Forms with UWP Refresh causes value to disappear

我有一个使用 Android 和 UWP 的 Xamarin Forms 应用程序。在 Android 下一切正常,但在 UWP 下,当我想修改屏幕上显示的某些值时遇到问题。

这是我的(简化的)ViewModel(使用 Fody.PropertyChanged):

public class SalesViewModel : BaseAppViewModel
{
    public ObservableCollection<SaleModel> Sales { get; set; } 
        = new ObservableCollection<SaleModel>();

    [DoNotNotify]
    public ICommand AddQuantityCommand => new Command<SaleModel>(item =>
    {
        item.Quantity += 1;
    });
}

BaseAppViewModel 实现 Fody.PropertyChanged

的 INotifyPropertyChanged 接口

(简化的)SaleModel class:

public class SaleModel  : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public double Quantity { get; set; }
}

以及显示 SaleModel 列表的 XAML 的(部分)

<ListView x:Name="ListView" ItemsSource="{Binding Sales, Mode=TwoWay}" SelectedItem="{Binding SelectedSale, Mode=TwoWay}">   
<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <ViewCell.View>
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
          </Grid.ColumnDefinitions>

          <Image Grid.Column="0" Source="arrow.png">
            <Image.GestureRecognizers>
              <TapGestureRecognizer Command="{Binding Path=BindingContext.AddQuantityCommand, Source={x:Reference Name=SalesPage}}" CommandParameter="{Binding .}" />
            </Image.GestureRecognizers>
          </Image>

          <Label Grid.Column="1" Text="{Binding Quantity, Mode=OneWay}" 
        </Grid>
      </ViewCell.View>
     </ViewCell>
    </DataTemplate>
   </ListView.ItemTemplate>
 </ListView>

在 UWP 下点击图片时,数量增加但数值从屏幕上消失。

我已经测试了你的代码并重现了你的问题。我写了下面的 ViewCell 替换你的。请尝试修改您的ViewCell.

<ViewCell>
    <StackLayout BackgroundColor="#eee"
    Orientation="Vertical">
        <StackLayout Orientation="Horizontal">
            <Image Source="time.jpg" HeightRequest="50" WidthRequest="50" >
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding AddQuantityCommand} " CommandParameter="{Binding .}"/>
                </Image.GestureRecognizers>
            </Image>
            <Label Text="{Binding Quantity}"
            TextColor="#f35e20" />
            <Label Text="{Binding Quantity}"
            HorizontalOptions="EndAndExpand"
            TextColor="#503026" />
        </StackLayout>
    </StackLayout>
</ViewCell>

我已经上传 code sample 到 github。请参考。