Libin Joseph
Libin Joseph

Reputation: 7342

Xamarin Forms applying Behaviors to StackLayout using Prism

I am trying to bind two labels with the date on the click of the StackLayout. The stacklayout has three elements, a label to display the date, another label to display the Month and Year, and the third element is a datepicker with the visiblity set to false.

My XAML

<StackLayout>
     <Label Text="End Date" Style="{StaticResource H2Style}"  HorizontalOptions="Center"/>
     <Label Text="26" HorizontalOptions="Center"/>
     <Label Text="Mar 2017" HorizontalOptions="Center"/>
     <DatePicker x:Name="endDatePicker" IsVisible="false" />
     <StackLayout.Behaviors>
        <b:EventToCommandBehavior EventName="DateTapped" 
            Command="{Binding DatePickerCommand}"
            CommandParameter= "{x:Reference endDatePicker}" />
     </StackLayout.Behaviors>
</StackLayout>

I would like to show the datepicker dialog on the click of the stackLayout.

My ViewModel

public class MyViewModel : BindableBase
{
    public DelegateCommand<Object> DatePickerCommand { get; set; }
    public MyViewModel()
    {
        DatePickerCommand = new DelegateCommand<Object>(DateTapped);

    }

    private void DateTapped(Object obj)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            if (endDatePicker.IsFocused)
                endDatePicker.Unfocus();
            endDatePicker.Focus();
        });
    }
}

But I get the following error:

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                      0x0000000104b7bb0b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x000000010f757141 objc_exception_throw + 48
2   CoreFoundation                      0x0000000104b7fcf2 +[NSException raise:format:arguments:] + 98
3   Foundation                          0x0000000105732536 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4   UIKit                               0x0000000109adac46 -[UIApplication _runWithMainScene:transitionContext:completion:] + 3343
5   UIKit                               0x0000000109ad77f3 -[UIApplication workspaceDidEndTransaction:] + 182
6   FrontBoardServices                  0x000000011246f5f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
7   FrontBoardServices                  0x000000011246f46d -[FBSSerialQueue _performNext] + 186
8   FrontBoardServices                  0x000000011246f7f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
9   CoreFoundation                      0x0000000104b21c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
10  CoreFoundation                      0x0000000104b070cf __CFRunLoopDoSources0 + 527
11  CoreFoundation                      0x0000000104b065ff __CFRunLoopRun + 911
12  CoreFoundation                      0x0000000104b06016 CFRunLoopRunSpecific + 406
13  UIKit                               0x0000000109ad608f -[UIApplication _run] + 468
14  UIKit                               0x0000000109adc134 UIApplicationMain + 159
15  ???                                 0x00000001282fcd94 0x0 + 4969188756
16  ???                                 0x00000001282fc9dd 0x0 + 4969187805

I will appreciate if someone can help me with this

Upvotes: 0

Views: 579

Answers (1)

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

The issue might be an unhandled exception somewhere in any of the threads. You might want to catch the real exception by following this - Forms with Prism: Application windows are expected to have a root VC

Instead of trying to set focus on an invisible control which mostly be unexpected behaviour, why not use Date picker as a dialog from ACR User Dialogs for Xamarin and Windows

Upvotes: 1

Related Questions