2016-12-14 105 views
1

我想实现一个事件的命令行为(这表现在Xamarin:https://github.com/xamarin/xamarin-forms-samples/tree/master/Behaviors/EventToCommandBehavior)一旦标签页行为导致Xamarin.Forms应用程序崩溃

,我尝试将TabbedPage.Behaviors部分添加到我的TabbedPage .XAML应用程序会崩溃上推出iOS中,以下情况例外:

Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Application windows are expected to have a root view controller at the end of application launch 
Native stack trace: 
    0 CoreFoundation      0x000000010b48434b __exceptionPreprocess + 171 
    1 libobjc.A.dylib      0x0000000115be221e objc_exception_throw + 48 
    2 CoreFoundation      0x000000010b488442 +[NSException raise:format:arguments:] + 98 
    3 Foundation       0x000000010c065e4d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 
    4 UIKit        0x000000010ecb0619 -[UIApplication _runWithMainScene:transitionContext:completion:] + 3827 
    5 UIKit        0x000000010ecacf69 -[UIApplication workspaceDidEndTransaction:] + 188 
    6 FrontBoardServices     0x000000011890b723 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24 
    7 FrontBoardServices     0x000000011890b59c -[FBSSerialQueue _performNext] + 189 
    8 FrontBoardServices     0x000000011890b925 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 
    9 CoreFoundation      0x000000010b429311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    10 CoreFoundation      0x000000010b40e59c __CFRunLoopDoSources0 + 556 
    11 CoreFoundation      0x000000010b40da86 __CFRunLoopRun + 918 
    12 CoreFoundation      0x000000010b40d494 CFRunLoopRunSpecific + 420 
    13 UIKit        0x000000010ecab7e6 -[UIApplication _run] + 434 
    14 UIKit        0x000000010ecb1964 UIApplicationMain + 159 
    15 ???         0x000000012e4eb58c 0x0 + 5071877516 
    16 ???         0x000000012e4eb1fd 0x0 + 5071876605 

    at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) 
    at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApplication.cs:79 
    at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
    at TabDemo.iOS.Application.Main (System.String[] args) [0x00008] in /Users/Shared/git/Experiments/TabDemo/TabDemo.iOS/Main.cs:17 

在Android应用程序只是挂在发射和永不显示标签页。

MyTabbedPage.Xaml看起来是这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<TabbedPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" 
    prism:ViewModelLocator.AutowireViewModel="True" 
    xmlns:local="clr-namespace:TabDemo.Views" 
    xmlns:behaviors="clr-namespace:TabDemo.Views.Behaviors" 
    x:Class="TabDemo.Views.MyTabbedPage"> 
    <TabbedPage.Behaviors> 
     <behaviors:EventToCommandBehavior 
      EventName="CurrentPageChanged" 
      Command="{Binding OnCurrentPageChangedCommand}" /> 
    </TabbedPage.Behaviors> 
    <NavigationPage 
     Title="Main"> 
     <x:Arguments> 
      <local:MainPage /> 
     </x:Arguments> 
    </NavigationPage> 
    <NavigationPage 
     Title="Second"> 
     <x:Arguments> 
      <local:SecondPage /> 
     </x:Arguments> 
    </NavigationPage> 
</TabbedPage> 

连线了在代码的行为背后导致完全相同的问题。如果我删除TabbedPage.Behaviors部分中的行为条目,则该应用程序在两个平台上均可正常运行。

任何人都可以阐明我可能做错了什么?

+0

我已经发布了一个示例应用程序演示问题[向上在github上(HTTPS ://github.com/jbachelor/TabbedAppSample/tree/master)。我会添加分支来尝试不同的答案。感谢您的帮助,每个人! – jbachelor

回答

1

我相信你可能需要改变EventToCommandBehavior继承的类型。打开EventToCommandBehavior.cs文件并更改:

public class EventToCommandBehavior : BehaviorBase<View> 

到:

public class EventToCommandBehavior : BehaviorBase<VisualElement> 

您还需要修改的两种方法的签名:

protected override void OnAttachedTo (View bindable) 

protected override void OnDetachingFrom (View bindable) 

到:

protected override void OnAttachedTo(VisualElement bindable) 

protected override void OnDetachingFrom(VisualElement bindable) 
+1

非常感谢您的帮助,@jgoldberger!看起来你让我通过了最大的最初障碍。随着你的建议,应用程序再次运行,而不是在启动时崩溃,但是,由于某种原因,我的DelegateCommand没有被调用。我会继续调查。再次感谢你!如果你很好奇,我已经把代码放在GitHub上了,在一个名为'jganswer'的分支中我实现了你的建议:[link](https://github.com/jbachelor/TabbedAppSample/tree/jganswer) – jbachelor

+1

好的...我发现命令没有执行的原因。我在ViewModel 中混淆了命名空间! – jbachelor

+1

已更新,现在工作代码在[jganswer分支]上的github上(https://github.com/jbachelor/TabbedAppSample/tree/jganswer)...我只需要弄清楚如何获取新选择的标签名称的事件,我会关闭和运行!再次感谢你! – jbachelor

1

如果你正在关注str对于EventToCommandBehavior来说是一个例子,我的猜测是有一个异常被抛出,因为你的命令需要的类型不同于传入的类型。没有看到你的项目,或者知道你想要完成什么,很难给你一个更清晰的方向。

,你可以尝试采取看看这个要点,提供了如何将行为添加到TabbedPage上CurrentPageChanged一个例子:https://gist.github.com/dansiegel/cdc81671f3610d8992d70c65c202f0a4

+1

非常感谢,@ dan-s! Gist看起来就像我正在寻找的东西。这是Xamarin.Forms Prism框架的未来部分吗?我不确定如何实现它,但我会玩弄它,看看我是否能得到像这样的工作。如果您碰巧知道有人在应用程序中实际使用MultiPageNavigationBehavior的代码示例,请告诉我。谢谢! – jbachelor

+1

@jbachelor您可以随时查看Prism的[里程碑](https://github.com/PrismLibrary/Prism/milestones)。 XF 6.3版本的一个里程碑是包含这种类型的行为,这些行为将由NavigationService自动附加。我应该补充说,如果能够帮助你,你现在可以在6.3 beta的TabbedPages中使用IActiveAware。 –

+1

谢谢,@ dan-s!我会去检查6.3版本和IActiveAware。 – jbachelor