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

x:Reference Array

$
0
0

I've got an IMarkupExtension like so:

            <controls:TouchableView 
                    x:Name="touchGrid"
                    HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
                    AffectedStatefulObjects="{ext:StatefulMarkup Names='flavorText,flavorShape,flavorBackground', ParentView={x:Reference contentView}}">
            </controls:TouchableView>

    public class StatefulMarkupExtension : IMarkupExtension
    {
        public IAffinityStatechangable[] AffectedStatefulObjects;
        public ContentView ParentView { get; set; }
        public string Names { get; set; }
        //public IAffinityStatechangable[] Names { get; set; }

        public StatefulMarkupExtension()
        {
        }

        object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
        {
            IProvideValueTarget provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            var parent = ParentView;
            string[] names = Names.Split(',');
            List<IAffinityStatechangable> objects = new List<IAffinityStatechangable>();
            foreach (string n in names)
            {
                var t = parent.FindByName(n) as IAffinityStatechangable;
                objects.Add(t);
            }
            return objects.ToArray();
        }
    }

How can I provide a list of objects as an x:Array of x:References of some type, where in the above, flavorText, flavorShape, and flavorBackground are all custom controls in the same contentView as contentView that have a common interface I made called IAffinityStatechangable?

Seems a lot cleaner than passing a string of names and splitting it, even though it works, plus I wouldn't have to do a "FindByName." I want Names to be an IAffinityStatechangable[] as commented out above.


Viewing all articles
Browse latest Browse all 89864

Trending Articles



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