.."ItemSelected += OnItemSelected" does not work for me

Hello Xamarin Developers,

I started new with mobile development in Xamarin. I have a problem and I really don't know how to solve, I hope you can help me.

So, I've created a MasterDetailPage

`public class MasterDetail : MasterDetailPage
{
Menu menu;
public MasterDetail()
{
menu = new Menu();

        Master = new Menu();
        Detail = new NavigationPage(new DetailHome());

        menu.ListView.ItemSelected += OnItemSelected;
    }
    void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem as MasterPageItem;
        if (item != null)
        {
            Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
            menu.ListView.SelectedItem = null;
            IsPresented = false;
        }
    }
}`

I wanted a simple menu where you can click/tap on one item and the detail page changes then to this page. In my menu I filled the items:

`public partial class Menu : ContentPage
{
public ListView ListView{ get { return listView; } }

    ListView listView;

    public Menu()
    {
        InitializeComponent();

        var masterPageItems = new List<MasterPageItem>();

        // Add masterPageItems
        masterPageItems.Add(new MasterPageItem
        {
            Title = "Home",
            IconSource = "icon.png",
            TargetType = typeof(DetailHome)
        });
        masterPageItems.Add(new MasterPageItem
        {
            Title = "Submit",
            IconSource = "icon.png",
            TargetType = typeof(DetailSubmitChallenge)
        });
        masterPageItems.Add(new MasterPageItem
        {
            Title = "Tutorial",
            IconSource = "icon.png",
            TargetType = typeof(DetailTutorial)
        });

        // Create ListView.
        listView = new ListView
        {
            ItemsSource = masterPageItems,
            ItemTemplate = new DataTemplate(() => {
                var imageCell = new ImageCell();
                imageCell.SetBinding(TextCell.TextProperty, "Title");
                imageCell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");
                return imageCell;
            }),
            VerticalOptions = LayoutOptions.FillAndExpand,
            SeparatorVisibility = SeparatorVisibility.None
        };

        // Create menu.
        Title = "Test";
        Content = new StackLayout
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            Children =
            {
                listView
            }
        };
    }
}`

The app, when executed starts and shows both the master page with the menu items and the first detail page but if i click/tap on a menu item, the color changes so that I know that it was clicked but there is no reaction to this: The detail page wont change to the new selected page.

Thank you very much for your help!

Best regards

Best Answer

Answers

Sign In or Register to comment.