Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法将工具栏项添加到ContentPage_Android_Xamarin_Xamarin.android_Xamarin.forms - Fatal编程技术网

Android 无法将工具栏项添加到ContentPage

Android 无法将工具栏项添加到ContentPage,android,xamarin,xamarin.android,xamarin.forms,Android,Xamarin,Xamarin.android,Xamarin.forms,我宣布我的主页: public App () { MainPage = new NavigationPage(new MainPage()); } 然后在主页面上点击按钮打开ContantPage: class MainPage : ContentPage { public MainPage() { button.Clicked += to_my_contentpage; //... } private async void t

我宣布我的主页:

public App () {
    MainPage = new NavigationPage(new MainPage());
}
然后在主页面上点击按钮打开ContantPage:

class MainPage : ContentPage {
    public MainPage() {
        button.Clicked += to_my_contentpage;
        //...
    }

    private async void to_my_contentpage(object sender, EventArgs e) {
        await Navigation.PushModalAsync(new my_contentpage());
        //using PushAsync doesn't help
    }
}
并尝试将此页面上的按钮显示为工具栏项:

我想根据这个答案做所有事情,但我的工具栏项目不包括在我的页面中:


我做错了什么?

PushModalAsync在这种情况下不起作用,因为您需要一个导航栏才能向其中添加工具栏项

由于模式页面没有显式地显示/包含导航栏,因此您不能这样做

解决方案:

创建自定义导航栏并向其中添加所需的任何视图。 不要使用模式页面。 希望这有帮助

添加工具栏项时,需要使用NavigationPage。是否尝试执行等待导航。PushModalAsyncnew NavigationPagenew my_contentpage;?
public class my_contentpage : ContentPage {
    public my_contentpage () {
        ToolbarItem AddButton = new ToolbarItem("AddButton", "AddIcon.png", () => {
            Console.WriteLine("Clicked");
        });
        this.ToolbarItems.Add(new ToolbarItem());
        //...
        this.Content = new StackLayout { Children = { header, listView } };
    }
}