maui-linux/Xamarin.Forms.Platform.UAP/PageExtensions.cs

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

[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
using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
namespace Xamarin.Forms.Platform.UWP
{
public sealed partial class FormsEmbeddedPageWrapper : Windows.UI.Xaml.Controls.Page
{
internal static Dictionary<Guid, ContentPage> Pages = new Dictionary<Guid, ContentPage>();
public FormsEmbeddedPageWrapper()
{
InitializeComponent();
}
protected override void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.Parameter == null)
{
throw new InvalidOperationException($"Cannot navigate to {nameof(FormsEmbeddedPageWrapper)} without "
+ $"providing a {nameof(Xamarin.Forms.Page)} identifier.");
}
// Find the page instance in the dictionary and then discard it so we don't prevent it from being collected
var key = (Guid)e.Parameter;
var page = Pages[key];
Pages.Remove(key);
// Convert that page into a FrameWorkElement we can display in the ContentPresenter
FrameworkElement frameworkElement = page.CreateFrameworkElement();
if (frameworkElement == null)
{
throw new InvalidOperationException($"Could not find or create a renderer for the Page {page}");
}
Root.Content = frameworkElement;
}
}
public static class PageExtensions
{
public static FrameworkElement CreateFrameworkElement(this ContentPage contentPage)
{
return contentPage.ToFrameworkElement();
}
internal static FrameworkElement ToFrameworkElement(this VisualElement visualElement)
{
if (!Forms.IsInitialized)
{
throw new InvalidOperationException("call Forms.Init() before this");
}
var root = new Windows.UI.Xaml.Controls.Page();
new WindowsPlatform(root).SetPlatformDisconnected(visualElement);
var renderer = visualElement.GetOrCreateRenderer();
if (renderer == null)
{
throw new InvalidOperationException($"Could not find or create a renderer for {visualElement}");
}
var frameworkElement = renderer.ContainerElement;
frameworkElement.Loaded += (sender, args) =>
{
visualElement.Layout(new Rectangle(0, 0, frameworkElement.ActualWidth, frameworkElement.ActualHeight));
};
return frameworkElement;
}
public static bool Navigate(this Windows.UI.Xaml.Controls.Frame frame, ContentPage page)
{
if (page == null)
{
throw new ArgumentNullException(nameof(page));
}
Guid id = Guid.NewGuid();
FormsEmbeddedPageWrapper.Pages.Add(id, page);
return frame.Navigate(typeof(FormsEmbeddedPageWrapper), id);
}
}
}