Xamarin Forms List View Show Different Pages? - xamarin

New to Xamarin here. How can I make each item on a listview go to a different page in Xamarin Forms?
For example, something like Facebook app:

Facebook is using a master detail page for this. Your should take a look at this Xamarin link about Master Detail Page. Let me know if this is not what you're looking for and I'll give you a solution for a listview. I personally would go with the master detail page.

The main flow is simple. You have your ListView, you attach an event on the ItemTapped to it, in code behind in the ItemTap handle method you can access which item was tapped and with that information you decide where you want to navigate.
void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
{
// Item tapped
var item = e.Item as YourItemThatWasBinded;
if (item == null) return;
// Choose the page to navigate to based on the item selected.
// You decide what logic use inside this method if can be as simple using IFs.
var page = GetPageToNavigateWithItem(item);
// Navigate
Navigation.PushAsync(page);
}

Related

How not to reload CollectionView after clicking on Back button

Hi I'm facing a problem with my small xamarin app.
I have my small xamarin-form app that everytime the user enter a product Id click OK button retrieves a single (data related to that product Id only) data thru Web API from page A, and then pass it to a collectionview in another page B.
then there is a back button to go back to page A and the user must enter another product Id again and the app must fetch data thru web api again and pass it again to page B in the collectionview.
So the problem is now, when I click on back button in Page B, it's clear the collectionview data, whereas I need the collectionview to keep the state even If I pop back.
I'm not using any MVVM pattern or complicated stuff from now as I'm new to xamarin; it's just a simple architecture.
Or maybe I'm using wrong technics to achieve this and can advise me a better way to achieve that. I'm please open to your suggestion !!!
In brief, I need to be able not to reload CollectionView after clicking on Back button.
Below is the code that Bind to the View on Page B
public async void OnGetProductDetailButton(object sender, EventArgs e)
{
await Navigation.PushAsync(new ProductDetailPage
{
BindingContext = productDetails
});
}

Xamarin Forms - Slide out menu to select items

I have a xamarin forms application search screen that allows a user to select an item from a list. Currently I am using a picklist and the user has to scroll and select and click done. This is using the native controls. Howerver I have noticed in iOS in settings menu etc where the have designed it so that when a user want to select from a list a arrow is shown on the right hand side which takes them to another page to select an item (s) from a list. To me this seems like a better UX. Here is an ex:
My question is there something built into this withing xamarin forms? I guess I could create a listview and then on click pass to another page. Any examples are appreciated.
There is no built-in for that, but it's not too hard to achieve this.
To set the disclosure indicator (the arrow), implement a custom renderer derived from ImageCellRenderer and override GetCell
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var viewCell = base.GetCell(item, reusableCell, tv);
viewCell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
return viewCell;
}
When the user taps that cell you should navigate modally to the view showing the list of languages.
Adding that Cancel-Button is not really hard either, but needs another step. You'll need to wrap the new language selection page in a NavigationPage and push that NavigationPage modally. Furthermore you'll have to add a toolbar item to the wrapped page (see here).
Within the page there is a SearchBar view and a ListView below it. To add the checkmarks, you'll have to implement a custom cell with a custom renderer (derived from ImageCellRenderer like the one shown above), setting the UITableViewCell.Accessory to Checkmark if the custom cell is selected
if(item is SelectableCell selectableCell)
{
var selected = selectableCell.IsSelected;
viewCell.Accessory = selected ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.Checkmark;
}
Please note: Selectable cell is no Xamarin.Forms stock cell, but one that you have to implement by yourself, including the bindable property IsSelected.
This should be basically the steps you need to achieve what you want. I've assumed basic Xamarin.Forms knowledge that is needed to fill the gaps.

How do I create a dynamic page based on a selection?

