Xamarin Forms ListView swipe action issues on iOS

AmolGaikwad.5908AmolGaikwad.5908 INUniversity ✭✭

Greeting,

I am using ContextActions & MenuItem for swipe functionality on listview cell.

I have requirement to set backgroundImage or background colour to menu item. There is not such kind of property available so please suggest any approach to achieve this functionality.

Below icon property not get reflected in iOS.

new MenuItem { Text = "", Icon = "iconname", IsDestructive = false };

Please suggest any approach. Please consider the case I have 4 buttons on swipe cell.

Thanks in advance.

Posts

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @AmolGaikwad.5908 ,

    I believe you do need to use the image file extension. See:
    https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Local_Images

    and the code sample from the above linked section:
    var beachImage = new Image { Aspect = Aspect.AspectFit }; beachImage.Source = ImageSource.FromFile("waterfront.jpg");

    Let me know if this helps.

  • AmolGaikwad.5908AmolGaikwad.5908 INUniversity ✭✭

    Hi Jon Glodberger,

    Thanks for the reply.

    But my issue is not related to set Source to Image.

    I have ListView in my app. Listview contains the ContextActions with 4 four menu items. I have requirement to set background image to menu items.

    Hope above description will clarify my query.

    Thanks,
    Amol G

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    OK, but you have this in your code above:
    Icon = "iconname"

    I believe it should be
    Icon = "iconname.png" // pr whatever the correct image file extension is... but it is very likely that only PNG's are supported for icons. Some platforms, including iOS I believe, only allow png's to be used for menu icons.

  • AmolGaikwad.5908AmolGaikwad.5908 INUniversity ✭✭

    Hi @JGoldberger ,

    I already tried png non png stuffs.

    Its not issue about Image.

    Issue is in ListView i have ContextActions inside it i have menu button. & menu button has icon property.
    That icon property only works with android Not with iOS.

    Hope this time you got my problem.

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @AmolGaikwad.5908 Can you show a mock up of your requirement? May be it was implemented elsewhere in your company...

  • I guess what Amol is talking about is that iOS does not display icons in the context menus. This is a really odd behavior because iOS already supports icons there.

    <ViewCell.ContextActions> <MenuItem Icon="rename.png" Clicked="OnRename" CommandParameter="{Binding .}" Text="Rename" /> </ViewCell.ContextActions>

    The icon rename.png does not display in iOS.

  • AmolGaikwad.5908AmolGaikwad.5908 INUniversity ✭✭
    edited September 2016

    @ArnoHofmann

    The bydefault iOS is not implementing icon property.

    I have written Dependency Service to set the image to contextaction button.

  • @AmolGaikwad.5908

    Good to know that a renderer will work, could you share it here? Thanks Arno

  • AmolGaikwad.5908AmolGaikwad.5908 INUniversity ✭✭
    edited September 2016

    @ArnoHofmann :

    I have referred SWTableViewCell as dependacy service. I am writing Nuget package for the same once i done with it will share it asap.

    You need to create interface & implementation for it.

    Seg1 Implementation :

        using System;
        using System.Reflection;
        using UIKit;
        using test.iOS;
        using CoreGraphics;
    
        [assembly: Xamarin.Forms.Dependency (typeof (SWTableViewCell))]
        namespace test.iOS
        {
            public class SWTableViewCell : ISWTableViewCell
            {
                #region ISWTableViewCell implementation
    
                public void SetRightButtons (Type bindingContextType)
                {
                    var t = Type.GetType("Xamarin.Forms.Platform.iOS.ContextActionsCell, Xamarin.Forms.Platform.iOS, Version=" + typeof(Xamarin.Forms.Forms).Assembly.GetName().Version + ", Culture=neutral, PublicKeyToken=null");
                    var destructiveBackground = t.GetField ("DestructiveBackground", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                    var normalBackground = t.GetField ("NormalBackground", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
    
                    normalBackground.SetValue(null, UIImage.FromFile("image1"));
                    destructiveBackground.SetValue(null, UIImage.FromFile("imag2"));
                }
    
                public void DisableAnimations()
                {
                    UIView.AnimationsEnabled = false;
                }
    
                public void EnableAnimations()
                {
                    UIView.AnimationsEnabled = true;
                }
    
                #endregion
            }
        }
    

    Seg2 : Interface :

    using System;
    
    namespace AppName
    {
        public interface ISWTableViewCell
        {
            void SetRightButtons(Type bindingContext);
            void DisableAnimations();
            void EnableAnimations();
        }
    }
    

    Seg 3 : Invoke it from your cell

            if (Device.OS == TargetPlatform.iOS)
                {
                    DependencyService.Get<ISWTableViewCell>().SetRightButtons(typeof(Customcell1));
                }
    
  • @AmolGaikwad.5908:

    Thank you very much for sharing your code, great work!

  • rio_riyario_riya INMember ✭✭

    @AmolGaikwad.8043 have you tried, with two menu item one text and another as image?

  • Ganesh.4229Ganesh.4229 USMember ✭✭

    @AmolGaikwad.5908 Could you please let me know where to invoke the cell in Xaml

    Thank you

Sign In or Register to comment.