Hi,
I'm creating a page with a ListView, and after selecting one of the objects and clicking on it, it should bring you to a details page for the item.
Everything works from the ListView, but I get an error after it tries to apply the BindingContext to the details page after an item is selected.
I'm not sure what I'm doing wrong.
Please advise...
I have packt all into a zip. Just put them into a portable project and adjust namespace, to get it to work...
The .Zip can be downloaded here: https://drive.google.com/open?id=0B3hQIMQb2QHmamFsZ0ZxVHc0LWM
(FYI: Error occurs in ListingPage line: 40 => page.BindingContext = ud)
public class ListingPage : ContentPage
{
private ObservableCollection users { get; set; }
public ListingPage()
{
PreLoadData();
}
private async void PreLoadData()
{
try
{
App.ContextData.Sproc = "GetMeetingInfosOSP";
string url = App.MainConnection + App.ContextData.Sproc + "/" + App.ContextData.UserID;
Task<bool> task = LoadData(url);
bool data = await task;
if (data)
{
await PrepareData();
var listView = new ListView();
listView.RowHeight = 40;
this.Title = "Kunden Info Liste";
listView.ItemTemplate = new DataTemplate(typeof(DataCell));
listView.ItemsSource = users;
listView.ItemSelected += (sender, e) => {
UserData ud = (UserData)e.SelectedItem;
var page = new UserDataDetailsPage(ud);
//this line creates the error!!!! //this line creates the error!!!! //this line creates the error!!!! //this line creates the error!!!!
page.BindingContext = ud;
Navigation.PushAsync(page);
};
Content = listView;
}
else
{
await this.DisplayAlert("Fehler", "Die Daten konnten nicht geladen werden!", "OK");
}
}
catch(Exception ex)
{
await this.DisplayAlert("Error", ex.Message, "OK");
}
}
public async Task OnItemTapped(UserData user, object sender)
{
if (user == null) return;
var page = new UserDataDetailsPage(user) { BindingContext = user };
((ListView)sender).SelectedItem = null; // de-select the row
await Navigation.PushAsync(page);
}
private async Task<bool> LoadData(string url)
{
try
{
System.Json.JsonValue json = await JSONHandle.GetJSONDataAsync(url);
App.ContextData.Data = json;
return (json != null) ? true : false;
}
catch
{
return false;
}
}
private async Task<bool> PrepareData()
{
try
{
users = new ObservableCollection<UserData>();
JsonValue json = App.ContextData.Data;
JsonArray jsonArray = (JsonArray)json[App.ContextData.Sproc + "Result"];
for (int i = 0; i < jsonArray.Count; i++)
{
JsonObject jObj = (JsonObject)jsonArray[i];
Newtonsoft.Json.Linq.JObject jo = Newtonsoft.Json.Linq.JObject.Parse(jObj.ToString());
users.Add(DynamicDataUtility.CreateDataUser(jo));
}
return true;
}
catch (Exception ex)
{
await this.DisplayAlert("Error", "Error, Daten konnten nicht geladen werden!", "OK");
return false;
}
}
}