2017-07-12 31 views
0

我有一个使用Android和UWP的Xamarin Forms应用程序。一切工作正常在Android下,但在UWP下我有一个问题,当我想修改屏幕上显示的一些值。带有UWP Refresh的Xamarin表单会使值消失

这是我(简化)视图模型(使用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

(简化)SaleModel类INotifyPropertyChanged接口:

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下的屏幕消失。

回答

0

我测试了您的代码并转载了您的问题。我写下以下代码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> 

enter image description here

我已经上传到code sample GitHub上。请参阅。

+0

我不得不保持网格,因为我有更多的列不只是2个值。但是用StackLayout(垂直方向)环绕的解决方案解决了我的问题。非常感谢! – hugoterelle