问题描述
试图为Xamarin.iOS
应用实现侧边菜单,但在控制台中得到警告:
Tying to implement side menu for Xamarin.iOS
app, but got warning in console:
步骤
1)创建菜单的基类(来自样本)
1) Create base class for menu (from 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(第一个可见)
2) Implement VisibleView (First one that will be visible)
[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)
3) Implement 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)在设置中为SideMenu添加演示者
4) Add presenter for SideMenu in Setup
protected override IMvxIosViewPresenter CreatePresenter()
{
return new MvxSidebarPresenter((MvxApplicationDelegate)ApplicationDelegate, Window);
}
预期行为
应该从1点看到带有汉堡按钮的控制器
Should see controller from point 1 with burger button
实际行为
point1上的控制器可见,但没有汉堡按钮,来自point2的控制器未初始化,在控制台中警告有关缺少类装饰的警告,但如您所见,警告也存在(不赞成使用的警告,需要更新-检查源代码-搜索正确的类型,但警告有旧消息)
Controller from point1 become visible but without burger button,Controller from point2 not initialized,Warning in console about missed decoration for class, but as u see they are present (also warnings deprecated and need to be updated - check source code - searching done for correct type, but warning has old message )
配置
MvvmCross v 5.0.6
MvvmCross v 5.0.6
- MvvmCross
- MvvmCross.Core
- MvvmCross.Binding
- MvvmCross.iOS.Support
- MvvmCross.iOS.Support.XamarinSidebar
- MvvmCross.Platform
- SidebarNavigation
在查找错误时还看到了这篇文章-重新检查并看起来一切都很好,但无法正常工作.
Also saw this post while looking for error - recheck and looks like everything is fine, but not work.
警告日志:
mvx:诊断:0.21显示ViewModel ContentViewModel iOSNavigation:诊断:0.21要求浏览
mvx:Diagnostic: 0.21 Showing ViewModel ContentViewModel iOSNavigation:Diagnostic: 0.21 Navigate requested
mvx:警告:0.23找不到侧面菜单.若要使用侧面菜单,请使用'MvxPanelPresentationAttribute'类装饰viewcontroller类,并将面板设置为'Left'或'Right'.
mvx:Warning: 0.23 No sidemenu found. To use a sidemenu decorate the viewcontroller class with the 'MvxPanelPresentationAttribute' class and set the panel to 'Left' or 'Right'.
我还希望在调用this.ViewModel.Show<MenuViewModel>();
时看到ViewDidload
的ViewDidload
中的breakPoint停止,但是在创建此控制器的模型的同一时刻,它从未被触发.
I also expect to see breakPoint stop in ViewDidload
for MenuViewController
when call this.ViewModel.Show<MenuViewModel>();
but it's never triggered, in same moment model for this controller created.
有人可以建议做错了什么吗?
Can someone advice what was done incorrect?
编辑
我能够设置带有侧边栏的新的空项目并且它按预期工作.但是相同的代码在我当前的项目中不起作用-我不知道为什么装饰属性没有按预期读取....
I was able to setup new empty project with sidebar and it's work as expected. But same code not work in my current projects - I don't know why decorated attributes are not read as expected....
推荐答案
花2天的时间找出原因-与检测和解析装饰器属性有关的问题.
Spend 2 days to figure out the reason - the issue related to detection and parsing decorator attributes.
由于种种原因,我之间很少共享代码库的项目[MvxSidebarPresentation(MvxPanelEnum.Left, MvxPanelHintType.PushPanel, false)]
如果放置在共享项目中而不放在项目本身中,则不考虑在内.在同一时刻
I have few projects that share codebase between each other, and due to some reason[MvxSidebarPresentation(MvxPanelEnum.Left, MvxPanelHintType.PushPanel, false)]
is not taken in to account if it placed in shared project but not in project itself. In same moment
[MvxSidebarPresentation(MvxPanelEnum.Center, MvxPanelHintType.ResetRoot, true)]
[MvxSidebarPresentation(MvxPanelEnum.Center, MvxPanelHintType.ResetRoot, true, MvxSplitViewBehaviour.Master)]
[MvxSidebarPresentation(MvxPanelEnum.Center, MvxPanelHintType.PushPanel, true, MvxSplitViewBehaviour.Detail)]
按预期工作.
解决方法->子类在每个应使用项目的项目中共享菜单类(带有MvxPanelEnum.Left
).
Workaround -> subclass shared menuClass (with MvxPanelEnum.Left
) in each project where it should be used.
不确定此问题是否与mvvmCross lib或Xamarin有关.
Not sure if this issue related to mvvmCross lib or to Xamarin.
这篇关于mvvmCross的SideMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!