public void WireWebView <T>(T webView)
        {
            WebView.SetWebContentsDebuggingEnabled(true);

            if (webView.GetType() == typeof(WebView))
            {
                _webView = webView as WebView;
                _webView.ClearCache(true);
                _webView.Settings.JavaScriptEnabled = true;

                _webViewClient = new BridgeWebViewClient();
                _webView.SetWebViewClient(_webViewClient);
            }
            else
            {
                _view = webView as Android.Views.ViewGroup;

                _webView = new WebView(_view.Context);
                _webView.ClearCache(true);
                _webView.Settings.JavaScriptEnabled = true;
                _webView.Settings.DomStorageEnabled = true;
                _webView.LayoutParameters           = new LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);

                _webViewClient = new BridgeWebViewClient();
                _webView.SetBackgroundColor(Android.Graphics.Color.Pink);
                _view.AddView(_webView);
                _webView.SetWebViewClient(_webViewClient);

                var button = new Button(_view.Context);
                button.Text = "My Dynamic Button";
                button.SetBackgroundColor(Android.Graphics.Color.Brown);
                button.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
                _view.AddView(button);
            }
        }
        Task RunWindowTest <THandler>(IWindow window, Func <THandler, Task> action)
            where THandler : class, IElementHandler
        {
            return(InvokeOnMainThreadAsync(async() =>
            {
                AViewGroup rootView = MauiContext.Context.GetActivity().Window.DecorView as AViewGroup;
                var linearLayoutCompat = new LinearLayoutCompat(MauiContext.Context);

                var fragmentManager = MauiContext.GetFragmentManager();
                var viewFragment = new WindowTestFragment(MauiContext, window);

                try
                {
                    linearLayoutCompat.Id = AView.GenerateViewId();
                    fragmentManager
                    .BeginTransaction()
                    .Add(linearLayoutCompat.Id, viewFragment)
                    .Commit();

                    rootView.AddView(linearLayoutCompat);
                    await viewFragment.FinishedLoading;

                    if (typeof(THandler).IsAssignableFrom(window.Handler.GetType()))
                    {
                        await action((THandler)window.Handler);
                    }
                    else if (typeof(THandler).IsAssignableFrom(window.Content.Handler.GetType()))
                    {
                        await action((THandler)window.Content.Handler);
                    }
                }
                finally
                {
                    if (window.Handler != null)
                    {
                        window.Handler.DisconnectHandler();
                    }

                    rootView.RemoveView(linearLayoutCompat);

                    fragmentManager
                    .BeginTransaction()
                    .Remove(viewFragment)
                    .Commit();

                    await linearLayoutCompat.OnUnloadedAsync();
                    if (viewFragment.View != null)
                    {
                        await viewFragment.View.OnUnloadedAsync();
                    }
                }
            }));
        }
        void ActivateStreamContainer(Android.Views.ViewGroup container, Android.Views.View view)
        {
            DeactivateStreamContainer(container);
            if (container == null || view == null)
            {
                return;
            }

            if (view.Parent != null)
            {
                (view.Parent as Android.Views.ViewGroup).RemoveView(view);
            }

            var layoutParams = new FrameLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, Android.Views.ViewGroup.LayoutParams.MatchParent);

            container.AddView(view, layoutParams);
        }
 public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
 {
     Context context = container.Context;
     LayoutInflater inflater = LayoutInflater.From(context);
     View view = null;
     if (position == 0) {
         view = inflater.Inflate (Resource.Layout.samples,container, false);
     }else{
         view = inflater.Inflate (Resource.Layout.item_font, container, false);
         RecyclerView recyclerView = view.FindViewById<RecyclerView> (Resource.Id.recyclerView);
         int nbColumns = AndroidUtils.getScreenSize ((Activity)context).width / context.Resources.GetDimensionPixelSize (Resource.Dimension.item_width);
         recyclerView.SetLayoutManager (new GridLayoutManager (context, nbColumns));
         recyclerView.SetAdapter (new IconAdapter (fonts [position-1].Value.Characters));
     }
     container.AddView(view);
     return view;
 }
