C# ImageGalleryControl未触发
我正试图从服务器下载一个以字节为单位的图像,但该图像不会显示。我得到一个合适的字节数组并调整它的大小。它可以从相机添加图片,但从互联网添加图片时不起作用 我已确认图像已正确保存,并已正确下载,因为我可以复制字节数组并使用字节数组字符串显示它 我在调试时比较这两种方法时发现了问题,在C# ImageGalleryControl未触发,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我正试图从服务器下载一个以字节为单位的图像,但该图像不会显示。我得到一个合适的字节数组并调整它的大小。它可以从相机添加图片,但从互联网添加图片时不起作用 我已确认图像已正确保存,并已正确下载,因为我可以复制字节数组并使用字节数组字符串显示它 我在调试时比较这两种方法时发现了问题,在executerpickCommand中,它会触发我的“ItemSourceChanged”方法,但不会使用AddImages方法触发 藏品 public class ImageGalleryPageModel {
executerpickCommand
中,它会触发我的“ItemSourceChanged”
方法,但不会使用AddImages
方法触发
藏品
public class ImageGalleryPageModel
{
public ObservableCollection<ImageModel> Images
{
get { return images; }
}
private ObservableCollection<ImageModel> images = new ObservableCollection<ImageModel>();
}
公共类ImageGalleryPageModel
{
公众可观测采集图像
{
获取{返回图像;}
}
私有ObservableCollection图像=新ObservableCollection();
}
这可以添加这个类中的图片
private async Task ExecutePickCommand()
{
MediaFile file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
byte[] imageAsBytes;
using (MemoryStream memoryStream = new MemoryStream())
{
file.GetStream().CopyTo(memoryStream);
file.Dispose();
imageAsBytes = memoryStream.ToArray();
}
if (imageAsBytes.Length > 0)
{
IImageResizer resizer = DependencyService.Get<IImageResizer>();
imageAsBytes = resizer.ResizeImage(imageAsBytes, 1080, 1080);
ImageSource imageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
Images.Add(new ImageModel { Source = imageSource, OrgImage = imageAsBytes });
}
}
private异步任务ExecutePickCommand()
{
MediaFile file=wait CrossMedia.Current.PickPhotoAsync();
if(file==null)
返回;
字节[]图像字节;
使用(MemoryStream MemoryStream=new MemoryStream())
{
file.GetStream().CopyTo(memoryStream);
Dispose();
imageAsBytes=memoryStream.ToArray();
}
如果(imageAsBytes.Length>0)
{
IImageResizer resizer=DependencyService.Get();
imageAsBytes=resizer.ResizeImage(imageAsBytes,10801080);
ImageSource ImageSource=ImageSource.FromStream(()=>new MemoryStream(imageAsBytes));
Add(新的ImageModel{Source=imageSource,OrgImage=imageAsBytes});
}
}
然后我下载这些图片并将它们放入收藏
private void AddTheImages(int imageIssueId)
{
var imageData = App.Client.GetImage(imageIssueId);
byte[] imageAsBytes = imageData.Item1;
if (imageAsBytes.Length > 0)
{
IImageResizer resizer = DependencyService.Get<IImageResizer>();
imageAsBytes = resizer.ResizeImage(imageAsBytes, 1080, 1080);
ImageSource imageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
ImageGalleryViewModel.Images.Add(new ImageModel { Source = imageSource, OrgImage = imageAsBytes });
}
}
private void添加图像(int-imagesissueid)
{
var imageData=App.Client.GetImage(imageIssueId);
字节[]imageAsBytes=imageData.Item1;
如果(imageAsBytes.Length>0)
{
IImageResizer resizer=DependencyService.Get();
imageAsBytes=resizer.ResizeImage(imageAsBytes,10801080);
ImageSource ImageSource=ImageSource.FromStream(()=>new MemoryStream(imageAsBytes));
ImageGalleryViewModel.Images.Add(新的ImageModel{Source=imageSource,OrgImage=imageAsBytes});
}
}
Xaml
...
...
这是控件,这是itemsourcechanged,它不是触发的
private readonly StackLayout imageStack;
public ImageGalleryControl()
{
this.Orientation = ScrollOrientation.Horizontal;
imageStack = new StackLayout
{
Orientation = StackOrientation.Horizontal
};
this.Content = imageStack;
}
public new IList<View> Children
{
get { return imageStack.Children; }
}
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create<ImageGalleryControl, IList>
(
view => view.ItemsSource,
default(IList),
BindingMode.TwoWay,
propertyChanging: (bindableObject, oldValue, newValue) =>
{
((ImageGalleryControl)bindableObject).ItemsSourceChanging();
},
propertyChanged: (bindableObject, oldValue, newValue) =>
{
((ImageGalleryControl)bindableObject).ItemsSourceChanged(bindableObject, oldValue, newValue);
}
);
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
private void ItemsSourceChanging()
{
if (ItemsSource == null)
return;
}
private void CreateNewItem(IList newItem)
{
View view = (View)ItemTemplate.CreateContent();
if (view is BindableObject bindableObject)
bindableObject.BindingContext = newItem;
imageStack.Children.Add(view);
}
private void ItemsSourceChanged(BindableObject bindable, IList oldValue, IList newValue)
{
if (ItemsSource == null)
return;
if (newValue is INotifyCollectionChanged notifyCollection)
{
notifyCollection.CollectionChanged += (sender, args) =>
{
if (args.NewItems != null)
{
if (args.NewItems.Count > 0)
{
foreach (object newItem in args.NewItems)
{
View view = (View)ItemTemplate.CreateContent();
if (view is BindableObject bindableObject)
bindableObject.BindingContext = newItem;
imageStack.Children.Add(view);
}
}
}
else
{
imageStack.Children.Clear();
foreach (object item in ItemsSource)
{
View view = (View)ItemTemplate.CreateContent();
BindableObject bindableObject = (BindableObject) view;
if (bindableObject != null)
bindableObject.BindingContext = item;
imageStack.Children.Add(view);
}
}
if (args.OldItems != null)
{
// not supported
}
};
}
}
public DataTemplate ItemTemplate
{
get;
set;
}
public static readonly BindableProperty SelectedItemProperty =
BindableProperty.Create<ImageGalleryControl, object>
(
view => view.SelectedItem,
null,
BindingMode.TwoWay,
propertyChanged: (bindable, oldValue, newValue) =>
{
((ImageGalleryControl)bindable).UpdateSelectedIndex();
}
);
public object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
private void UpdateSelectedIndex()
{
if (SelectedItem == BindingContext)
return;
SelectedIndex = Children
.Select(c => c.BindingContext)
.ToList()
.IndexOf(SelectedItem);
}
public static readonly BindableProperty SelectedIndexProperty =
BindableProperty.Create<ImageGalleryControl, int>
(
carousel => carousel.SelectedIndex,
0,
BindingMode.TwoWay,
propertyChanged: (bindable, oldValue, newValue) =>
{
((ImageGalleryControl)bindable).UpdateSelectedItem();
}
);
public int SelectedIndex
{
get { return (int)GetValue(SelectedIndexProperty); }
set { SetValue(SelectedIndexProperty, value); }
}
private void UpdateSelectedItem()
{
SelectedItem = SelectedIndex > -1 ? Children[SelectedIndex].BindingContext : null;
}
}
private readonly StackLayout imageStack;
公共图像库控件()
{
this.Orientation=ScrollOrientation.Horizontal;
imageStack=新StackLayout
{
方向=堆叠方向。水平
};
this.Content=imageStack;
}
公共新移民儿童
{
获取{return imageStack.Children;}
}
公共静态只读BindableProperty项SourceProperty=
BindableProperty.Create
(
view=>view.ItemsSource,
违约(IList),
BindingMode.TwoWay,
属性更改:(bindableObject、oldValue、newValue)=>
{
((ImageGalleryControl)bindableObject).ItemsSourceChange();
},
propertyChanged:(bindableObject、oldValue、newValue)=>
{
((ImageGalleryControl)bindableObject).ItemsSourceChanged(bindableObject,oldValue,newValue);
}
);
公共IList项目资源
{
get{return(IList)GetValue(ItemsSourceProperty);}
set{SetValue(ItemsSourceProperty,value);}
}
私有无效项资源更改()
{
if(ItemsSource==null)
返回;
}
私有void CreateNewItem(IList newItem)
{
视图=(视图)ItemTemplate.CreateContent();
if(视图为BindableObject BindableObject)
bindableObject.BindingContext=newItem;
imageStack.Children.Add(视图);
}
私有无效项SourceChanged(BindableObject bindable、IList oldValue、IList newValue)
{
if(ItemsSource==null)
返回;
if(newValue为INotifyCollectionChanged notifyCollection)
{
notifyCollection.CollectionChanged+=(发件人,参数)=>
{
如果(args.NewItems!=null)
{
如果(args.NewItems.Count>0)
{
foreach(args.NewItems中的对象newItem)
{
视图=(视图)ItemTemplate.CreateContent();
if(视图为BindableObject BindableObject)
bindableObject.BindingContext=newItem;
imageStack.Children.Add(视图);
}
}
}
其他的
{
imageStack.Children.Clear();
foreach(ItemsSource中的对象项)
{
视图=(视图)ItemTemplate.CreateContent();
BindableObject BindableObject=(BindableObject)视图;
if(bindableObject!=null)
bindableObject.BindingContext=项;
imageStack.Children.Add(视图);
}
}
如果(args.OldItems!=null)
{
//不支持
}
};
}
}
公共数据模板ItemTemplate
{
得到;
设置
}
公共静态只读BindableProperty SelectedItemProperty=
可绑定的
private readonly StackLayout imageStack;
public ImageGalleryControl()
{
this.Orientation = ScrollOrientation.Horizontal;
imageStack = new StackLayout
{
Orientation = StackOrientation.Horizontal
};
this.Content = imageStack;
}
public new IList<View> Children
{
get { return imageStack.Children; }
}
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create<ImageGalleryControl, IList>
(
view => view.ItemsSource,
default(IList),
BindingMode.TwoWay,
propertyChanging: (bindableObject, oldValue, newValue) =>
{
((ImageGalleryControl)bindableObject).ItemsSourceChanging();
},
propertyChanged: (bindableObject, oldValue, newValue) =>
{
((ImageGalleryControl)bindableObject).ItemsSourceChanged(bindableObject, oldValue, newValue);
}
);
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
private void ItemsSourceChanging()
{
if (ItemsSource == null)
return;
}
private void CreateNewItem(IList newItem)
{
View view = (View)ItemTemplate.CreateContent();
if (view is BindableObject bindableObject)
bindableObject.BindingContext = newItem;
imageStack.Children.Add(view);
}
private void ItemsSourceChanged(BindableObject bindable, IList oldValue, IList newValue)
{
if (ItemsSource == null)
return;
if (newValue is INotifyCollectionChanged notifyCollection)
{
notifyCollection.CollectionChanged += (sender, args) =>
{
if (args.NewItems != null)
{
if (args.NewItems.Count > 0)
{
foreach (object newItem in args.NewItems)
{
View view = (View)ItemTemplate.CreateContent();
if (view is BindableObject bindableObject)
bindableObject.BindingContext = newItem;
imageStack.Children.Add(view);
}
}
}
else
{
imageStack.Children.Clear();
foreach (object item in ItemsSource)
{
View view = (View)ItemTemplate.CreateContent();
BindableObject bindableObject = (BindableObject) view;
if (bindableObject != null)
bindableObject.BindingContext = item;
imageStack.Children.Add(view);
}
}
if (args.OldItems != null)
{
// not supported
}
};
}
}
public DataTemplate ItemTemplate
{
get;
set;
}
public static readonly BindableProperty SelectedItemProperty =
BindableProperty.Create<ImageGalleryControl, object>
(
view => view.SelectedItem,
null,
BindingMode.TwoWay,
propertyChanged: (bindable, oldValue, newValue) =>
{
((ImageGalleryControl)bindable).UpdateSelectedIndex();
}
);
public object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
private void UpdateSelectedIndex()
{
if (SelectedItem == BindingContext)
return;
SelectedIndex = Children
.Select(c => c.BindingContext)
.ToList()
.IndexOf(SelectedItem);
}
public static readonly BindableProperty SelectedIndexProperty =
BindableProperty.Create<ImageGalleryControl, int>
(
carousel => carousel.SelectedIndex,
0,
BindingMode.TwoWay,
propertyChanged: (bindable, oldValue, newValue) =>
{
((ImageGalleryControl)bindable).UpdateSelectedItem();
}
);
public int SelectedIndex
{
get { return (int)GetValue(SelectedIndexProperty); }
set { SetValue(SelectedIndexProperty, value); }
}
private void UpdateSelectedItem()
{
SelectedItem = SelectedIndex > -1 ? Children[SelectedIndex].BindingContext : null;
}
}