【问题标题】:Xamarin Forms with UWP Refresh causes value to disappear具有 UWP 刷新的 Xamarin 表单导致值消失
【发布时间】:2017-12-17 09:47:12
【问题描述】:

我有一个使用 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 类:

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

【问题讨论】:

    标签: xaml xamarin uwp xamarin.forms xamarin.forms.listview


    【解决方案1】:

    我已经测试了您的代码并重现了您的问题。我写了以下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。请参考。

    【讨论】:

    • 我不得不保留网格,因为我的列数远不止 2 个值。但是您使用 StackLayout(垂直方向)环绕的解决方案解决了我的问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2018-03-28
    • 2013-09-29
    • 1970-01-01
    • 2022-12-31
    相关资源
    最近更新 更多