簡體   English   中英

我如何在 xamarin 中為 android 添加工具欄,因為 ToolbarItem 不適用於 .droid 表單?

[英]How do i add toolbar for android in xamarin,forms as ToolbarItem is not working for .droid?

我想在 xamarin.forms 中為我的移動應用程序提供一個工具欄。 我在網上沖浪並了解了工具欄項目,但我認為它僅適用於 Windows 手機,不適用於 android。 任何有關此問題的幫助將不勝感激。 Ps:我還需要標簽頁中的工具欄。

這是我的一段代碼:

//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。 它工作得很好。

  • 將您的MainPage包裹在NavigationPage

     MainPage = new NavigationPage(new YourHomePage());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM