Load more items in listbox - windows-phone-7

I've read some topics/questions/google results before but still don't know how to solve my problem.
In my case I consume json and what I do is deserializing JSON into objects. Then using ListboxName.ItemsSource I bind them to listbox.
The problem is that my server gives me only 20 top results, but also I receive a link to next 20.
Theoretically, I would like to bind this link to LoadMore button which I would put at the end of the list. Then what? Merge new result to existing ObservableCollection? (I assume that I have to use ObservableCollection)

Create an
ObservableCollection<Items> Items {get;set;}
then in XAML bind to it and add items to this collection. It will notify a list box to update a view.
XAML
<ListBox x:Name="ListboxName" ItemsSource="{Binding Items}"/>
in .cs file:
//After you have parsed json
private void OnNewDataDownloaded(List<Items> parsedItems)
{
foreach(var item in parsedItems)
{
Items.Add(item);
}
}
Here you can find an elegant way to add LoadMore button

Related

how to update ObservableCollection<> of one page from another page in xamarin

My application is in MVVM architecture.
I Have photo.xaml page in which i have one 1 ListView whoose bindingcontext is ObservableCollection listphoto of photos which is defined in its viewmodel.cs file.
now i have to redirect to BarcodeScan.cs from button click of photo.xaml .
my que i how can i add item to listphoto from here(BarcodeScan.cs )??
I tried to define new list in BarcodeScan like this
public ObservableCollection<JobPhoto> ListSerialNumbers { get; set; }
and intialised in its constructor like this
ListSerialNumbers = new ObservableCollection<JobPhoto>();
but it dont update list on photo.xaml page.
how can i achieve this. I am new to MVVM.Please Help.
Thank you.
You should use messaging center for this
First get it method registered as :
MessagingCenter.Subscribe<YourObjectClassComesHere>(this, "Any Message or empty string will be okay", (Obj) =>
{
//Code you want to execute
});
After this you can invoke it from another page as
MessagingCenter.Send(YourObject(of type "YourObjectClassComesHere"), "Any Message or empty string will be okay");
Hope it helps.
More details are available at : https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/
You can try MessageCenter https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/
In Phonepage you subscript message and send a message from another page.
I Have photo.xaml page in which i have one 1 ListView whoose bindingcontext is ObservableCollection listphoto of photos which is defined in its viewmodel.cs file.
Firstly it would be worth showing your XAML code.
You say in the quote above that you set the bindingcontext of the listview to the collection. You should be setting the ItemSource property of the ListView to the collection.

Implementation of OnSearchBarButtonPressed in the ListView in Xamarin Form

Is there a way to search a name on the table(Listview) in Xamarin Form without using any MonoTouch components - for crossplatform?
I have a number of names and populated on listview and what I want is that once user types the name and listview would be sorted and it should bring to matched name. I have added searchbar at the top of the listview.
I do not know how to implement OnSearchBarButtonPressed method. I believe that I should not reinvent the wheel again.
SearchBar searchBar = new SearchBar
{
Placeholder = "Search Employee Name",
};
searchBar.SearchButtonPressed += OnSearchBarButtonPressed;
Padding = new Thickness (10, 20, 10, 10);
Content = new StackLayout () {
Children={searchBar,listView}
};
In relation to your other posting here your ListView ItemsSource is a collection of objects.
The ListView control has no way of knowing about your model and wouldn't be able to any filtering by itself.
There appears to be nothing inbuilt into the ListView that would allow you to specify some Predicate to help with this task anyway.
You should therefore do the filtering in the SearchBar.SearchButtonPressed and then re-assign the ListView ItemsSource to reflect the newly filtered ItemsSource you want to show to the end-user. Your filtering logic could then be anything you wanted as your be able to customize this to even filter over many fields etc.
The SearchBar.SearchButtonPressed is just an event which you assign an EventHandler, like you are already doing. You just need to fill in the filtering logic and update the ListView accordingly.

Windows Phone 7: How to notify data is updated in ListBox?

