public void ApplyBindings()
 {
     if (_layoutInflaterResult != null)
     {
         _layoutInflaterResult?.ApplyBindings();
         _layoutInflaterResult = null;
     }
 }
 public ViewHolderImpl(LayoutInflaterResult result, int viewType)
     : base(result.View)
 {
     if (viewType != global::Android.Resource.Layout.SimpleListItem1)
     {
         _bindingContext       = BindingServiceProvider.ContextManager.GetBindingContext(result.View);
         _layoutInflaterResult = result;
     }
 }
        private static bool MenuItemUpdateActionView(IBindingMemberInfo bindingMemberInfo, IMenuItem menuItem, object content)
        {
            var actionView = menuItem.GetActionView();

            if (actionView != null)
            {
                ParentObserver.Set(actionView, null);
            }

            object template = menuItem.GetBindingMemberValue(AttachedMembers.MenuItem.ActionViewTemplateSelector)?.SelectTemplate(content, menuItem);

            if (template != null)
            {
                content = template;
            }
            if (content == null)
            {
#if APPCOMPAT
                MenuItemCompat.SetActionView(menuItem, null);
#else
                menuItem.SetActionView(null);
#endif
                return(true);
            }

            LayoutInflaterResult result = null;
            int viewId;
            if (int.TryParse(content.ToString(), out viewId))
            {
                result  = GetContextFromItem(menuItem).GetBindableLayoutInflater().InflateEx(viewId, null, false);
                content = result.View;
            }

            actionView = content as View;
            if (actionView == null)
            {
                Type viewType = TypeCache <View> .Instance.GetTypeByName(content.ToString(), true, true);

                actionView = viewType.CreateView(GetContextFromItem(menuItem));
            }
#if APPCOMPAT
            MenuItemCompat.SetActionView(menuItem, actionView);
#else
            menuItem.SetActionView(actionView);
#endif

            ParentObserver.Set(actionView, menuItem);
            var bindings = GetActionViewBind(menuItem);
            if (!string.IsNullOrEmpty(bindings))
            {
                BindingServiceProvider.BindingProvider.CreateBindingsFromString(actionView, bindings, null);
            }
            result?.ApplyBindings();
            return(true);
        }
        public static LayoutInflaterResult InflateEx(this LayoutInflater layoutInflater, XmlReader parser, ViewGroup root, bool attachToRoot)
        {
            var oldResult = CurrentLayoutInflaterResult;

            try
            {
                var result = new LayoutInflaterResult();
                CurrentLayoutInflaterResult = result;
                result.View = layoutInflater.Inflate(parser, root, attachToRoot);
                return(result);
            }
            finally
            {
                CurrentLayoutInflaterResult = oldResult;
            }
        }
