在 Xamarin.Forms Shell 如何以编程方式添加菜单项 [英] In Xamarin.Forms Shell How do I add menuitems programatically

查看:39
本文介绍了在 Xamarin.Forms Shell 如何以编程方式添加菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Visual Studio 2017 的 Xamarin Forms v4.0.0.394984 pre10 中,我能够使用 MenuItemsCollection MenuItems 在 Shell 的构造函数中以编程方式将 MenuItems 添加到 Shell.

这是在 v4.0.0.394984 pre10 中工作的代码(为了这个问题简化了它,添加了一个硬编码的菜单项,这里没有显示函数 LoadView)

公共部分类外壳:Xamarin.Forms.Shell{公共 ICommand cmdLoadView { 获取;} = 新命令(加载视图);公共外壳(){初始化组件();BindingContext = this;MenuItem mi = new MenuItem();mi.Command = cmdLoadView;mi.CommandParameter = "myCommand";mi.Text = "sampleName";MenuItems.Add(mi);}...}

在所有后续版本中,此代码不起作用.Intellisense 表明 MenuItems 仍然是 Shell 的一部分,但我收到一个编译错误说:错误 CS0103:当前上下文中不存在名称MenuItems".

当我引用 this.MenuItems 时,出现编译错误:错误 CS1061:Shell"不包含MenuItems"的定义,并且找不到接受Shell"类型的第一个参数的可访问扩展方法MenuItems"(您是否缺少 using 指令或程序集引用?)

这在当前版本的 Xamarin.Forms 中是否可行?自 v4 pre10 以来,我已经尝试过每个版本和预发布版本,但都没有奏效.

感谢您提供任何帮助!

解决方案

您需要将 MenuItem 对象添加到 AppShell 中的 Current.Items 属性 实现.示例:

//创建 MenuItem 对象的工厂方法私有静态菜单项 CreateMenuItem(字符串标题,ICommand cmd){var menuItem = new MenuItem();menuItem.Text = 标题;menuItem.Command = cmd;返回菜单项;}公共 AppShell(){...//您可以将此代码放在 AppShell 类中的任何方法中Current.Items.Add(CreateMenuItem("AppInfo", new Command(async () =>{ShellNavigationState state = Shell.Current.CurrentState;等待 Shell.Current.Navigation.PushAsync(new AppInfoPage());Shell.Current.FlyoutIsPresented = false;})));...}

In Xamarin Forms v4.0.0.394984 pre10 using Visual Studio 2017 I was able to add MenuItems to the Shell programatically in the constructor of the Shell using the MenuItemsCollection MenuItems.

Here is the code that works in v4.0.0.394984 pre10 (simplified it for this question to add a single hard-coded menuitem and function LoadView is not shown here)

public partial class Shell : Xamarin.Forms.Shell
{
    public ICommand cmdLoadView { get; } = new Command(LoadView);

    public Shell()
    {
        InitializeComponent();
        BindingContext = this;

        MenuItem mi = new MenuItem();
        mi.Command = cmdLoadView;
        mi.CommandParameter = "myCommand";
        mi.Text = "sampleName";
        MenuItems.Add(mi);
    }

...
}

In all subsequent versions, this code does not work. Intellisense indicates that MenuItems is still part of the Shell, but I get a compilation error saying: error CS0103: The name 'MenuItems' does not exist in the current context.

When I reference as this.MenuItems I get the compilation error: error CS1061: 'Shell' does not contain a definition for 'MenuItems' and no accessible extension method 'MenuItems' accepting a first argument of type 'Shell' could be found (are you missing a using directive or an assembly reference?)

Is this possible in the current version of Xamarin.Forms? I've tried with each of the releases and pre-releases since v4 pre10 and none have worked.

Thanks is advance for any help!

解决方案

You need to add the MenuItem objects to the Current.Items property in your AppShell implementation. Example:

    // factory method to create MenuItem objects
    private static MenuItem CreateMenuItem(string title, ICommand cmd)
    {
        var menuItem = new MenuItem();
        menuItem.Text = title;
        menuItem.Command = cmd;
        return menuItem;
    }

    public AppShell() 
    {
        ...
        // you can place this code in any method in the AppShell class
        Current.Items.Add(CreateMenuItem("AppInfo", new Command(async () =>
                {
                    ShellNavigationState state = Shell.Current.CurrentState;
                    await Shell.Current.Navigation.PushAsync(new AppInfoPage());
                    Shell.Current.FlyoutIsPresented = false;
                })));
         ...
     }

这篇关于在 Xamarin.Forms Shell 如何以编程方式添加菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