Exemple #5
0
        public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
        {
            Context        context  = container.Context;
            LayoutInflater inflater = LayoutInflater.From(context);
            View           view     = null;

            if (position == 0)
            {
                view = inflater.Inflate(Resource.Layout.samples, container, false);
            }
            else
            {
                view = inflater.Inflate(Resource.Layout.item_font, container, false);
                RecyclerView recyclerView = view.FindViewById <RecyclerView> (Resource.Id.recyclerView);
                int          nbColumns    = AndroidUtils.getScreenSize((Activity)context).width / context.Resources.GetDimensionPixelSize(Resource.Dimension.item_width);
                recyclerView.SetLayoutManager(new GridLayoutManager(context, nbColumns));
                recyclerView.SetAdapter(new IconAdapter(fonts [position - 1].Value.Characters));
            }
            container.AddView(view);
            return(view);
        }
Exemple #6
0
        Task RunWindowTest <THandler>(IWindow window, Func <THandler, Task> action)
            where THandler : class, IElementHandler
        {
            return(InvokeOnMainThreadAsync(async() =>
            {
                AViewGroup rootView = MauiContext.Context.GetActivity().Window.DecorView as AViewGroup;
                _decorDrawable ??= rootView.Background;
                var linearLayoutCompat = new LinearLayoutCompat(MauiContext.Context);

                var fragmentManager = MauiContext.GetFragmentManager();
                var viewFragment = new WindowTestFragment(MauiContext, window);

                try
                {
                    linearLayoutCompat.Id = AView.GenerateViewId();
                    rootView.AddView(linearLayoutCompat);

                    fragmentManager
                    .BeginTransaction()
                    .Add(linearLayoutCompat.Id, viewFragment)
                    .Commit();

                    await viewFragment.FinishedLoading;

                    if (window.Content is Shell shell)
                    {
                        await OnLoadedAsync(shell.CurrentPage);
                    }

                    if (typeof(THandler).IsAssignableFrom(window.Handler.GetType()))
                    {
                        await action((THandler)window.Handler);
                    }
                    else if (typeof(THandler).IsAssignableFrom(window.Content.Handler.GetType()))
                    {
                        await action((THandler)window.Content.Handler);
                    }
                    else if (window.Content is ContentPage cp && typeof(THandler).IsAssignableFrom(cp.Content.Handler.GetType()))
                    {
                        await action((THandler)cp.Content.Handler);
                    }
                    else
                    {
                        throw new Exception($"I can't work with {typeof(THandler)}");
                    }
                }
                finally
                {
                    if (window.Handler != null)
                    {
                        window.Handler.DisconnectHandler();
                    }

                    fragmentManager
                    .BeginTransaction()
                    .Remove(viewFragment)
                    .Commit();

                    rootView.RemoveView(linearLayoutCompat);

                    await linearLayoutCompat.OnUnloadedAsync();
                    if (viewFragment.View != null)
                    {
                        await viewFragment.View.OnUnloadedAsync();
                    }

                    await viewFragment.FinishedDestroying;

                    // This is mainly to remove changes to the decor view that shell imposes
                    if (_decorDrawable != rootView.Background)
                    {
                        rootView.Background = _decorDrawable;
                    }

                    // Unset the Support Action bar if the calling code has set the support action bar
                    if (MauiContext.Context.GetActivity() is AppCompatActivity aca)
                    {
                        aca.SetSupportActionBar(null);
                    }

                    // Ideally this wouldn't be needed but I haven't found the right platform
                    // component I can key into for knowing when the world can move on
                    await Task.Delay(1000);
                    fragmentManager.ExecutePendingTransactions();
                }
            }));
Exemple #7
0
        Task RunWindowTest <THandler>(IWindow window, Func <THandler, Task> action)
            where THandler : class, IElementHandler
        {
            return(InvokeOnMainThreadAsync(async() =>
            {
                AViewGroup rootView = MauiContext.Context.GetActivity().Window.DecorView as AViewGroup;
                var decorBackground = rootView.Background;
                var linearLayoutCompat = new LinearLayoutCompat(MauiContext.Context);

                var fragmentManager = MauiContext.GetFragmentManager();
                var viewFragment = new WindowTestFragment(MauiContext, window);

                try
                {
                    linearLayoutCompat.Id = AView.GenerateViewId();
                    rootView.AddView(linearLayoutCompat);
                    fragmentManager
                    .BeginTransaction()
                    .Add(linearLayoutCompat.Id, viewFragment)
                    .Commit();

                    await viewFragment.FinishedLoading;

                    if (typeof(THandler).IsAssignableFrom(window.Handler.GetType()))
                    {
                        await action((THandler)window.Handler);
                    }
                    else if (typeof(THandler).IsAssignableFrom(window.Content.Handler.GetType()))
                    {
                        await action((THandler)window.Content.Handler);
                    }
                    else if (window.Content is ContentPage cp && typeof(THandler).IsAssignableFrom(cp.Content.Handler.GetType()))
                    {
                        await action((THandler)cp.Content.Handler);
                    }
                    else
                    {
                        throw new Exception($"I can't work with {typeof(THandler)}");
                    }
                }
                finally
                {
                    if (window.Handler != null)
                    {
                        window.Handler.DisconnectHandler();
                    }

                    rootView.RemoveView(linearLayoutCompat);

                    fragmentManager
                    .BeginTransaction()
                    .Remove(viewFragment)
                    .Commit();

                    await linearLayoutCompat.OnUnloadedAsync();
                    if (viewFragment.View != null)
                    {
                        await viewFragment.View.OnUnloadedAsync();
                    }

                    // This is mainly to remove changes to the decor view that shell imposes
                    if (decorBackground != rootView.Background)
                    {
                        rootView.Background = decorBackground;
                    }

                    // Unset the Support Action bar if the calling code has set the support action bar
                    if (MauiContext.Context.GetActivity() is AppCompatActivity aca)
                    {
                        aca.SetSupportActionBar(null);
                    }
                }
            }));
        Task SetupWindowForTests <THandler>(IWindow window, Func <Task> runTests, IMauiContext mauiContext = null)
            where THandler : class, IElementHandler
        {
            mauiContext ??= MauiContext;
            return(InvokeOnMainThreadAsync(async() =>
            {
                AViewGroup rootView = MauiContext.Context.GetActivity().Window.DecorView as AViewGroup;
                _decorDrawable ??= rootView.Background;
                var linearLayoutCompat = new LinearLayoutCompat(MauiContext.Context);
                var fragmentManager = MauiContext.GetFragmentManager();
                var viewFragment = new WindowTestFragment(MauiContext, window);

                try
                {
                    linearLayoutCompat.Id = AView.GenerateViewId();
                    rootView.AddView(linearLayoutCompat);

                    fragmentManager
                    .BeginTransaction()
                    .Add(linearLayoutCompat.Id, viewFragment)
                    .Commit();

                    await viewFragment.FinishedLoading;
                    await runTests.Invoke();
                }
                finally
                {
                    if (window.Handler != null)
                    {
                        window.Handler.DisconnectHandler();
                    }

                    fragmentManager
                    .BeginTransaction()
                    .Remove(viewFragment)
                    .Commit();

                    rootView.RemoveView(linearLayoutCompat);

                    await linearLayoutCompat.OnUnloadedAsync();
                    if (viewFragment.View != null)
                    {
                        await viewFragment.View.OnUnloadedAsync();
                    }

                    await viewFragment.FinishedDestroying;

                    // This is mainly to remove changes to the decor view that shell imposes
                    if (_decorDrawable != rootView.Background)
                    {
                        rootView.Background = _decorDrawable;
                    }

                    // Unset the Support Action bar if the calling code has set the support action bar
                    if (MauiContext.Context.GetActivity() is AppCompatActivity aca)
                    {
                        aca.SetSupportActionBar(null);
                    }
                }
            }));
        }