Xamarin形成了对ObservableCollection绑定上下文的理解
我在让我的可观察集合绑定到Alexarinman CarouselView时遇到了一些问题。 在阅读了一些基本文章后,我创建了我的视图模型:Xamarin形成了对ObservableCollection绑定上下文的理解,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我在让我的可观察集合绑定到Alexarinman CarouselView时遇到了一些问题。 在阅读了一些基本文章后,我创建了我的视图模型: public class PostObject : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; string postOwner = string.Empty; string i
public class PostObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
string postOwner = string.Empty;
string id = string.Empty;
string profileimage = string.Empty;
string post = string.Empty;
List<string> postimages = null;
public string PostOwner
{
set
{
if (postOwner != value)
{
postOwner = value;
OnPropertyChanged("PostOwner");
}
}
get
{
return postOwner;
}
}
public string Id {
set
{
if (id != value)
{
id = value;
OnPropertyChanged("Id");
}
}
get
{
return id;
}
}
public string Post
{
set
{
if (post != value)
{
post = value;
OnPropertyChanged("Post");
}
}
get
{
return post;
}
}
public string ProfileImage
{
set
{
if (profileimage != value)
{
profileimage = value;
OnPropertyChanged("ProfileImage") ;
}
}
get
{
return profileimage;
}
}
public List<string> PostImages
{
set
{
if (postimages != value)
{
postimages = value;
OnPropertyChanged("PostImages");
}
}
get
{
return postimages;
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
所以我不确定我遗漏了什么,或者我需要多读一点?任何帮助都会很好。您想在这里做一些更改:
将可观察集合传递给底层数据提供程序是一种不好的做法,因为这将限制它仅在UI线程上操作(否则,在非UI线程上更新集合后,可能会使应用程序崩溃)。但这是另一篇文章的顶部:)您想在这里做一些更改:
将可观察集合传递给底层数据提供程序是一种不好的做法,因为这将限制它仅在UI线程上操作(否则,在非UI线程上更新集合后,可能会使应用程序崩溃)。但这是另一篇文章的顶部:)您的BindingContext是GroupPosts,但它不包含属性PostImages,因此对于CarouseViewControl的ItemsSource无效。还有,是哪一行导致了null ref异常?@Jason,我遗漏了这一行:observetecollection GroupPosts=newobservetecollection();PostObject确实包含已填充的PostImages列表。当页面显示在BindingContext=GroupPosts之后时,会发生错误;您的BindingContext是PostObject的集合。每个PostObject都可以有PostImages属性,但集合没有。如果你的群发帖子中有20个对象,那么旋转木马应该如何从中提取帖子图像?@jason是的,我明白你的意思,我在旋转木马中添加了一个,并用它包装,错误消失了,但是没有图像显示出不同的问题。非常感谢。如果你愿意,我会接受你的回答。感谢您指出outyour BindingContext是GroupPosts,但它不包含属性PostImages,因此这对于CarouseViewControl的ItemsSource无效。还有,是哪一行导致了null ref异常?@Jason,我遗漏了这一行:observetecollection GroupPosts=newobservetecollection();PostObject确实包含已填充的PostImages列表。当页面显示在BindingContext=GroupPosts之后时,会发生错误;您的BindingContext是PostObject的集合。每个PostObject都可以有PostImages属性,但集合没有。如果你的群发帖子中有20个对象,那么旋转木马应该如何从中提取帖子图像?@jason是的,我明白你的意思,我在旋转木马中添加了一个,并用它包装,错误消失了,但是没有图像显示出不同的问题。非常感谢。如果你愿意,我会接受你的回答。谢谢你指出这一点
public static bool GetMyPostData(ref ObservableCollection<PostObject> myPosts, string groupid, string apikey)
{
try
{
string newURL = URL + "GetPosts";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
MultipartFormDataContent formdata = new MultipartFormDataContent
{
{ new StringContent(apikey), "apikey" },
{ new StringContent(groupid), "groupid" }
};
HttpResponseMessage response = client.PostAsync(newURL, formdata).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs.
if (response.IsSuccessStatusCode)
{
try
{
myPosts = response.Content.ReadAsAsync<ObservableCollection<PostObject>>().Result;
}
catch (Exception e)
{
Debug.WriteLine(e);
return false;
}
}
}
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return false;
}
}
ObservableCollection<PostObject> GroupPosts = new ObservableCollection<PostObject>();
public Posts (GroupInfo ginfo)
{
InitializeComponent ();
GroupTitle.Text = ginfo.Title;
CurrentGroupInfo = ginfo;
GetDataPosts();
BindingContext = GroupPosts;
}
public void GetDataPosts()
{
try
{
GroupPosts.Clear();
if (RestController.GetMyPostData(ref GroupPosts, CurrentGroupInfo.Id.ToString(), apikey))
{
Debug.WriteLine("Data downloaded");
}
}
catch(Exception e)
{
Debug.WriteLine(e.Message);
}
<?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:controls="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin"
xmlns:cv="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"
NavigationPage.HasNavigationBar="True"
NavigationPage.HasBackButton="False"
NavigationPage.BackButtonTitle="Back"
x:Class="forms.Posts">
<NavigationPage.TitleView>
<StackLayout Orientation="Horizontal" VerticalOptions="Center" Spacing="10" >
<Label x:Name="GroupTitle" TextColor="White" FontSize="Medium"/>
</StackLayout>
</NavigationPage.TitleView>
<ContentPage.ToolbarItems>
<ToolbarItem Name="iconexample" Icon="settings.png" Priority="0" Order="Primary" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
<cv:CarouselViewControl x:Name="carousel"
ItemsSource="{Binding PostImages}"
ShowArrows="true"
ShowIndicators="true"
Orientation="Horizontal">
</cv:CarouselViewControl>
</ContentPage.Content>
</ContentPage>
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
public ObservableCollection<PostObject> GroupPosts { get; } = new ObservableCollection<PostObject>();
private ObservableCollection<PostObject> _groupPosts = new ObservableCollection<PostObject>();
public ObservableCollection<PostObject> GroupPosts
{
get { return _groupPosts; }
set
{
_groupPosts = value;
RaisePropertyChanged(.....); // here you should notify your binding that value has changed
}
}
GroupPosts.Clear();
var newData = RestController.GetMyPostData(CurrentGroupInfo.Id.ToString(), apikey);
GroupPosts = newData;