ToolbarItem Command binding doesn't work

.. when defined in xaml:
<ContentPage.ToolbarItems>

Posts

  • EricMaupinEricMaupin USXamarin Team Xamurai

    Going to need more information than that. Have you double checked there are no binding errors in the debug output?

  • InquisitorJaxInquisitorJax USMember
    edited June 2014

    yip - I have a button on the same page with exactly the same binding, and it works just fine.

    Button Text="View" Grid.Row="2" Command="{Binding ExecuteButtonCommand}" [works]

    ToolbarItem Name="view" Icon="Toolkit.Content/ApplicationBar.Add.png" Command="{Binding ExecuteButtonCommand}" [does not work]

  • DanielLDanielL PLInsider ✭✭✭✭

    Something is wrong with toolbars on Android platform. I couldn't make them working. If only one toolbar item is used everything is ok.

    Please take a look at:
    http://forums.xamarin.com/discussion/comment/56822/
    http://forums.xamarin.com/discussion/comment/56807/
    Is it a bug?

  • ravensorbravensorb USMember ✭✭
    edited June 2014

    I am running into the same issue --

    <toolkit:BaseView.ToolbarItems> <ToolbarItem Name="Validate" Command="{Binding ValidateCommand}" /> <ToolbarItem Name="Cancel" Command="{Binding CancelCommand}" /> </toolkit:BaseView.ToolbarItems>

    Does not work for me however this code (in the exact same Xaml) works without issue

    <Button Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Text="Login" Command="{Binding ValidateCommand}" />

    Note: I am also noticing odd issues with the CanExecute -- it doesn't seem to trigger when updating other properties in the view model.

  • EricMaupinEricMaupin USXamarin Team Xamurai

    So the issue here is that ToolbarItems were not inheriting their BindingContext properly, we've fixed this for the upcoming update.

  • rmarinhormarinho PTMember, Insider, Beta Xamurai

    About the coommand , i think thats a problem with the toolkit and RelayCommand @ShawnAnderson‌ .
    Its missing a RaiseCanExecuteChanged, so basicly in your vm, when something changes you do:

    ValidateCommand.RaiseCanExecuteChanged()

  • ravensorbravensorb USMember ✭✭

    good thought -- i was also thinking about that so I tried switching over to the Command object that is in the Xamarin.Forms library -- same issue.

  • JeffMiller.8034JeffMiller.8034 CAMember
    edited July 2014

    Looks like I am experiencing the same thing where the command binding is not working using xaml and <ToolbarItem . No binding errors in the application output window.

  • OlivierMatisOlivierMatis BEUniversity

    Got same issue with 1.2.2.0. If I don't set the BindingContext explicitly in XAML on the ToolBarItem (using <ToolBarItem.BindingContext>) it doesn't take the ContentPage one and is Null

  • nikolaj.skovnikolaj.skov DKMember ✭✭

    @ermau When is this bug planned to be released? In Forms version 1.2.2.6243 I still see this.

  • JaysonGoJaysonGo USMember

    Same problem as described by this thread. Two questions:

    1. It's mentioned that this has been fixed? When was it fixed?
      I'm running Xamarin 3.9.41.0, Xamarin.Android 4.20.0.24 and I'm experiencing this issue.

    2. If it's not fixed in the versions that I mentioned, what is the best way to set the BindingContext? Per ToolbarItem? Or can it be done by it's Page.ToolbarItems container? I tried the latter using something similar to:


    ...
    <ContentPage.ToolbarItems BindingContext="{x:Reference page}">
    ..

    But that doesn't seem to work.

    Thanks,
    Jayson

  • JaysonGoJaysonGo USMember

    Looks like the ToolBarItems collection can't have a BindingContext. I was able to temporarily solve this with my code-behind by just iterating against the ToolBarItems and setting the context to the same context as my page (which is my already instantiated view model).

  • VincentPoirierVincentPoirier CAMember ✭✭

    I'm experiencing the same issue on Xamarin.Forms 1.3.1.

    <LoadingContentPage.ToolbarItems>
        <ToolbarItem Name="Filtrer"
                     Order="Primary"
                     Priority="0"
                     Icon="ic_content_filter_list.png"
                     Command="ViewFilterCommand" />
    </LoadingContentPage.ToolbarItems>
    

    And

    public static readonly BindableProperty ViewFilterCommandProperty =
                BindableProperty.Create("ViewFilterCommand", typeof(Command), typeof(ClaimHistoryViewModel), default(Command));
    public Command ViewFilterCommand
    {
        get { return (Command)GetValue(ViewFilterCommandProperty); }
        set { SetValue(ViewFilterCommandProperty, value); }
    }
    

    Command creation

    ViewFilterCommand = new Command(async (sender) =>
    {
        DisplayActionSheet("Filtrer", new string[]{"En suspend", "En traitement", "Refusée", "Acceptée"});
    });
    

    It generates an exception which I think is from XAML. When InitializeComponent (); is called.

    System.Diagnostics.Debugger.Mono_UnhandledException_internal () in

    Am I doing something wrong? (I hope xP)

  • VincentPoirierVincentPoirier CAMember ✭✭

    The fix is the following:

    <LoadingContentPage.ToolbarItems>
        <ToolbarItem Name="Filtrer"
                     Order="Primary"
                     Priority="0"
                     Icon="ic_content_filter_list.png"
                     Command="{Binding ViewFilterCommand}" />
    </LoadingContentPage.ToolbarItems>
    
  • SteveMacdonaldSteveMacdonald CAUniversity, Certified XTC Partners ✭✭

    Still broken.

                var tbi = new ToolbarItem {Text = "New", Priority=0, Order=ToolbarItemOrder.Primary };
                tbi.SetBinding(Button.CommandProperty, new Binding("NewCommand"));
                tbi.BindingContext = this.BindingContext;
                ToolbarItems.Add(tbi);
    

    On Android:

    Error while resolving expression: One or more errors occurred.
    06-30 10:55:45.431 I/Choreographer( 7136): Skipped 413 frames!  The application may be doing too much work on its main thread.
    06-30 10:55:45.436 D/Mono    ( 7136): DllImport attempting to load: '/system/lib/liblog.so'.
    06-30 10:55:45.436 D/Mono    ( 7136): DllImport loaded library '/system/lib/liblog.so'.
    06-30 10:55:45.436 D/Mono    ( 7136): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
    06-30 10:55:45.436 D/Mono    ( 7136): Searching for '__android_log_print'.
    06-30 10:55:45.436 D/Mono    ( 7136): Probing '__android_log_print'.
    06-30 10:55:45.436 D/Mono    ( 7136): Found as '__android_log_print'.
    06-30 10:55:45.437 I/MonoDroid( 7136): UNHANDLED EXCEPTION:
    06-30 10:55:45.437 I/MonoDroid( 7136): System.InvalidCastException: Cannot cast from source type to destination type.
    06-30 10:55:45.438 I/MonoDroid( 7136): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x0004b>
    06-30 10:55:45.438 I/MonoDroid( 7136): at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) <IL 0x00006, 0x0003b>
    06-30 10:55:45.438 I/MonoDroid( 7136): at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
    06-30 10:55:45.438 I/MonoDroid( 7136): at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
    06-30 10:55:45.438 I/MonoDroid( 7136): at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Java.Lang.IRunnable.cs:71
    06-30 10:55:45.438 I/MonoDroid( 7136): at (wrapper dynamic-method) object.ceef60d3-4c92-4518-8740-3725d1e9a882 (intptr,intptr) <IL 0x00011, 0x0001f>
    06-30 10:55:45.441 W/art     ( 7136): JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
    06-30 10:55:45.442 D/AndroidRuntime( 7136): Shutting down VM
    An unhandled exception occured.
    

    If I just change the toolbar item to a normal button it all works fine.

  • NMackayNMackay GBInsider, University mod

    This is still broken :neutral:

    This was reported back in June 2014.

  • JulienRosenJulienRosen CAMember ✭✭✭✭
    edited January 2016

    <ContentPage.ToolbarItems> <ToolbarItem Text="Search" Icon="Toolbarsave.png" Command="{Binding CmdSearch}" /> </ContentPage.ToolbarItems>

    That works fine for me, as does this

                var toolbarItem = new ToolbarItem();
                toolbarItem.SetBinding(MenuItem.TextProperty, new Binding("ActionBarButtonText"));
                toolbarItem.SetBinding(MenuItem.CommandProperty, new Binding("ActionBarButtonCommand"));
                toolbarItem.SetBinding(MenuItem.IconProperty, new Binding("ActionBarButtonImage"));`
    
  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    Works for me too

  • NMackayNMackay GBInsider, University mod

    @NMackay said:
    This is still broken :neutral:

    This was reported back in June 2014.

    Working now in the latest release. Not sure why it wasn't working in Android for me before.

  • daizdaiz USMember

    ToolbarItems do not work for me.
    <ContentPage.ToolbarItems> <ToolbarItem Text="TEST"></ToolbarItem> </ContentPage.ToolbarItems> <StackLayout> <ListView ItemsSource="{Binding Path=Programs}"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.View> <StackLayout> <Label Text="{Binding Path=Name}"></Label> <Label Text="{Binding Path=ProgramId}"></Label> </StackLayout> </ViewCell.View> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout>

    Please advise

  • LucasMcNameeLucasMcNamee USMember

    @NMackay
    Hey guys I came here with the same problem and then I realized that the problem was that the Command in my ViewModel was private. If your Command is private Xamarin will just print a line saying: "Cannot find property...". Kinda silly but it got me so maybe this was your problem too.

  • I have the same issue but it doesn't work only when there is a ControlTemplate on the page. If I remove the ControlTemplate then the binding is OK. To workaround the problem I specify the Source of the binding when there is a ControlTemplate :

    Command="{Binding BindingContext.MyCommand, Source={x:Reference uxPageName}}"

  • MaxFMaxF DEMember

    Got the same error. I had created the command (new ...) inside the OnNavigatedTo method and it wasn't bindable. So unfortunately the bound command still null.

Sign In or Register to comment.