我如何添加工具栏的android在Xamarin,形式为ToolbarItem不适用于.droid?

问题描述:

我想在xamarin.forms中为我的移动应用程序提供一个工具栏。我通过网络冲浪,并了解有关toolbaritems,但我认为它只适用于Windows手机不适用于Android。任何有关这个问题的帮助将会受到衷心的感谢。 Ps:我还需要标签页中的工具栏。我如何添加工具栏的android在Xamarin,形式为ToolbarItem不适用于.droid?

这里是我的一段代码:

//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 "); 
      }; 
     } 
    } 
} 
+0

你到底想干什么?你有什么代码可以在Windows Phone上运行,但不是在Android上运行?您是在XAML还是从代码隐藏工作?请提供一些代码和更详细的信息,然后才能有效地帮助您:) –

+0

我使用代码编辑了帖子。请看一看。谢谢! –

  • 添加到您的Page构造

    ToolbarItems.Add(new ToolbarItem("Click", null,() => { 
        Console.WriteLine("Clicked"); 
    })); 
    
  • 确保您使用的是最新版本的Xamarin.Forms。它工作得很好。

  • 包装你MainPageNavigationPage

    MainPage = new NavigationPage(new YourHomePage()); 
    
+0

我以前也用过它,今天也尝试过,但工具栏仍未显示出来。任何其他方式来做到这一点。 –

+0

你确定你有最新版本的Xamarin.Forms?你的主页是一个导航页面吗?我编辑了我的答案 –

+0

我实际上有一个标签页作为主页。上面提到的内容页面是从一个标签页面调用的。有一个标签页作为主页与工具栏有关。关于xamarin.forms的版本,是的,我有最新版本的xamarin.forms。我在应用程序中整理了页面的整个切换。 –