NotifyProperty不会触发

时间:2018-01-30 14:21:43

标签: c# xaml xamarin

我在同一页面上有两个按钮,一个位于我的画廊下方,另一个位于底部导航栏中。两个命令都会触发但底部命令会被触发。它永远不会进入我的NOTIFYProperyChanged

  <Label Grid.Row="0" Grid.Column="0" Text="IMAGES" XAlign="Start" YAlign="Center" Style="{StaticResource Labelfont}" TextColor="White"/>
                <Grid x:Name="ImageGallerys">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*">
                        </RowDefinition>
                        <RowDefinition Height="128">
                        </RowDefinition>
                        <RowDefinition Height="Auto">
                        </RowDefinition>
                    </Grid.RowDefinitions>
                    <Image 
                        Source="{Binding PreviewImage}" Grid.Row="0">
                    </Image>
                    <custom:ImageGallery ItemsSource="{Binding Images}" Grid.Row="1">
                        <custom:ImageGallery.ItemTemplate>
                            <DataTemplate>
                                <Image Source="{Binding Source}" Aspect="AspectFit">
                                    <Image.GestureRecognizers>
                                        <TapGestureRecognizer Command="{Binding Path=BindingContext.PreviewImageCommand, Source={x:Reference EditIssue}}" CommandParameter="{Binding ImageId}" />
                                    </Image.GestureRecognizers>
                                </Image>
                            </DataTemplate>
                        </custom:ImageGallery.ItemTemplate>
                    </custom:ImageGallery>
                    <Button 
                        Grid.Row="2" Text="Photo" Command="{Binding CameraCommand}">
                    </Button>
                    <Button 
                        Grid.Row="2" Text="Pick" Command="{Binding PickCommand}">
                    </Button>
                </Grid>



<!--Bottom Navigation Bar-->
        <Grid Grid.Row="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="50" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="20" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="20" />
            </Grid.ColumnDefinitions>
        <StackLayout x:Name="slButtons" Grid.ColumnSpan="3" Grid.Row="1" Orientation="Horizontal" HorizontalOptions="FillAndExpand" Spacing="5" BackgroundColor="White">
            <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand"  BackgroundColor="#FBB040">
                <Button 
                    Grid.Row="2" Text="Pick" Command="{Binding PickCommand}">
                </Button>
            </StackLayout>

这是应该调用NotifyProperty之前的方法。

public async Task ExecutePickCommand()
        {
            var file = await CrossMedia.Current.PickPhotoAsync();

            if (file == null)
                return;

            byte[] imageAsBytes = null;
            using (var memoryStream = new MemoryStream())
            {
                file.GetStream().CopyTo(memoryStream);
                file.Dispose();
                imageAsBytes = memoryStream.ToArray();
            }

            if (imageAsBytes.Length > 0)
            {
                var resizer = DependencyService.Get<IImageResize>();
                imageAsBytes = resizer.ResizeImage(imageAsBytes, 1080, 1080);

                var imageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
                _images.Add(new GalleryImage { Source = imageSource, OrgImage = imageAsBytes });
            }
        }

这是Notifypropery部分,它应该像我的第一个按钮一样进入方法ItemSourceChange,第二个按钮就在它之后结束。

 public static readonly BindableProperty ItemsSourceProperty =
            BindableProperty.Create<ImageGallery, IList>(
                view => view.ItemsSource,
                default(IList),
                BindingMode.TwoWay,
                propertyChanging: (bindableObject, oldValue, newValue) => {
                    ((ImageGallery)bindableObject).ItemsSourceChanging();
                },
                propertyChanged: (bindableObject, oldValue, newValue) => {
                    ((ImageGallery)bindableObject).ItemsSourceChanged(bindableObject, oldValue, newValue);
                }
            );

        public IList ItemsSource
        {
            get
            {
                return (IList)GetValue(ItemsSourceProperty);
            }
            set
            {

                SetValue(ItemsSourceProperty, value);
            }
        }

        void ItemsSourceChanging()
        {
            if (ItemsSource == null)
                return;
        }


        void ItemsSourceChanged(BindableObject bindable, IList oldValue, IList newValue)
        {
            if (ItemsSource == null)
                return;

            var notifyCollection = newValue as INotifyCollectionChanged;
            if (notifyCollection != null)
            {
                notifyCollection.CollectionChanged += (sender, args) => {
                    if (args.NewItems != null)
                    {
                        if (args.NewItems.Count > 0)
                        {
                            foreach (var newItem in args.NewItems)
                            {
                                var view = (View)ItemTemplate.CreateContent();
                                var bindableObject = view as BindableObject;
                                if (bindableObject != null)
                                    bindableObject.BindingContext = newItem;
                                _imageStack.Children.Add(view);
                            }
                        }
                    }
                    else
                    {
                        _imageStack.Children.Clear();
                        foreach (var Item in ItemsSource)
                        {
                            var view = (View)ItemTemplate.CreateContent();
                            var bindableObject = view as BindableObject;
                            if (bindableObject != null)
                                bindableObject.BindingContext = Item;
                            _imageStack.Children.Add(view);
                        }
                    }
                    if (args.OldItems != null)
                    {
                        // not supported
                    }
                };

我发现如果我这样做,它最终会像上面的按钮一样调用notifyproperty。但由于此按钮中的nullreference错误,它现在崩溃,因为它似乎创建了另一个实例或什么?

<custom:ImageGallery ItemsSource="{Binding Images}" Grid.Row="1" x:Name="ImageGalleryss">
                        <Button 
                        Grid.Row="3" Text="Pick" Command="{Binding PickCommand}">
                        </Button>
                </custom:ImageGallery>

0 个答案:

没有答案