Beispiel #5
0
 public static void RegisterActionBarTabMembers()
 {
     MemberProvider.Register(AttachedBindingMember.CreateAutoProperty(AttachedMembers.ActionBarTab.Content.Override <ActionBar.Tab>()));
     MemberProvider.Register(AttachedBindingMember.CreateAutoProperty(AttachedMembers.ActionBarTab.ContentTemplateSelector.Override <ActionBar.Tab>()));
     MemberProvider.Register(AttachedBindingMember.CreateAutoProperty(AttachedMembers.ActionBarTab.ContentTemplate.Override <ActionBar.Tab>()));
     MemberProvider.Register(AttachedBindingMember
                             .CreateNotifiableMember <ActionBar.Tab, string>(nameof(ActionBar.Tab.ContentDescription),
                                                                             (info, tab) => tab.ContentDescription,
                                                                             (info, tab, value) =>
     {
         tab.SetContentDescription(value);
         return(true);
     }));
     MemberProvider.Register(AttachedBindingMember
                             .CreateNotifiableMember(AttachedMembers.ActionBarTab.CustomView.Override <ActionBar.Tab>(),
                                                     (info, tab) => tab.CustomView, (info, tab, value) =>
     {
         LayoutInflaterResult result = null;
         if (tab.CustomView != null)
         {
             ParentObserver.Set(tab.CustomView, null);
         }
         if (value is int)
         {
             result = GetContextFromItem(tab).GetBindableLayoutInflater().InflateEx((int)value, null, false);
             value  = result.View;
         }
         tab.SetCustomView((View)value);
         if (tab.CustomView != null)
         {
             ParentObserver.Set(tab.CustomView, tab);
         }
         result?.ApplyBindings();
         return(true);
     }));
     MemberProvider.Register(AttachedBindingMember
                             .CreateNotifiableMember(AttachedMembers.ActionBarTab.Icon.Override <ActionBar.Tab>(),
                                                     (info, tab) => tab.Icon, (info, tab, value) =>
     {
         if (value is int)
         {
             tab.SetIcon((int)value);
         }
         else
         {
             tab.SetIcon((Drawable)value);
         }
         return(true);
     }));
     MemberProvider.Register(AttachedBindingMember
                             .CreateNotifiableMember <ActionBar.Tab, string>(nameof(ActionBar.Tab.Text),
                                                                             (info, tab) => tab.Text,
                                                                             (info, tab, value) =>
     {
         tab.SetText(value);
         return(true);
     }));
     MemberProvider.Register(AttachedBindingMember
                             .CreateNotifiableMember <ActionBar.Tab, Object>(nameof(ActionBar.Tab.Tag),
                                                                             (info, tab) => tab.Tag,
                                                                             (info, tab, value) =>
     {
         tab.SetTag(value);
         return(true);
     }));
 }
