你能用masterdetailpage图标/文字做一个clickevent吗?

时间:2016-04-03 20:07:25

标签: c# xamarin xamarin.forms

我有一个带有menupage的rootpage(作为我的masterdetailpage)和我的contentpage。当我点击我的menupage图标/文本时,我希望menupage(mdp)在我点击图标/文本时初始化它的组件。

有可能吗?

这是我目前的代码。

public RootPage ()
    {
        NavigationPage.SetHasNavigationBar (this, false);
        var theMenu = new MenuPage (this);

        theMenu.Title = "Click";
        theMenu.Icon = "Icon-Small-40.png";

        //can I make a click with the theMenu.Title or Icon above?

        Master = theMenu;

        NavigationPage page = new NavigationPage(new StartPage ());
        Detail = page;

    }

1 个答案:

答案 0 :(得分:0)

如果以下代码可以帮助您

,请告诉我
namespace LoginNavigation
{
public class RootPage:MasterDetailPage
{
MenuPage menuPage;
public RootPage ()
{

menuPage = new MenuPage ();

menuPage.Menu.ItemSelected += (sender, e) => NavigateTo (e.SelectedItem as MenuItemForMaster);

Master = menuPage;
Detail = new NavigationPage (new TimeSheet()){
};
}

void NavigateTo (MenuItemForMaster menu)
{
if (menu == null)
return;

Page displayPage = (Page)Activator.CreateInstance (menu.TargetType);
//Detail = displayPage;
Detail = new NavigationPage (displayPage) { BarBackgroundColor = Color.FromHex("008dce"),BackgroundColor  = Color.FromHex("008dce")};

menuPage.Menu.SelectedItem = null;
IsPresented = false;
}
}
}
在MenuPage中

,我有

Menu = new MenuListView ();
Menu.RowHeight = 44;
Menu.SeparatorColor = Color.FromHex ("e8e8e8");
Menu.SeparatorVisibility = SeparatorVisibility.Default;

menuListView和DataClass如下

namespace LoginNavigation
{
public class MenuListView : ListView
{
public MenuListView ()
{
List<MenuItemForMaster> data = new MenuListData ();

ItemsSource = data;
VerticalOptions = LayoutOptions.FillAndExpand;
BackgroundColor = Color.Accent;

var cell = new DataTemplate (typeof(MenuCell));
//cell.SetBinding (MenuCell.TextProperty, "Title");
//cell.SetBinding (MenuCell.ImageSourceProperty, "IconSource");
this.HasUnevenRows = false;
ItemTemplate = cell;
}

namespace LoginNavigation
{
public class MenuListData : List<MenuItemForMaster>
{
public MenuListData ()
{
this.Add (new MenuItemForMaster () { 
Name = “"
ImageSource = "paper_plane.png", 
TargetType = typeof(TimeSheet)
});
this.Add (new MenuItemForMaster () { 
Name = "Extn : 3969", 
ImageSource = "phone_reciever.png", 
TargetType = typeof(TimeSheet)
});
this.Add (new MenuItemForMaster () { 
Name = "TimeSheet", 
ImageSource = "Calender.png", 
TargetType = typeof(TimeSheet)
});

this.Add (new MenuItemForMaster () { 
Name = "Omega", 
ImageSource = "Notes.png", 
TargetType = typeof(Omega)
});

}
}
}

最后这是MenuItemForMaster

 namespace LoginNavigation
    {

    public class MenuItemForMaster
    {
    public string Name { get; set; }

    public string ImageSource { get; set; }

    public Type TargetType { get; set; }
    }