Xamarin Forms - 棱镜绑定属性 - 渲染问题

时间:2016-09-08 22:22:45

标签: xamarin.forms uwp prism

Git:https://github.com/jimmyt1988/Test

我在UWP本地设备“模拟器”

上运行桌面窗口10个电脑

我的视图模型中有一个深度整数属性,它通过按钮命令递增。

当我这样做时,数字从屏幕上消失,然后如果我调整应用程序的大小,它将再次正确呈现。

发生了什么事?

它似乎适用于Android模拟器。

enter image description here

代码

public DelegateCommand<FoodGroupModel> SubtractFromAmountEatenCommand { get; private set; }

...

SubtractFromAmountEatenCommand = new DelegateCommand<FoodGroupModel>((foodGroup) => SubtractFromAmountEaten(foodGroup));

...

public void SubtractFromAmountEaten(FoodGroupModel foodGroup)
{
    if(foodGroup.AmountEaten != 0)
    {
        foodGroup.AmountEaten--;
    }
}

...

public class FoodGroupModel : BindableBase
{
    private int _amountEaten;
    public int AmountEaten
    {
        get { return _amountEaten; }
        set { SetProperty(ref _amountEaten, value); }
    }
}

XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="...Views.MealPage">

    <StackLayout>
        <Label Text="{Binding Meal.Number}"/>
        <ListView x:Name="FoodGroupsListView" ItemsSource="{Binding Meal.FoodGroups}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="50"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="100" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="50" />
                                </Grid.ColumnDefinitions>

                                <Label Grid.Row="0" Grid.Column="0" TextColor="#bababa" Text="Group"></Label>
                                <Label Grid.Row="0" Grid.Column="1" Text="{Binding Title}" />

                                <Label Grid.Row="0" Grid.Column="2" TextColor="#bababa" Text="Qty"></Label>
                                <Label Grid.Row="0" Grid.Column="3" Text="{Binding AmountEaten}" />

                                <Button Grid.Row="0" Grid.Column="4"
                                        Command="{Binding Source={x:Reference FoodGroupsListView}, Path=BindingContext.UndoAmountEatenByOneCommand}"
                                        CommandParameter="{Binding}"
                                        Text="✖"></Button>

                                <Button Grid.Row="0" Grid.Column="5"
                                        Command="{Binding Source={x:Reference FoodGroupsListView}, Path=BindingContext.SubtractFromAmountEatenCommand}"
                                        CommandParameter="{Binding}"
                                        Text="✔"></Button>
                            </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

</ContentPage>

1 个答案:

答案 0 :(得分:0)

  

当我这样做时,数字从屏幕上消失,然后如果我调整应用程序的大小,它将再次正确呈现。

您可能会遇到一个问题:已更改的财产绑定通知在Xamarin ListView内无法正常工作(可能与重新注册有关),请在此处报告:Bugzilla < / p>

在Windows运行时平台(Win / WP 8.1&amp; UWP)中,我们必须使用一种解决方法来暂时避免这种情况:

private void RefreashFoodGroupModel()
        {
            if (Device.OS == TargetPlatform.Windows || Device.OS == TargetPlatform.WinPhone)
            {
                //var index = Meal.FoodGroups.IndexOf(foodGroup);
                //Meal.FoodGroups.RemoveAt(index);
                //Meal.FoodGroups.Insert(index, foodGroup);

                var t = Meal.FoodGroups;
                Meal.FoodGroups = new System.Collections.ObjectModel.ObservableCollection<FoodGroupModel>();
                foreach (var item in t)
                {
                    Meal.FoodGroups.Add(item);
                }
            }
        }

在数据更改后调用上述方法刷新数据:

public void SubtractFromAmountEaten(FoodGroupModel foodGroup)
        {
            if(foodGroup.AmountEaten != 0)
            {
                foodGroup.AmountEaten--;
                RefreashFoodGroupModel();
            }
        }

        public void UndoAmountEatenByOne(FoodGroupModel foodGroup)
        {
            if (foodGroup.AmountEaten != foodGroup.Quantity)
            {
                foodGroup.AmountEaten++;
                RefreashFoodGroupModel();
            }
        }

此解决方法将导致刷新ListView项目。

enter image description here