C# NotifyProperty未触发
我在同一页上有两个按钮,一个在我的图库下方,另一个在底部导航栏中。两个命令都会触发,但底部的命令已被破坏。它永远不会去我的房间C# NotifyProperty未触发,c#,xaml,xamarin,C#,Xaml,Xamarin,我在同一页上有两个按钮,一个在我的图库下方,另一个在底部导航栏中。两个命令都会触发,但底部的命令已被破坏。它永远不会去我的房间 <Label Grid.Row="0" Grid.Column="0" Text="IMAGES" XAlign="Start" YAlign="Center" Style="{StaticResource Labelfont}" TextColor="White"/> <Grid x:Name="ImageGall
<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之前的方法
<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>
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 });
}
}
public异步任务ExecutePickCommand()
{
var file=await CrossMedia.Current.PickPhotoAsync();
if(file==null)
返回;
字节[]imageAsBytes=null;
使用(var memoryStream=new memoryStream())
{
file.GetStream().CopyTo(memoryStream);
Dispose();
imageAsBytes=memoryStream.ToArray();
}
如果(imageAsBytes.Length>0)
{
var resizer=DependencyService.Get();
imageAsBytes=resizer.ResizeImage(imageAsBytes,10801080);
var imageSource=imageSource.FromStream(()=>newmemoryStream(imageAsBytes));
_Add(新的GalleryImage{Source=imageSource,OrgImage=imageAsBytes});
}
}
这是Notifypropery部分,它应该进入方法ItemSourceChange,就像它在我的第一个按钮中所做的一样,在第二个按钮之后它就结束了
<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>
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
}
};
公共静态只读BindableProperty项资源属性=
BindableProperty.Create(
view=>view.ItemsSource,
违约(IList),
BindingMode.TwoWay,
属性更改:(bindableObject、oldValue、newValue)=>{
((ImageGallery)bindableObject.ItemsSourceChange();
},
propertyChanged:(bindableObject、oldValue、newValue)=>{
((ImageGallery)bindableObject).ItemsSourceChanged(bindableObject,oldValue,newValue);
}
);
公共IList项目资源
{
得到
{
返回(IList)GetValue(ItemsSourceProperty);
}
设置
{
设置值(ItemsSourceProperty,value);
}
}
作废项目采购更改()
{
if(ItemsSource==null)
返回;
}
void ItemsSourceChanged(BindableObject bindable、IList oldValue、IList newValue)
{
if(ItemsSource==null)
返回;
var notifyCollection=INOTIFYCOLLECTION更改时的新值;
if(notifyCollection!=null)
{
notifyCollection.CollectionChanged+=(发件人,参数)=>{
如果(args.NewItems!=null)
{
如果(args.NewItems.Count>0)
{
foreach(args.NewItems中的var newItem)
{
var view=(view)ItemTemplate.CreateContent();
var bindableObject=作为bindableObject查看;
if(bindableObject!=null)
bindableObject.BindingContext=newItem;
_imageStack.Children.Add(视图);
}
}
}
其他的
{
_imageStack.Children.Clear();
foreach(ItemsSource中的var项)
{
var view=(view)ItemTemplate.CreateContent();
var bindableObject=作为bindableObject查看;
if(bindableObject!=null)
bindableObject.BindingContext=项;
_imageStack.Children.Add(视图);
}
}
如果(args.OldItems!=null)
{
//不支持
}
};
我发现如果我这样做了,它最终会像上面的按钮一样调用notifyproperty。但是现在由于这个按钮中的nullreference错误,它崩溃了,因为它似乎创建了另一个实例或其他什么
<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>
<custom:ImageGallery ItemsSource="{Binding Images}" Grid.Row="1" x:Name="ImageGalleryss">
<Button
Grid.Row="3" Text="Pick" Command="{Binding PickCommand}">
</Button>
</custom:ImageGallery>
是\u images
一个可观察到的采集
是“可观察到的采集”\u images=新的可观察到的采集()奇怪的是,它与本地网格中的按钮一起工作,但底部导航栏中的按钮会触发,但notifyproperty在本地网格之外的任何地方都不起作用,所以可能需要自定义库?