Beispiel #6
0
        public static void RegisterActionBarMembers()
        {
            MemberProvider.Register(AttachedBindingMember.CreateAutoProperty(AttachedMembers.ActionBar.ItemsSource.Override <ActionBar>(), (bar, args) => ActionBarUpdateItemsSource(bar)));
            MemberProvider.Register(AttachedBindingMember.CreateAutoProperty(AttachedMembers.ActionBar.SelectedItem.Override <ActionBar>(), ActionBarSelectedItemChanged));
            MemberProvider.Register(AttachedBindingMember.CreateAutoProperty(AttachedMembers.ActionBar.ContextActionBarVisible.Override <ActionBar>(), ActionBarContextActionBarVisibleChanged));
            MemberProvider.Register(AttachedBindingMember.CreateAutoProperty(AttachedMembers.ActionBar.ContextActionBarTemplate.Override <ActionBar>()));

            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.BackgroundDrawable.Override <ActionBar>(),
                                                        (actionBar, args) =>
            {
                if (args.NewValue is int)
                {
                    actionBar.SetBackgroundDrawable(
                        actionBar.ThemedContext.Resources.GetDrawable((int)args.NewValue));
                }
                else
                {
                    actionBar.SetBackgroundDrawable((Drawable)args.NewValue);
                }
            }));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateNotifiableMember(AttachedMembers.ActionBar.CustomView.Override <ActionBar>(),
                                                            (info, actionBar) => actionBar.CustomView,
                                                            (info, actionBar, value) =>
            {
                LayoutInflaterResult result = null;
                if (actionBar.CustomView != null)
                {
                    ParentObserver.Set(actionBar.CustomView, null);
                }
                if (value is int)
                {
                    result = actionBar.ThemedContext.GetBindableLayoutInflater().InflateEx((int)value, null, false);
                    value  = result.View;
                }
                actionBar.CustomView = (View)value;
                if (actionBar.CustomView != null)
                {
                    ParentObserver.Set(actionBar.CustomView, actionBar);
                }
                result?.ApplyBindings();
                return(true);
            }));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.DisplayHomeAsUpEnabled.Override <ActionBar>(),
                                                        (actionBar, args) => actionBar.SetDisplayHomeAsUpEnabled(args.NewValue)));

            MemberProvider.Register(AttachedBindingMember
                                    .CreateNotifiableMember(AttachedMembers.ActionBar.DisplayOptions.Override <ActionBar>().Cast <ActionBarDisplayOptions>(),
                                                            (info, actionBar) => actionBar.GetActionBarDisplayOptions(),
                                                            (info, actionBar, value) =>
            {
                actionBar.SetActionBarDisplayOptions(value);
                return(true);
            }));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.DisplayShowCustomEnabled.Override <ActionBar>(),
                                                        (actionBar, args) => actionBar.SetDisplayShowCustomEnabled(args.NewValue)));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.DisplayShowHomeEnabled.Override <ActionBar>(),
                                                        (actionBar, args) => actionBar.SetDisplayShowHomeEnabled(args.NewValue)));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.DisplayShowTitleEnabled.Override <ActionBar>(),
                                                        (actionBar, args) => actionBar.SetDisplayShowTitleEnabled(args.NewValue)));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.DisplayUseLogoEnabled.Override <ActionBar>(),
                                                        (actionBar, args) => actionBar.SetDisplayUseLogoEnabled(args.NewValue)));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.HomeButtonEnabled.Override <ActionBar>(),
                                                        (actionBar, args) => actionBar.SetHomeButtonEnabled(args.NewValue)));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.Icon.Override <ActionBar>(), (actionBar, args) =>
            {
                if (args.NewValue is int)
                {
                    actionBar.SetIcon((int)args.NewValue);
                }
                else
                {
                    actionBar.SetIcon((Drawable)args.NewValue);
                }
            }));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.Logo.Override <ActionBar>(), (actionBar, args) =>
            {
                if (args.NewValue is int)
                {
                    actionBar.SetLogo((int)args.NewValue);
                }
                else
                {
                    actionBar.SetLogo((Drawable)args.NewValue);
                }
            }));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateNotifiableMember(AttachedMembers.ActionBar.NavigationMode.Override <ActionBar>().Cast <ActionBarNavigationMode>(),
                                                            (info, actionBar) => actionBar.GetNavigationMode(), ActionBarSetNavigationMode));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.SplitBackgroundDrawable.Override <ActionBar>(),
                                                        (actionBar, args) =>
            {
                if (args.NewValue is int)
                {
                    actionBar.SetSplitBackgroundDrawable(
                        actionBar.ThemedContext.Resources.GetDrawable((int)args.NewValue));
                }
                else
                {
                    actionBar.SetSplitBackgroundDrawable((Drawable)args.NewValue);
                }
            }));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateAutoProperty(AttachedMembers.ActionBar.StackedBackgroundDrawable.Override <ActionBar>(),
                                                        (actionBar, args) =>
            {
                if (args.NewValue is int)
                {
                    actionBar.SetStackedBackgroundDrawable(
                        actionBar.ThemedContext.Resources.GetDrawable((int)args.NewValue));
                }
                else
                {
                    actionBar.SetStackedBackgroundDrawable((Drawable)args.NewValue);
                }
            }));
            MemberProvider.Register(AttachedBindingMember
                                    .CreateNotifiableMember(AttachedMembers.ActionBar.IsShowing.Override <ActionBar>(), (info, actionBar) => actionBar.IsShowing, SetActionBarIsShowing));


            MemberProvider.Register(AttachedBindingMember
                                    .CreateNotifiableMember(AttachedMembers.ActionBar.Visible.Override <ActionBar>(), (info, actionBar) => actionBar.IsShowing, SetActionBarIsShowing));
            MemberProvider.Register(AttachedBindingMember.CreateAutoProperty(AttachedMembers.ActionBar.DropDownItemTemplate.Override <ActionBar>()));
            MemberProvider.Register(AttachedBindingMember.CreateAutoProperty(AttachedMembers.ActionBar.DropDownItemTemplateSelector.Override <ActionBar>()));
        }
