Comment puis-je créer un menu de tiroir / curseur avec Xamarin.Les formes?
5 réponses
vous créez une nouvelle classe qui contient toutes les définitions pour le maître - i.e. le menu - et le détail - i.e. le principal page. Je sais, ça sonne de face à face, mais par exemple..
using System;
using Xamarin.Forms;
namespace testXamForms
{
public class HomePage : MasterDetailPage
{
public HomePage()
{
// Set up the Master, i.e. the Menu
Label header = new Label
{
Text = "MENU",
Font = Font.BoldSystemFontOfSize(20),
HorizontalOptions = LayoutOptions.Center
};
// create an array of the Page names
string[] myPageNames = {
“Main”,
“Page 2”,
“Page 3”,
};
// Create ListView for the Master page.
ListView listView = new ListView
{
ItemsSource = myPageNames,
};
// The Master page is actually the Menu page for us
this.Master = new ContentPage
{
Title = "The Title is required.",
Content = new StackLayout
{
Children =
{
header,
listView
},
}
};
// Define a selected handler for the ListView contained in the Master (ie Menu) Page.
listView.ItemSelected += (sender, args) =>
{
// Set the BindingContext of the detail page.
this.Detail.BindingContext = args.SelectedItem;
Console.WriteLine("The args.SelectedItem is
{0}",args.SelectedItem);
// This is where you would put your “go to one of the selected pages”
// Show the detail page.
this.IsPresented = false;
};
// Set up the Detail, i.e the Home or Main page.
Label myHomeHeader = new Label
{
Text = "Home Page",
HorizontalOptions = LayoutOptions.Center
};
string[] homePageItems = { “Alpha”, “Beta”, “Gamma” };
ListView myHomeView = new ListView {
ItemsSource = homePageItems,
};
var myHomePage = new ContentPage();
myHomePage.Content = new StackLayout
{
Children =
{
myHomeHeader,
myHomeView
} ,
};
this.Detail = myHomePage;
}
}
}
Il est construit en: MasterDetailPage
. Vous définiriez les propriétés Detail
et Master
de celui-ci à n'importe quel genre de Pages que vous voudriez. J'ai trouvé Hansleman.Formulaires pour être très instructif.
mon exemple minimum (affiché ici ) est le suivant:
public class App
{
static MasterDetailPage MDPage;
public static Page GetMainPage()
{
return MDPage = new MasterDetailPage {
Master = new ContentPage {
Title = "Master",
BackgroundColor = Color.Silver,
Icon = Device.OS == TargetPlatform.iOS ? "menu.png" : null,
Content = new StackLayout {
Padding = new Thickness(5, 50),
Children = { Link("A"), Link("B"), Link("C") }
},
},
Detail = new NavigationPage(new ContentPage {
Title = "A",
Content = new Label { Text = "A" }
}),
};
}
static Button Link(string name)
{
var button = new Button {
Text = name,
BackgroundColor = Color.FromRgb(0.9, 0.9, 0.9)
};
button.Clicked += delegate {
MDPage.Detail = new NavigationPage(new ContentPage {
Title = name,
Content = new Label { Text = name }
});
MDPage.IsPresented = false;
};
return button;
}
}
un exemple de solution est hébergé sur GitHub .
sur iOS le résultat ressemble à ceci( gauche: menu ouvert, droite: après avoir cliqué sur "B"):
notez que vous devez ajouter l'icône du menu comme ressource dans votre projet iOS.
si vous recherchez un exemple simple de MasterDetailPage s'il vous plaît jeter un oeil à mon échantillon repo à GitHub . Très bel exemple est également présenté ici
Slideoverkit est un grand plugin disponible pour les formulaires Xamarin. Il ya un github pour voir des échantillons gratuits et vous pourriez trouver de la documentation à ce sujet ici .