2017-07-12 32 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