Issue with binding to GridLayout to Android

后端 未结 2 622
难免孤独
难免孤独 2020-12-21 10:41

I create game using technology Mono. While writing, I have a problem. I need create a grid (4 to 5) with pictures. I thought to apply here GridLayout or GridView. But I do

2条回答
  •  醉话见心
    2020-12-21 11:05

    Now the example of BindableGridView is on https://github.com/slodge/MvvmCross/issues/37

    Here is the solution we used to bind images on Buttons when the name of the file comes from a viewmodel:

    1) Create a binding

    public class MvxButtonIconBinding: MvxBaseAndroidTargetBinding
    {
        private readonly View _view;
    
        public MvxButtonIconBinding(View view)
        {
            _view = view;
        }
    
        public override void SetValue(object value)
        {
            string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            path = Path.Combine(path, "pictures");
            path = Path.Combine(path, (string)value);
            if (File.Exists(path))
            {
                var dr = Drawable.CreateFromPath(path);
                Button b = _view as Button;
                var drawables = b.GetCompoundDrawables();
                foreach (var d in drawables)
                    if (d!=null)
                        d.Dispose(); // To avoid "out of memory" messages
                b.SetCompoundDrawablesWithIntrinsicBounds(null, dr, null, null);
            }
            else
            {
                Console.WriteLine("File {0} does not exists", path);
            } 
        }
    
        public override MvxBindingMode DefaultMode
        {
            get { return MvxBindingMode.OneWay; }
        }
    
        public override Type TargetType
        {
            get { return typeof(string); }
        }
    
        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
            }
            base.Dispose(isDisposing);
        }
    }
    

    2) Setup the binding :

     registry.RegisterFactory(new MvxCustomBindingFactory("ButtonIcon", view => new MvxButtonIconBinding(view)));
    

    3) Use it in your gridview/listview:

        
    

    "article_buttonlayout" :

     

    Where "Item" is my object, and "PictureFileName", the filename on the local filesystem.
    I'm new on C#, be indulgent if you find errors :)

提交回复
热议问题