I am trying to dynamically fill a second panorama page based on what item was selected from the home application screen.
On the application's first start screen there is a listbox if items each with text. If a user taps on an item with text "foobar" a template page should load and the title of the template page should be set to "foobar" and this second panorama page should know that it's data should be related to "foobar".
Is there anyway to do this?
I currently have my MainPage navigate to a new page (DynamicPage.xaml). This navigation is triggered when a ListBox_SelectionChanged event occurs. I have the title text of the DynampicPage.xaml Binding to a TitleText variable that is located in MainPage.xaml.cs. However, when I do this the title of DynamicPage.xaml is ever only set to my initialization value for the titleText variable even though I am updating this variable right before I navigate to the page.
If anyone can provide some help I would be very grateful as I am just a beginner on the WP7 platform. Thanks!
The Binding you're using for the title is only going to update if the TitleText property is a dependency property or if your MainPage is implementing the INotifyPropertyChanged interface so your class can notify the UI when one of its properties changed.
http://windowsphonegeek.com/articles/All-about-Dependency-Properties-in-Silverlight-for-WP7
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.95).aspx
But I think this is not the best way for you to achieve this. For now a much better way is to store your data somewhere in a static class, in the main page's constructor load these data into the listbox, and when the user selected an item, navigate the user to the second page like this:
NavigationService.Navigate(new Uri("/DynamicPage.xaml?Item=" + selectedItem.Id, UriKind.Relative));
When you navigate like this a new instance of DynamicPage is created, and in the OnNavigatedTo method you can access the navigation parameters and populate your page with the selected data. For example:
<controls:Panorama x:Name="MyPanorama" Title="TitleHere">...</controls:Panorama>
Item selectedItem = StaticData.GetItem(NavigationContext.QueryString["Item"]);
MyPanorama.Title = selectedItem.Name.ToUpper();
Description.Text = selectedItem.Description;
This way you can use secondary tiles and toast notifications to directly point to a specific content in your application.
If you're getting to understand the navigation you should definitely use the pattern called Model-View-ViewModel which is about to solve these problems mostly with bindings, but trust me, probably this is the easier way for now.

Windows Phone 7 - passing selected item (listbox databound to ObservableCollection) to "update" page

I am writing a Windows Phone 7 Application that should be pretty basic.
I have a view model ("MainViewModel") that contains a class I created ("EntrySheet"), which contains an ObservableCollection (I'll refer to it as "Entries").
My MainPage.xaml contains a listbox that is databound to the App.ViewModel.EntrySheet.Entries. This works perfectly for showing the list, and adding entries to the ObservableCollection is reflected in the ListBox.
However, what I can't get my head around is the proper way to "pass" a selected Entry to an update page (this will let a user change fields on the Entry: name, amount, date, whatever which when navigated back to MainPage will be reflected in the ListBox).
I guess what I expected what for there to be a "SelectedItem" on the ObservableCollection, and I could just navigate to the update page, which would be able to use something like: App.ViewModel.EntrySheet.Entries.SelectedItem.
I really appreciate any help on this and would also welcome constructive criticism on how to better structure my app.
I know this is a late anwer, but I just happened to stumble across your question now.
Here's a possible solution for you:
XAML
<ListBox Name="listBox" Tap="listBox_Tap">
<!-- Some code -->
</ListBox>
C#
private void listBox_Tap(object sender, GestureEventArgs e)
{
DataObject dataObject = (sender as ListBox).SelectedItem as DataObject;
//Do something with dataObject
}
This will enable you to interact with the data object behind the selected item in the listbox.
Courtesy of Windows Phone Geek

Pickerboxdialog Customizing

So, I'm attempting to use this:
http://blogs.msdn.com/b/priozersk/archive/2010/09/17/customizing-picker-box-dialog.aspx
However, I just want a normal pickerboxdialog (just text) but I'd like to attach an id to it, so I can easily reference the selection the user picked. However, even after building my own class to pass in, I still cannot get the text to display properly (IE at all) within the pickerbox.
Does anyone have an experience? I basically copied his code and still no luck...
if you want a normal picker box then you shouldn't have to worry about customizing the template (unless you want to display the ID too).
The way you reference the object selected by the user is just in the Closed event handler:
void Dialog_Closed(object sender, EventArgs e)
{
var picker = (PickerBoxDialog)sender;
var selected = (YourCustomObject)picker.SelectedItem;
}
In other words you shouldn't need the ID of the selected object because you can get a reference select object directly.
The Silverlight toolkit includes a ListPicker control which provides the functionality you're after.
It displays like the so called "picker box" but also includes a SelectedItem property and a SelectionChanged event.

Resources