Exemplo n.º 1
0
        Fragment InstantiateFragment <T>() where T : MvxFragment
        {
            var fragment = (MvxFragment)Fragment.Instantiate(activity, Class.FromType(typeof(T)).Name);

            fragment.DataContext = Mvx.Resolve(typeof(T).GetProperties().First(p => p.Name == "ViewModel").PropertyType);
            return(fragment);
        }
Exemplo n.º 2
0
 private void SetupViewPager()
 {
     adapter = new ViewPagerAdapter(SupportFragmentManager);
     adapter.AddFragment(Fragment.Instantiate(this, Class.FromType(typeof(SearchFragment)).Name), Resources.GetString(Resource.String.FragmentSearch));
     adapter.AddFragment(Fragment.Instantiate(this, Class.FromType(typeof(MatchFragment)).Name), Resources.GetString(Resource.String.FragmentMatch));
     //adapter.AddFragment(Fragment.Instantiate(this, Class.FromType(typeof(DesignFragment)).Name), Resources.GetString(Resource.String.FragmentDesign));
     adapter.AddFragment(Fragment.Instantiate(this, Class.FromType(typeof(MyClothesFragment)).Name), Resources.GetString(Resource.String.FragmentMyClothes));
     viewPager.Adapter = adapter;
 }
Exemplo n.º 3
0
        public override Fragment GetItem(int position)
        {
            var fragmentInfo = Fragments.ElementAt(position);
            var fragment     = Fragment.Instantiate(_context,
                                                    FragmentJavaName(fragmentInfo.FragmentType));

            ((MvxFragment)fragment).ViewModel = fragmentInfo.ViewModel;
            return(fragment);
        }
Exemplo n.º 4
0
        public override Fragment GetItem(int position)
        {
            var fragInfo = Fragments.ElementAt(position);

            if (fragInfo.CachedFragment == null)
            {
                fragInfo.CachedFragment = Fragment.Instantiate(_context, FragmentJavaName(fragInfo.FragmentType));

                var request = new MvxViewModelRequest(fragInfo.ViewModelType, null, null);
                ((IMvxView)fragInfo.CachedFragment).ViewModel = Mvx.Resolve <IMvxViewModelLoader>().LoadViewModel(request, null);
            }

            return(fragInfo.CachedFragment);
        }
        /// <summary>
        ///     Show Fragment with a specific tag at a specific placeholder
        /// </summary>
        /// <param name="tag">The tag for the fragment to lookup</param>
        /// <param name="contentId">Where you want to show the Fragment</param>
        /// <param name="bundle">Bundle which usually contains a Serialized MvxViewModelRequest</param>
        /// <param name="forceAddToBackStack">If you want to force add the fragment to the backstack so on backbutton it will go back to it. Note: This will override IMvxCachedFragmentInfo.AddToBackStack configuration.</param>
        /// <param name="forceReplaceFragment">If you want the fragment to be re-created</param>
        protected virtual void ShowFragment(string tag, int contentId, Bundle bundle, bool forceAddToBackStack = false, bool forceReplaceFragment = false)
        {
            IMvxCachedFragmentInfo fragInfo;

            FragmentCacheConfiguration.TryGetValue(tag, out fragInfo);

            IMvxCachedFragmentInfo currentFragInfo = null;
            var currentFragment = SupportFragmentManager.FindFragmentById(contentId);

            if (currentFragment != null)
            {
                FragmentCacheConfiguration.TryGetValue(currentFragment.Tag, out currentFragInfo);
            }

            if (fragInfo == null)
            {
                throw new MvxException("Could not find tag: {0} in cache, you need to register it first.", tag);
            }

            // We shouldn't replace the current fragment unless we really need to.
            FragmentReplaceMode fragmentReplaceMode = FragmentReplaceMode.ReplaceFragmentAndViewModel;

            if (!forceReplaceFragment)
            {
                fragmentReplaceMode = ShouldReplaceCurrentFragment(fragInfo, currentFragInfo, bundle);
            }

            if (fragmentReplaceMode == FragmentReplaceMode.NoReplace)
            {
                return;
            }

            var ft = SupportFragmentManager.BeginTransaction();

            OnBeforeFragmentChanging(fragInfo, ft);

            fragInfo.ContentId = contentId;

            //If we already have a previously created fragment, we only need to send the new parameters
            if (fragInfo.CachedFragment != null && fragmentReplaceMode == FragmentReplaceMode.ReplaceFragment)
            {
                ((Fragment)fragInfo.CachedFragment).Arguments.Clear();
                ((Fragment)fragInfo.CachedFragment).Arguments.PutAll(bundle);
            }
            else
            {
                //Otherwise, create one and cache it
                fragInfo.CachedFragment = Fragment.Instantiate(this, FragmentJavaName(fragInfo.FragmentType),
                                                               bundle) as IMvxFragmentView;
                OnFragmentCreated(fragInfo, ft);
            }

            ft.Replace(fragInfo.ContentId, fragInfo.CachedFragment as Fragment, fragInfo.Tag);

            //if replacing ViewModel then clear the cache after the fragment
            //has been added to the transaction so that the Tag property is not null
            //and the UniqueImmutableCacheTag property (if not overridden) has the correct value
            if (fragmentReplaceMode == FragmentReplaceMode.ReplaceFragmentAndViewModel)
            {
                var cache = Mvx.GetSingleton <IMvxMultipleViewModelCache>();
                cache.GetAndClear(fragInfo.ViewModelType, GetTagFromFragment(fragInfo.CachedFragment as Fragment));
            }

            if ((currentFragment != null && fragInfo.AddToBackStack) || forceAddToBackStack)
            {
                ft.AddToBackStack(fragInfo.Tag);
            }

            OnFragmentChanging(fragInfo, ft);
            ft.Commit();
            SupportFragmentManager.ExecutePendingTransactions();
            OnFragmentChanged(fragInfo);
        }