Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to declare MenuItems in Xaml?

$
0
0

(The full POC is here as I'm trying to determine if it is a bug, or if the MenuItems have not been properly declared in Xaml - https://github.com/Xamtastic/MenuItemsPOC )

I've created a custom control of type ViewCell, which I have declared in Xaml - how do I declare the MenuItems in Xaml?

I read somewhere that declaring them in Xaml as follows by default is consumed as an ObservableCollection, so long as the backing bindable property was also an ObservableCollection.

The Xaml is as follows, and one of the properties follows it:

(To explain why there are 4 sets of MenuItem, I want to be able to dynamically switch the push to swipe MenuItems in the ListView, when a 'Status' property changes)

    <DataTemplate x:Key="MainTemplate">
        <controls:SwitchableMenuItemsDemoViewCell
            Title="{Binding Title}"
            Status="{Binding Status}"
            >
            <controls:SwitchableMenuItemsDemoViewCell.PendingMenuItems>
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Pend. 1" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Pend. 2" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Pend. 3" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Pend. 4" />                       
            </controls:SwitchableMenuItemsDemoViewCell.PendingMenuItems>
            <controls:SwitchableMenuItemsDemoViewCell.UploadMenuItems>
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Uplo. 1" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Uplo. 2" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Uplo. 3" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Uplo. 4" />                       
            </controls:SwitchableMenuItemsDemoViewCell.UploadMenuItems>
            <controls:SwitchableMenuItemsDemoViewCell.CompleteMenuItems>
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Comp. 1" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Comp. 2" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Comp. 3" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Comp. 4" />                       
            </controls:SwitchableMenuItemsDemoViewCell.CompleteMenuItems>
            <controls:SwitchableMenuItemsDemoViewCell.FailedMenuItems>
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Fail. 1" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Fail. 2" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Fail. 3" />
                <MenuItem Clicked="DemoMenuItems" CommandParameter="{Binding DisplayData.DocumentId}" Text="Fail. 4" />                       
            </controls:SwitchableMenuItemsDemoViewCell.FailedMenuItems>                
            </controls:SwitchableMenuItemsDemoViewCell>
    </DataTemplate>

        #region ObservableCollection<MenuItem> PendingMenuItems [BindableProperty]
        public static readonly BindableProperty PendingMenuItemsProperty =
             BindableProperty.CreateAttached(
                  "PendingMenuitems",
                  typeof(ObservableCollection<MenuItem>),
                  typeof(SwitchableMenuItemsDemoViewCell),
                  new ObservableCollection<MenuItem>(),
                  BindingMode.OneWay);

        public ObservableCollection<MenuItem> PendingMenuItems
        {
            get { return (ObservableCollection<MenuItem>)GetValue(PendingMenuItemsProperty); }
            set { SetValue(PendingMenuItemsProperty, value); }
        }
        #endregion

The problem that I have using the Xaml declarations as above are that despite declaring 4 for each bindableproperty, that each time one of 4 ViewCells load, 4 items are added in addition to the previous items.

The following Debug in one of the property changes is as follows:

            this.PropertyChanged += (s, e) =>
            {
                if(e.PropertyName.Equals(SwitchableMenuItemsDemoViewCell.StatusProperty.PropertyName))
                {
        // Here I would switch on the Status property to decide 
        // which set of ObservableCollection<MenuItem> to add to the 
        // ViewCell's .ContextActions property, which is where the MenuItems are set 

                    Debug.WriteLine($"Pending Count: {PendingMenuItems.Count}");
                    Debug.WriteLine($"Upload Count: {UploadMenuItems.Count}");
                    Debug.WriteLine($"Complete Count: {CompleteMenuItems.Count}");
                    Debug.WriteLine($"Failed Count: {FailedMenuItems.Count}");
                }
            };

However I end up with the following output:

Pending Count: 4
Upload Count: 4
Complete Count: 4
Failed Count: 4
Pending Count: 8
Upload Count: 8
Complete Count: 8
Failed Count: 8
Pending Count: 12
Upload Count: 12
Complete Count: 12
Failed Count: 12
Pending Count: 16
Upload Count: 16
Complete Count: 16
Failed Count: 16

There should always only be 4 for each as only 4 were declared.

Any ideas on how to properly declare the MenuItems, or is this an actual bug?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>