maui-linux/System.Maui.Platform.UAP/ViewToRendererConverter.cs

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

2020-05-19 03:56:25 +03:00
using System;
using global::Windows.Foundation;
using global::Windows.UI.Xaml;
using global::Windows.UI.Xaml.Controls;
2016-03-22 23:02:25 +03:00
2020-05-19 03:56:25 +03:00
namespace System.Maui.Platform.UWP
2016-03-22 23:02:25 +03:00
{
2020-05-19 03:56:25 +03:00
public class ViewToRendererConverter : global::Windows.UI.Xaml.Data.IValueConverter
2016-03-22 23:02:25 +03:00
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var view = value as View;
if (view == null)
{
var page = value as Page;
if (page != null)
{
IVisualElementRenderer renderer = page.GetOrCreateRenderer();
return renderer.ContainerElement;
}
}
if (view == null)
return null;
return new WrapperControl(view);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotSupportedException();
}
2018-04-30 19:46:03 +03:00
internal class WrapperControl : Panel
2016-03-22 23:02:25 +03:00
{
readonly View _view;
FrameworkElement FrameworkElement { get; }
Adds UWP support to Shell (#6015) * Some basics to get started * more renderers * Fix titlebar color * More shell stuff working * Hacked some more UI in * Fix null ref issue * Move renderer registration outside common code (for now) * Re-write of the renderers to better use a cleaner UWP approach * Moved functionality around, bug fixesetc * Added null check * Added null-check on appearance and use default colors as fallback * Handle change in flyout behavior to correctly turn the flyout on/off * Handle the TabBarIsVisible property * code formatting * Ensure FlyoutHeader isn't show if the app starts up with a minimal pane * Throw if used on versions lower than Windows 10 1809 * Added null-check for when ShellContent isn't set * Support tabs in FlyoutItems with Display AsMultipleItems by using the generated FlyoutGroupings instead * Improve pane behavior and styling * Undo Android change used during testing * Fix platform support check * Use FileImageSourcePathConverter on NavigationViewItem instead of a custom control (so I deleted ShellNavigationViewItemRenderer which is no longer needed). Ensure `FileImageSourcePathConverter` won't throw if it didn't get a FileImageSource. Move the flyout data templates into a resource so they can be overridden and compiled. * Delete renamed file * Use a resource instead of parsing a string template * Handle search box property changes * Update page title on property change * Update bottombar when shellitems change * Guard against API usage not present * Platform check comments * Fix problem running in release mode (use Bindable to generate XamlMetadata * Trigger rebind of menu items source when collection changes * Added support for Toolbar * Fix searchbox behavior (still lacks expand/collapse feature) * Add overload for defining the navigation transition * Use different navigation transitions based on navigatin direction * Hides header with show / hide nav command * collapses header area on hide nav * Move to use WinUI * Fix runtime issues after merge. * - rebase fixes * - rebase fixes * - fix spaces/tabs * - flags, hide apis, delete assembly info * - set flag on UWP CG * - expose renderer creations and make them all public * - formatting fixes * - address PR comments * - fix header so it's full width and swappable
2019-09-18 01:26:56 +03:00
internal void CleanUp()
{
_view?.Cleanup();
if(_view != null)
_view.MeasureInvalidated -= OnMeasureInvalidated;
}
2018-04-30 19:46:03 +03:00
2016-03-22 23:02:25 +03:00
public WrapperControl(View view)
{
_view = view;
Adds UWP support to Shell (#6015) * Some basics to get started * more renderers * Fix titlebar color * More shell stuff working * Hacked some more UI in * Fix null ref issue * Move renderer registration outside common code (for now) * Re-write of the renderers to better use a cleaner UWP approach * Moved functionality around, bug fixesetc * Added null check * Added null-check on appearance and use default colors as fallback * Handle change in flyout behavior to correctly turn the flyout on/off * Handle the TabBarIsVisible property * code formatting * Ensure FlyoutHeader isn't show if the app starts up with a minimal pane * Throw if used on versions lower than Windows 10 1809 * Added null-check for when ShellContent isn't set * Support tabs in FlyoutItems with Display AsMultipleItems by using the generated FlyoutGroupings instead * Improve pane behavior and styling * Undo Android change used during testing * Fix platform support check * Use FileImageSourcePathConverter on NavigationViewItem instead of a custom control (so I deleted ShellNavigationViewItemRenderer which is no longer needed). Ensure `FileImageSourcePathConverter` won't throw if it didn't get a FileImageSource. Move the flyout data templates into a resource so they can be overridden and compiled. * Delete renamed file * Use a resource instead of parsing a string template * Handle search box property changes * Update page title on property change * Update bottombar when shellitems change * Guard against API usage not present * Platform check comments * Fix problem running in release mode (use Bindable to generate XamlMetadata * Trigger rebind of menu items source when collection changes * Added support for Toolbar * Fix searchbox behavior (still lacks expand/collapse feature) * Add overload for defining the navigation transition * Use different navigation transitions based on navigatin direction * Hides header with show / hide nav command * collapses header area on hide nav * Move to use WinUI * Fix runtime issues after merge. * - rebase fixes * - rebase fixes * - fix spaces/tabs * - flags, hide apis, delete assembly info * - set flag on UWP CG * - expose renderer creations and make them all public * - formatting fixes * - address PR comments * - fix header so it's full width and swappable
2019-09-18 01:26:56 +03:00
_view.MeasureInvalidated += OnMeasureInvalidated;
2016-03-22 23:02:25 +03:00
IVisualElementRenderer renderer = Platform.CreateRenderer(view);
Platform.SetRenderer(view, renderer);
FrameworkElement = renderer.ContainerElement;
Children.Add(renderer.ContainerElement);
2016-03-22 23:02:25 +03:00
// make sure we re-measure once the template is applied
if (FrameworkElement != null)
{
FrameworkElement.Loaded += (sender, args) =>
{
// If the view is a layout (stacklayout, grid, etc) we need to trigger a layout pass
// with all the controls in a consistent native state (i.e., loaded) so they'll actually
// have Bounds set
(_view as Layout)?.ForceLayout();
InvalidateMeasure();
};
}
2016-03-22 23:02:25 +03:00
}
Adds UWP support to Shell (#6015) * Some basics to get started * more renderers * Fix titlebar color * More shell stuff working * Hacked some more UI in * Fix null ref issue * Move renderer registration outside common code (for now) * Re-write of the renderers to better use a cleaner UWP approach * Moved functionality around, bug fixesetc * Added null check * Added null-check on appearance and use default colors as fallback * Handle change in flyout behavior to correctly turn the flyout on/off * Handle the TabBarIsVisible property * code formatting * Ensure FlyoutHeader isn't show if the app starts up with a minimal pane * Throw if used on versions lower than Windows 10 1809 * Added null-check for when ShellContent isn't set * Support tabs in FlyoutItems with Display AsMultipleItems by using the generated FlyoutGroupings instead * Improve pane behavior and styling * Undo Android change used during testing * Fix platform support check * Use FileImageSourcePathConverter on NavigationViewItem instead of a custom control (so I deleted ShellNavigationViewItemRenderer which is no longer needed). Ensure `FileImageSourcePathConverter` won't throw if it didn't get a FileImageSource. Move the flyout data templates into a resource so they can be overridden and compiled. * Delete renamed file * Use a resource instead of parsing a string template * Handle search box property changes * Update page title on property change * Update bottombar when shellitems change * Guard against API usage not present * Platform check comments * Fix problem running in release mode (use Bindable to generate XamlMetadata * Trigger rebind of menu items source when collection changes * Added support for Toolbar * Fix searchbox behavior (still lacks expand/collapse feature) * Add overload for defining the navigation transition * Use different navigation transitions based on navigatin direction * Hides header with show / hide nav command * collapses header area on hide nav * Move to use WinUI * Fix runtime issues after merge. * - rebase fixes * - rebase fixes * - fix spaces/tabs * - flags, hide apis, delete assembly info * - set flag on UWP CG * - expose renderer creations and make them all public * - formatting fixes * - address PR comments * - fix header so it's full width and swappable
2019-09-18 01:26:56 +03:00
void OnMeasureInvalidated(object sender, EventArgs e)
{
InvalidateMeasure();
}
2020-05-19 03:56:25 +03:00
protected override global::Windows.Foundation.Size ArrangeOverride(global::Windows.Foundation.Size finalSize)
2016-03-22 23:02:25 +03:00
{
_view.IsInNativeLayout = true;
Layout.LayoutChildIntoBoundingRegion(_view, new Rectangle(0, 0, finalSize.Width, finalSize.Height));
if (_view.Width <= 0 || _view.Height <= 0)
{
// Hide Panel when size _view is empty.
// It is necessary that this element does not overlap other elements when it should be hidden.
Opacity = 0;
}
else
{
Opacity = 1;
FrameworkElement?.Arrange(new Rect(_view.X, _view.Y, _view.Width, _view.Height));
}
2016-03-22 23:02:25 +03:00
_view.IsInNativeLayout = false;
return finalSize;
}
2020-05-19 03:56:25 +03:00
protected override global::Windows.Foundation.Size MeasureOverride(global::Windows.Foundation.Size availableSize)
2016-03-22 23:02:25 +03:00
{
Size request = _view.Measure(availableSize.Width, availableSize.Height, MeasureFlags.IncludeMargins).Request;
if (request.Height < 0)
{
request.Height = availableSize.Height;
}
2020-05-19 03:56:25 +03:00
global::Windows.Foundation.Size result;
2016-03-22 23:02:25 +03:00
if (_view.HorizontalOptions.Alignment == LayoutAlignment.Fill && !double.IsInfinity(availableSize.Width) && availableSize.Width != 0)
{
2020-05-19 03:56:25 +03:00
result = new global::Windows.Foundation.Size(availableSize.Width, request.Height);
2016-03-22 23:02:25 +03:00
}
else
{
2020-05-19 03:56:25 +03:00
result = new global::Windows.Foundation.Size(request.Width, request.Height);
2016-03-22 23:02:25 +03:00
}
Layout.LayoutChildIntoBoundingRegion(_view, new Rectangle(0, 0, result.Width, result.Height));
FrameworkElement?.Measure(availableSize);
2018-04-30 19:46:03 +03:00
2016-03-22 23:02:25 +03:00
return result;
}
}
}
}