Beispiel #7
0
            private void SetContent(ActionBar.Tab tab, FragmentTransaction ft, int?placeHolder, Activity activity, ActionBar bar)
            {
                if (placeHolder == null)
                {
                    Tracer.Error("The placeholder for tab {0} was not found.", tab);
                    return;
                }
                var layout = activity.FindViewById <ViewGroup>(placeHolder.Value);

                if (layout == null)
                {
                    Tracer.Warn("The ActionBarTabContentId with id = {0} is not found in activity {1}",
                                placeHolder.Value,
                                activity);
                    return;
                }
                if (_content == null)
                {
#if APPCOMPAT
                    _content = tab.GetBindingMemberValue(AttachedMembersCompat.ActionBarTab.Content);
#else
                    _content = tab.GetBindingMemberValue(AttachedMembers.ActionBarTab.Content);
#endif
                    LayoutInflaterResult result = null;
                    var viewModel = _content as IViewModel;
                    if (viewModel == null)
                    {
                        var fragmentClass = _content as string;
                        //If content is a string, trying to create a fragment.
                        if (!string.IsNullOrEmpty(fragmentClass))
                        {
                            var type = TypeCache <Fragment> .Instance.GetTypeByName(fragmentClass, true, false);

                            if (type != null)
                            {
                                var fragment = Fragment.Instantiate(bar.ThemedContext, Java.Lang.Class.FromType(type).Name);
                                _content = fragment;
                            }
                        }
                        else if (_content is int)
                        {
                            result   = activity.GetBindableLayoutInflater().InflateEx((int)_content, null, false);
                            _content = result.View;
                        }
                    }
                    else
                    {
                        viewModel.Settings.Metadata.AddOrUpdate(ViewModelConstants.StateNotNeeded, true);
                    }
                    _content = AndroidToolkitExtensions.GetContentView(layout, layout.Context, _content,
                                                                       _contentTemplateProvider.GetTemplateId(), _contentTemplateProvider.GetDataTemplateSelector());
                    if (BindingServiceProvider.BindingManager.GetBindings(tab, AttachedMembersBase.Object.DataContext).Any())
                    {
                        _content.SetBindingMemberValue(AttachedMembersBase.Object.Parent, tab);
                    }
                    layout.SetContentView(_content, ft, (group, fragment, arg3) =>
                    {
                        if (fragment.IsDetached)
                        {
                            arg3.Attach(fragment);
                        }
                        else
                        {
                            arg3.Replace(group.Id, fragment);
                        }
                    });
                    result?.ApplyBindings();
                }
                else
                {
                    layout.SetContentView(_content, ft, (group, fragment, arg3) => arg3.Attach(fragment));
                }
            }
 protected virtual View CreateView(object value, ViewGroup parent, int templateId, out LayoutInflaterResult inflaterResult)
 {
     inflaterResult = LayoutInflater.InflateEx(templateId, parent, false);
     inflaterResult.View.SetTag(Resource.Id.ListTemplateId, templateId);
     return(inflaterResult.View);
 }
        protected virtual View CreateView(object value, View convertView, ViewGroup parent, DataTemplateProvider templateProvider, int defaultTemplate, out LayoutInflaterResult inflaterResult)
        {
            inflaterResult = null;
            var valueView = value as View;

            if (valueView != null)
            {
                return(valueView);
            }

            int?templateId = null;
            int id;

            if (templateProvider.TrySelectResourceTemplate(value, out id))
            {
                templateId = id;
            }
            else
            {
                object template;
                if (templateProvider.TrySelectTemplate(value, convertView, out template))
                {
                    if (template != null)
                    {
                        if (ReferenceEquals(convertView, template))
                        {
                            return(convertView);
                        }

                        valueView = template as View;
                        if (valueView != null)
                        {
                            valueView.SetDataContext(value);
                            return(valueView);
                        }
                        if (template is int)
                        {
                            templateId = (int)template;
                        }
                        else
                        {
                            value = template;
                        }
                    }
                }
                else
                {
                    templateId = templateProvider.GetTemplateId();
                }
            }

            if (templateId == null)
            {
                if (!(convertView is TextView))
                {
                    convertView = LayoutInflater.Inflate(defaultTemplate, null);
                }
                var textView = convertView as TextView;
                if (textView != null)
                {
                    textView.Text = value.ToStringSafe("(null)");
                }
                return(textView);
            }
            var oldId = GetViewTemplateId(convertView);

            if (oldId == null || oldId.Value != templateId.Value)
            {
                convertView = CreateView(value, parent, templateId.Value, out inflaterResult);
            }
            convertView.SetDataContext(value);
            return(convertView);
        }