SideMenu for mvvmCross

时间:2017-07-13 13:31:31

标签: c# iphone xamarin.ios mvvmcross

Xamarin.iOS应用实施侧边菜单,但在控制台中收到警告:

  

mvx:警告:0.25没有发现侧面菜单。使用sidemenu装饰   viewcontroller类与' MvxPanelPresentationAttribute'类   并将面板设置为' Left'或者'对'。

步骤

1)为菜单创建基类(来自sample

public class BaseMenuViewController<T> : MvxViewController<T>, IMvxSidebarMenu where T : class, IMvxViewModel
{
    public virtual UIImage MenuButtonImage => UIImage.FromBundle("burger");

    public virtual bool AnimateMenu => true;
    public virtual float DarkOverlayAlpha => 0;
    public virtual bool HasDarkOverlay => false;
    public virtual bool HasShadowing => true;
    public virtual bool DisablePanGesture => false;
    public virtual bool ReopenOnRotate => true;

    private int MaxMenuWidth = 300;
    private int MinSpaceRightOfTheMenu = 55;

    public int MenuWidth => UserInterfaceIdiomIsPhone ?
        int.Parse(UIScreen.MainScreen.Bounds.Width.ToString()) - MinSpaceRightOfTheMenu : MaxMenuWidth;

    private bool UserInterfaceIdiomIsPhone
    {
        get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
    }

    public virtual void MenuWillOpen()
    {
    }

    public virtual void MenuDidOpen()
    {
    }

    public virtual void MenuWillClose()
    {
    }

    public virtual void MenuDidClose()
    {
    } 
}

2)实现VisibleView(第一个可见的)

[MvxSidebarPresentation(MvxPanelEnum.Center, MvxPanelHintType.ResetRoot, true)]
public partial class ContentViewController : MvxViewController<ContentViewModel>
{
    public ContentViewController()
        : base("ContentViewController", null)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        View.BackgroundColor = UIColor.Purple;
        this.ViewModel.Show<MenuViewModel>();
    }
}

3)实现MenuElementViewController(SideMenu)

[Register("MenuViewController")]
[MvxSidebarPresentation(MvxPanelEnum.Left, MvxPanelHintType.PushPanel, false)]
public class MenuViewController : BaseMenuViewController<MenuViewModel>
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        this.View.BackgroundColor = UIColor.Red;
    }
}

4)在Setup

中添加SideMenu的演示者
protected override IMvxIosViewPresenter CreatePresenter()
{
    return new MvxSidebarPresenter((MvxApplicationDelegate)ApplicationDelegate, Window);
}

预期行为

应该从点1看汉堡按钮

的控制器

实际行为

来自point1的控制器变得可见,但没有汉堡按钮, 来自point2的控制器未初始化, 在控制台中警告错过了类的装饰,但是当你看到它们存在时(同样警告已被弃用且需要更新 - check source code - searching done for correct type, but warning has old message

配置

MvvmCross v 5.0.6

  • MvvmCross
  • MvvmCross.Core
  • MvvmCross.Binding
  • MvvmCross.iOS.Support
  • MvvmCross.iOS.Support.XamarinSidebar
  • MvvmCross.Platform
  • SidebarNavigation

在寻找错误时也看到this post - 重新检查并看起来一切都很好,但不起作用。

警告日志:

  

mvx:诊断:0.21设置:次要结束

     

mvx:Diagnostic:0.21显示ViewModel ContentViewModel   iOS导航:诊断:0.21导航请求

     

mvx:警告:0.23没有发现侧面菜单。要使用sidemenu使用&#39; MvxPanelPresentationAttribute&#39;来修饰viewcontroller类。选择并将面板设置为“左”&#39;或者&#39;对&#39;。

我还希望在调用ViewDidloadMenuViewController this.ViewModel.Show<MenuViewModel>();看到breakPoint停止,但它永远不会触发,在此控制器创建的同一时刻模型中。

有人可以建议做错了吗?

修改

我能够设置new empty project with sidebar并按预期工作。但是相同的代码在我目前的项目中不起作用 - 我不知道为什么装饰属性没有按预期读取....

2 个答案:

答案 0 :(得分:1)

该跟踪由MvxSidebarViewController中的这段代码触发。

    protected virtual void SetupSideMenu()
    {
        var leftSideMenu = ResolveSideMenu(MvxPanelEnum.Left);
        var rightSideMenu = ResolveSideMenu(MvxPanelEnum.Right);

        if (leftSideMenu == null && rightSideMenu == null)
        {
            Mvx.Trace(MvxTraceLevel.Warning, $"No sidemenu found. To use a sidemenu decorate the viewcontroller class with the 'MvxPanelPresentationAttribute' class and set the panel to 'Left' or 'Right'.");
            AttachNavigationController();
            return;
        }

        // ...
    }

因此,Mvvmcross无法解析标有您上面使用的属性的SideMenu视图控制器。我建议删除所有的MvvmCross nugets,然后查看 bin obj 文件夹。

可能尝试在MenuViewController添加构造函数可能会有帮助。

如果一切都失败了,我会尝试在测试项目中确切地构建类,我注意到你的ContentViewController不像在项目中那样从基类继承。我知道这不应该是真正相关的,但是我从侧边栏类的实验中发现的是,它非常特别适用于ViewControllers和嵌套视图控制器的设置。

我完全取消了MvvmCross siderbar nuget并且直接使用了XamarinSidebar nuget,我使用MvxSidebarViewController作为创建我自己的实现的起点,我并不是特别热衷于属性设置而我喜欢自己处理导航,而不是使用ShowViewModel或新的导航系统。

希望其中一些有用。

答案 1 :(得分:0)

花2天时间找出原因 - 与检测和解析装饰属性有关的问题。

由于某些原因,我几乎没有相互之间共享代码库的项目 [MvxSidebarPresentation(MvxPanelEnum.Left, MvxPanelHintType.PushPanel, false)] 如果它放在共享项目中而不是项目本身,则不考虑它。在同一时刻

[MvxSidebarPresentation(MvxPanelEnum.Center, MvxPanelHintType.ResetRoot, true)]
[MvxSidebarPresentation(MvxPanelEnum.Center, MvxPanelHintType.ResetRoot, true, MvxSplitViewBehaviour.Master)]
[MvxSidebarPresentation(MvxPanelEnum.Center, MvxPanelHintType.PushPanel, true, MvxSplitViewBehaviour.Detail)]

按预期工作。

解决方法 - &gt;子类在应该使用它的每个项目中共享menuClass(带MvxPanelEnum.Left)。

不确定此问题是否与mvvmCross lib或Xamarin有关。