maui-linux/Xamarin.Forms.Platform.Android/AppCompat/FragmentContainer.cs

137 строки
3.5 KiB
C#
Исходник Обычный вид История

2016-03-22 23:02:25 +03:00
using System;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific.AppCompat;
2016-03-22 23:02:25 +03:00
using AView = Android.Views.View;
using Fragment = Android.Support.V4.App.Fragment;
2016-03-22 23:02:25 +03:00
namespace Xamarin.Forms.Platform.Android.AppCompat
{
internal class FragmentContainer : Fragment
{
readonly WeakReference _pageReference;
Action<PageContainer> _onCreateCallback;
2016-03-22 23:02:25 +03:00
PageContainer _pageContainer;
IVisualElementRenderer _visualElementRenderer;
public FragmentContainer()
{
}
public FragmentContainer(Page page) : this()
{
_pageReference = new WeakReference(page);
}
protected FragmentContainer(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public Page Page => (Page)_pageReference?.Target;
IPageController PageController => Page as IPageController;
2016-03-22 23:02:25 +03:00
public static Fragment CreateInstance(Page page)
{
return new FragmentContainer(page) { Arguments = new Bundle() };
}
public void SetOnCreateCallback(Action<PageContainer> callback)
{
_onCreateCallback = callback;
}
2016-03-22 23:02:25 +03:00
public override AView OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (Page != null)
{
[All] Page embedding (#1124) * Fix broken SetTextAppearance call when targeting below API 23 * Enable embedding XF apps in Android * VS didn't save the project file * Allow retrieving rendered XF VisualElements for use in UWP apps * Prevent XF from slaughtering the menu bar in embedded scenarios * ? * Post-rebase fixups * Create embedding test project for Android * Create iOS test bed project * Attempting to get functional embedded project * Android and iOS functional testbed projects * Basic embedding app on UWP * First pass at handling navigation to Forms page from UWP * Fleshed out example, added parameter checks, sealed wrapper class * Create test page and load it from UWP * Make DisplayAlert/DisplayActionSheet independent of Platform instance on UWP * Set up test page for alerts/actionsheets on Android * Move DisplayAlert/ActionSheet code to a helper class so embedded Forms can use it * VS didn't save my changes * Add test page for alerts and action sheets * Get FrameworkElement from renderer * Page -> ContentPage * Can now display the webview embedded page * Example working on iOS * Repro of crash * Remove the IStartActivityForResult dependency from FormsWebChromeClient * Created repro * Remove need for Forms.Context to create renderers * Remove Forms.Context dependencies in ScrollViewRenderer * Remove Forms.Context dependencies in FrameRenderer * Remove Forms.Context dependencies * Remove Context/Forms.Context dependencies in Drawable subclasses * Remove some more Forms.Context dependencies * CellAdapter no longer dependent on Forms.Context * Obsolete ToAndroid using Forms.Context * Remove Forms.Context dependencies in ResourceManager * Remove need for Forms.Context for SupportsProgress * Remove Forms.Context dependency for setting titlebar visibility * Remove Forms.Context dependencies in GetAccentColor * Some comments about caching * Remove Forms.Context dependencies for AndroidPlatformServices and ResourceProvider * Remove Forms.Context dependencies in Maps * Disabled warnings for Forms.Context in CustomRenderers * Filter logs so we don't get those annoying "parked" messages * Filter alerts/actionsheets/activityindicator by context * Clean up constructor debugging messages * Remove old TODOs * Set up test page for Device.Openuri * Make AndroidDeviceInfo more resilient to multiple activity scenarios * Use parameter instead of member * Add missing parameterless constructors for embedded fragment wrappers * Fix multiple popup subscriptions for embedded context * Remove UpdateGlobalContext and Page Context attached property * Obsolete static reference to Context * Warnings as errors in embedded test bed projects * Comment cleanup * Clean up unused code * Obsolete old constructor for DefaultRenderer * Make sure embedded fragment wrappers handle disposing the platform * Revert to old DependencyService registration for ResourceProvider; use ApplicationContext for ResourceProvider; Comments for everything happing in Forms.SetupInit * Remove old TODO * Make PopupManager take the correct type instead of casting a bunch * Update docs * Add missing nuspec entry for FormsEmbeddedPageWrapper * Post-rebase cleanup * Update docs * Disable XF target validation so package restore works * Restore InputTransparent handling lost in rebase * Restore parameter lost during rebase * Finalize the list of subscriptions to avoid 'modified collection' errors * Avoid double-fetching Context * Fix "with you package" typo
2017-10-09 20:51:55 +03:00
_visualElementRenderer = Android.Platform.CreateRenderer(Page, ChildFragmentManager, inflater.Context);
2016-03-22 23:02:25 +03:00
Android.Platform.SetRenderer(Page, _visualElementRenderer);
[All] Page embedding (#1124) * Fix broken SetTextAppearance call when targeting below API 23 * Enable embedding XF apps in Android * VS didn't save the project file * Allow retrieving rendered XF VisualElements for use in UWP apps * Prevent XF from slaughtering the menu bar in embedded scenarios * ? * Post-rebase fixups * Create embedding test project for Android * Create iOS test bed project * Attempting to get functional embedded project * Android and iOS functional testbed projects * Basic embedding app on UWP * First pass at handling navigation to Forms page from UWP * Fleshed out example, added parameter checks, sealed wrapper class * Create test page and load it from UWP * Make DisplayAlert/DisplayActionSheet independent of Platform instance on UWP * Set up test page for alerts/actionsheets on Android * Move DisplayAlert/ActionSheet code to a helper class so embedded Forms can use it * VS didn't save my changes * Add test page for alerts and action sheets * Get FrameworkElement from renderer * Page -> ContentPage * Can now display the webview embedded page * Example working on iOS * Repro of crash * Remove the IStartActivityForResult dependency from FormsWebChromeClient * Created repro * Remove need for Forms.Context to create renderers * Remove Forms.Context dependencies in ScrollViewRenderer * Remove Forms.Context dependencies in FrameRenderer * Remove Forms.Context dependencies * Remove Context/Forms.Context dependencies in Drawable subclasses * Remove some more Forms.Context dependencies * CellAdapter no longer dependent on Forms.Context * Obsolete ToAndroid using Forms.Context * Remove Forms.Context dependencies in ResourceManager * Remove need for Forms.Context for SupportsProgress * Remove Forms.Context dependency for setting titlebar visibility * Remove Forms.Context dependencies in GetAccentColor * Some comments about caching * Remove Forms.Context dependencies for AndroidPlatformServices and ResourceProvider * Remove Forms.Context dependencies in Maps * Disabled warnings for Forms.Context in CustomRenderers * Filter logs so we don't get those annoying "parked" messages * Filter alerts/actionsheets/activityindicator by context * Clean up constructor debugging messages * Remove old TODOs * Set up test page for Device.Openuri * Make AndroidDeviceInfo more resilient to multiple activity scenarios * Use parameter instead of member * Add missing parameterless constructors for embedded fragment wrappers * Fix multiple popup subscriptions for embedded context * Remove UpdateGlobalContext and Page Context attached property * Obsolete static reference to Context * Warnings as errors in embedded test bed projects * Comment cleanup * Clean up unused code * Obsolete old constructor for DefaultRenderer * Make sure embedded fragment wrappers handle disposing the platform * Revert to old DependencyService registration for ResourceProvider; use ApplicationContext for ResourceProvider; Comments for everything happing in Forms.SetupInit * Remove old TODO * Make PopupManager take the correct type instead of casting a bunch * Update docs * Add missing nuspec entry for FormsEmbeddedPageWrapper * Post-rebase cleanup * Update docs * Disable XF target validation so package restore works * Restore InputTransparent handling lost in rebase * Restore parameter lost during rebase * Finalize the list of subscriptions to avoid 'modified collection' errors * Avoid double-fetching Context * Fix "with you package" typo
2017-10-09 20:51:55 +03:00
_pageContainer = new PageContainer(inflater.Context, _visualElementRenderer, true);
_onCreateCallback?.Invoke(_pageContainer);
2016-03-22 23:02:25 +03:00
return _pageContainer;
}
return null;
}
2016-03-22 23:02:25 +03:00
public override void OnDestroyView()
{
if (Page != null)
{
if (_visualElementRenderer != null)
2016-03-22 23:02:25 +03:00
{
Android fastrenderers (#845) * Obsolete IVisualElementRenderer.ViewGroup in favor of .View * Fix NRE * Changing TContainer in PlatformEffect to View * Fix "View" type * new VisualElementRenderer * First attempt at a fast(er) button renderer * Fast Label Renderer * Let's try that again. Behold: Label Fast Renderer * Move FrameRenderer into Fast Renderers * Fix Disposable on VisualElementRenderer * Simplify touch and click handlers * Drop empty if clause * [Android] Add initial Image fast renderer * Split accessibility out to a separate helper class; fix tapgesture bug with label * [Android] Small fixes to VisualElementRenderer * Move accessiblity stuff to a separate class (which needs a good name) * Prevent query from looking to parent for fast renderers * [Android] ImageRenderer refactoring * Fix elevation/z-index bugs with Button (e.g., 40173) * Move SetLabeledBy to Accessibilitizer * Un-break automation IDs for Labels * Move gesture handling to its own class * Split gesture and effect management into separate classes * Remove unneeded packager from LabelRenderer * LabelRenderer inherits from FormsTextView * Batch updates to View * Fix isOnParentRenderer check for non-Android platforms * [Controls] Update Xamarin.Forms.ControlGallery.iOS.csproj * [Android,IOS] Small fixes to rebase and use of Internals * [Android] Ignroe warning for now * Fast renderers now passing InputTransparent and IsEnabled tests * Fast and legacy renderers now pass the Enabled and InputTransparent tests * Change PlatformEffect back, default container to null * Fix mangled using directives
2017-04-06 16:19:52 +03:00
if (_visualElementRenderer.View.Handle != IntPtr.Zero)
{
Android fastrenderers (#845) * Obsolete IVisualElementRenderer.ViewGroup in favor of .View * Fix NRE * Changing TContainer in PlatformEffect to View * Fix "View" type * new VisualElementRenderer * First attempt at a fast(er) button renderer * Fast Label Renderer * Let's try that again. Behold: Label Fast Renderer * Move FrameRenderer into Fast Renderers * Fix Disposable on VisualElementRenderer * Simplify touch and click handlers * Drop empty if clause * [Android] Add initial Image fast renderer * Split accessibility out to a separate helper class; fix tapgesture bug with label * [Android] Small fixes to VisualElementRenderer * Move accessiblity stuff to a separate class (which needs a good name) * Prevent query from looking to parent for fast renderers * [Android] ImageRenderer refactoring * Fix elevation/z-index bugs with Button (e.g., 40173) * Move SetLabeledBy to Accessibilitizer * Un-break automation IDs for Labels * Move gesture handling to its own class * Split gesture and effect management into separate classes * Remove unneeded packager from LabelRenderer * LabelRenderer inherits from FormsTextView * Batch updates to View * Fix isOnParentRenderer check for non-Android platforms * [Controls] Update Xamarin.Forms.ControlGallery.iOS.csproj * [Android,IOS] Small fixes to rebase and use of Internals * [Android] Ignroe warning for now * Fast renderers now passing InputTransparent and IsEnabled tests * Fast and legacy renderers now pass the Enabled and InputTransparent tests * Change PlatformEffect back, default container to null * Fix mangled using directives
2017-04-06 16:19:52 +03:00
_visualElementRenderer.View.RemoveFromParent();
}
2016-03-22 23:02:25 +03:00
_visualElementRenderer.Dispose();
2016-03-22 23:02:25 +03:00
}
// We do *not* eagerly dispose of the _pageContainer here; doing so causes a memory leak
// if animated fragment transitions are enabled (it removes some info that the animation's
// onAnimationEnd handler requires to properly clean things up)
// Instead, we let the garbage collector pick it up later, when we can be sure it's safe
Page?.ClearValue(Android.Platform.RendererProperty);
2016-03-22 23:02:25 +03:00
}
_onCreateCallback = null;
2016-03-22 23:02:25 +03:00
_visualElementRenderer = null;
base.OnDestroyView();
}
public override void OnHiddenChanged(bool hidden)
{
base.OnHiddenChanged(hidden);
if (Page == null)
return;
if (hidden)
PageController?.SendDisappearing();
2016-03-22 23:02:25 +03:00
else
PageController?.SendAppearing();
2016-03-22 23:02:25 +03:00
}
public override void OnPause()
{
bool shouldSendEvent = Application.Current.OnThisPlatform().GetSendDisappearingEventOnPause();
if (shouldSendEvent)
SendLifecycleEvent(false);
2016-03-22 23:02:25 +03:00
base.OnPause();
}
public override void OnResume()
{
bool shouldSendEvent = Application.Current.OnThisPlatform().GetSendAppearingEventOnResume();
if (shouldSendEvent)
SendLifecycleEvent(true);
base.OnResume();
}
void SendLifecycleEvent(bool isAppearing)
{
var masterDetailPage = Application.Current.MainPage as MasterDetailPage;
var pageContainer = (masterDetailPage != null ? masterDetailPage.Detail : Application.Current.MainPage) as IPageContainer<Page>;
Page currentPage = pageContainer?.CurrentPage;
if(!(currentPage == null || currentPage == PageController))
return;
if (isAppearing && UserVisibleHint)
PageController?.SendAppearing();
else if(!isAppearing)
PageController?.SendDisappearing();
}
2016-03-22 23:02:25 +03:00
}
}