【问题标题】:How do i add toolbar for android in xamarin,forms as ToolbarItem is not working for .droid?如何在 xamarin 中为 android 添加工具栏,因为 ToolbarItem 不适用于 .droid?
【发布时间】:2015-09-07 11:30:45
【问题描述】:

我想在 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 ");
            };
        }
    }
}

【问题讨论】:

  • 你想做什么?你有什么代码可以在 Windows Phone 上运行,但不能在 Android 上运行?您是在 XAML 中工作还是在代码隐藏中工作?请提供一些代码和更多详细信息,然后我们才能有效地帮助您:)
  • 我用代码编辑了帖子。请看一看。谢谢!

标签: xamarin.forms uitoolbaritem


【解决方案1】:
  • 将此添加到您的Page 构造函数

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

  • 将您的 MainPage 包裹在 NavigationPage 中:

    MainPage = new NavigationPage(new YourHomePage());
    

【讨论】:

  • 我以前也用过,今天也试过了,但工具栏仍然没有显示出来。任何其他方法。
  • 您确定您拥有最新版本的 Xamarin.Forms 吗?您的主页是 NavigationPage 吗?我编辑了我的答案
  • 我实际上有一个标签页作为主页。上面提到的内容页面是从一个标签页调用的。是否有一个标签页作为主页与工具栏有任何关系。关于 xamarin.forms 的版本,是的,我有最新版本的 xamarin.forms。我已经在我的应用程序中使用整个页面切换编辑了我的帖子。
  • 我用我的项目运行了一个干净的构建,它现在工作正常。我非常感谢你:)
猜你喜欢
  • 1970-01-01
  • 2019-12-27
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
相关资源
最近更新 更多