我如何在xamarin,forms中为android添加工具栏,因为ToolbarItem对于.droid不起作用? [英] How do i add toolbar for android in xamarin,forms as ToolbarItem is not working for .droid?
本文介绍了我如何在xamarin,forms中为android添加工具栏,因为ToolbarItem对于.droid不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在xamarin.forms中有一个用于我的移动应用程序的工具栏.我在网上冲浪,对工具栏项目有所了解,但我认为它仅适用于Windows Phone,不适用于android.对此问题的任何帮助将不胜感激. 附:我在选项卡式页面中也需要工具栏.
I want to have a toolbar for my mobile application in xamarin.forms. I surfed throught the net and got to know about toolbaritems but i think it just works for the windows phone not for android. Any help regarding this issue would is heartly appreciated. Ps: I need a toolbar in the tabbed pages also.
这是我的代码:
//main page
namespace eSewaXamarin
{
public class App:Application
{
public App ()
{
MainPage = new TabbedPageClass ();
}
protected override void OnStart ()
{
// Handle when your app starts
}
protected override void OnSleep ()
{
// Handle when your app sleeps
}
protected override void OnResume ()
{
// Handle when your app resumes
}
}
}
//tabbed page
namespace eSewaXamarin
{
public class TabbedPageClass: TabbedPage
{
public TabbedPageClass ()
{
//this.SwipeEnabled = true;
ToolbarItems.Add(new ToolbarItem("Click", null, () => {
Console.WriteLine("Clicked");
}));
//this.Title = "eSewa";
//TabbedPageClass tabbedPage=new TabbedPageClass();
this.Children.Add (new MainMenu ());
this.Children.Add (new QuickMenu ());
this.Children.Add (new AboutPage ());
}
}
}
//about page
namespace eSewaXamarin
{
public class AboutPage:ContentPage
{
public AboutPage(){
Title="About";
this.BackgroundColor = Colors.lightGrey;
Padding = new Thickness (0,Device.OnPlatform(20, 0, 0),0,0);
var listView = new ListView ();
listView.ItemTemplate = new DataTemplate (typeof(AboutCell));
listView.ItemsSource = AboutClass.getAboutText ();
listView.RowHeight = 80;
var fonePayIcon = new Image{
Aspect = Aspect.AspectFit,
};
fonePayIcon.Source = ImageSource.FromFile ("footer_logo.png");
this.Content = new ScrollView {
Content = new StackLayout{
Children={listView,new StackLayout{
Padding=new Thickness(0,8,0,8),
BackgroundColor=Color.FromHex ("dfdfdf"),
VerticalOptions=LayoutOptions.EndAndExpand,
Children={fonePayIcon}
}
}
}
};
listView.ItemSelected += (sender, e) => {
if (e.SelectedItem != null) {
AboutClass aboutClass=(AboutClass)e.SelectedItem;
int id=aboutClass.id;
AppLog.showlog ("Selected item:::::" + aboutClass.id);
if(id==0){
Navigation.PushModalAsync (new NavigationPage(new Registration()));
}else if(id==1){
Navigation.PushModalAsync(new NavigationPage(new AbouteSewa()));
}else if(id==2){
Navigation.PushModalAsync(new AboutFonePay());
}else if(id==3){
Navigation.PushModalAsync(new AboutSecurityTips());
}else if(id==4){
Navigation.PushModalAsync(new AboutAppInfo());
}
}
((ListView)sender).SelectedItem = null;
};
}
}
}
//registration page
namespace eSewaXamarin
{
public class Registration: ContentPage
{
public Registration ()
{
this.Title="Registration";
ToolbarItems.Add(new ToolbarItem("Click", null, () => {
Console.WriteLine("Clicked");
}));
this.BackgroundColor = Colors.lightGrey;
var label = new Label {
Text = Strings.eSewaRegistratinAbout,
Style=Styles.LabelStyle
};
var registerButton=new Button{
Text="Register",
Style= Styles.buttonStyleGrey
};
var esewa_icon = new Image {
Aspect = Aspect.AspectFit,
};
esewa_icon.Source = ImageSource.FromFile ("header_logo.png");
Content = new StackLayout {
Style = Styles.toolbarStyle,
Children = {new StackLayout{
Style=Styles.stackLayoutStyleForAboutSection,
Children={label,registerButton}}
}
};
registerButton.Clicked+=delegate{
var SmsTask = MessagingPlugin.SmsMessenger;
if (SmsTask.CanSendSms)
SmsTask.SendSms("9801063628", "hello aqhtar ");
};
}
}
}
推荐答案
-
将此添加到您的
Page
构造函数:ToolbarItems.Add(new ToolbarItem("Click", null, () => { Console.WriteLine("Clicked"); }));
-
确保您使用的是最新版本的Xamarin.Forms.效果很好.
Make sure you're using the newest version of Xamarin.Forms. It works just fine.
将
MainPage
包裹在NavigationPage
内:MainPage = new NavigationPage(new YourHomePage());
这篇关于我如何在xamarin,forms中为android添加工具栏,因为ToolbarItem对于.droid不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文