I have above scenario: If user click on ListBox, it will either have sub items (again ListBox) or detail view.
So what i did currently is: Whenever user clicks any item, made a web call and filled up the same ListBox if clicked item is having further sub items.
Now, issue comes in picture:
Suppose i am in 4th screen (detail view),
Moved to the 3rd and 2nd screen with data maintained as stack (Its working fine, yes i am maintaining data in ObservableCollection<ObservableCollection<MyObjects>> so while moving back, i am fetching data from there)
Now, if i click any item in screen 2, it will open detail view for the screen 3 ListBox data.
Means that ListBox is not getting notified that we have filled data inside OnBackKeyPress()
FYI, i am filling up ListBox and WebBrowser in the same page., so my problem is that how do i notify ListBox once i filled up data from stack which i have maintained?
Yes i have also implemented INotifyPropertyChanged but don't know why its not working.
Please check my code:
ListBox and WebView screen: http://pastebin.com/K1G27Yji
RootPageItem class file with the implementation of INotifyPropertyChanged: http://pastebin.com/E0uqLtVG
sorry for pasting code in above way, i did as question is being long.
Problem:
How do i notify ListBox that data is changed from OnBackKeyPress?
And what is the behavior if you set:
listBox1.ItemsSource = null;
before
listBox1.ItemsSource = listRootPageItems;
This is just wrong architecture. Instead of reloading the same listbox, please add a single page for each screen. Share data between them inside the App class (internal static) and use the built in navigation stack for handling "going back". Don't override OnBackKeyPress for this purpose.
You will get your desired functionality for "free" with easier to maintain and use codebase.
Oops it was a silly mistake i made.
I forgot to set items[] array inside OnBackKeyPress() but was accessing while clicking item, hence its having items[] data of last step we moved in forward direction, it was executing the same data.
Now, i have just included a single line and it has solved my problem.
items = listRootPageItems.ToArray(); // resolution point
So final code of onBackKeyPress() is:
/**
* While moving back, taking data from stack and displayed inside the same ListBox
* */
protected override void OnBackKeyPress(CancelEventArgs e)
{
listBox1.Visibility = Visibility.Visible;
webBrowser1.Visibility = Visibility.Collapsed;
listBox1.SelectedIndex = -1;
if (dataStack.Count != 0)
{
listBox1.ItemsSource = null;
listRootPageItems = dataStack[dataStack.Count-1];
listBox1.ItemsSource = listRootPageItems;
items = listRootPageItems.ToArray(); // resolution point
dataStack.Remove(listRootPageItems);
e.Cancel = true;
}
}

WP7 : Populating a ListBox depending on the input from another ListBox

I'm trying to add ListItems to a ListBox (ListBox3) depending on the selectedItem of another ListBox (ListBox1). The problem is , The Items aren't added to the Listbox.
Here's the code :
private void createlist()
{
if (listBox1.SelectedValue.ToString().Equals("EPL"))
{
ListBoxItem manchesterunited = new ListBoxItem();
manchesterunited.Content = "Manchester United";
listBox3.Items.Add(manchesterunited);
}
}
private void listBox1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
createlist();
}
createlist() does the changes and is called in the SelctionChanged() event of ListBox1.
New to C# and WP7 programming , any help will be much appreciated.
Create the lists in your viewmodel and bind the listbox to a list<> in your viewmodel say SelectedList. When the user selects the item from ListBox1 just change the value of SelectedList with the appropriate List and Notify the property changed event. And it will be done.!
i think you program not run in mvvm structure.
make sure your logic is right. you can make a breakpoint at the line
ListBoxItem manchesterunited = new ListBoxItem();
Ensure run those code in if code block.
the way add a control in a listbox is correct.

How to delete items from a listbox on Windows 7 phone?

I use ItemsSource, when I want to delete an item from my list I get an error.
Error:
Operation not supported on read-only collection.
MenuItem menuItem = (MenuItem) sender;
MessageBox.Show(menuItem.Header.ToString(), "Result", MessageBoxButton.OK);
ListBoxItem lb (ListBoxItem)listBoxJournal.ItemContainerGenerator.ContainerFromItem(((MenuItem)sender).DataContext);
liste.Remove((Note)lb.Content);
listBoxJournal.UpdateLayout();
listBoxJournal.Items.Clear();
listBoxJournal.ItemsSource = liste;
I get the error on the line items.clear.
Thanks
If you set ItemsSource on the ListBox, then Items is internally generated and read-only.
So in this case you need to delete the item from the supoplied item collection.
If the collection implements INotifyCollectionChanged, then the collection changes are reflected in the listbox.
ObservableCollection is a list that implemented INotifyCollectionChanged
use that --- Listname.RemoveAt(ListBoxName.SelectedIndex);

Resources