maui-linux/Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs

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

Add pressed and released events to Button (#446) * Add pressed and released events to Button * Update ButtonRenderer.cs * Apply safely casting to android button renderer * Use safety casting for Android buttin renderer * [Windows] Fix modal pages being laid out below soft buttons (#395) * Add sample HanselForms and TwitterDemo to ControlGallery (#651) * [Controls] Add Hanselforms sample * Remove extra twitter sample * [Controls]Add TwitterDemo sample * [Controls] Fix build * Slider should show user-set value on initial load (#378) * [UWP] Use toolbar foreground color on primary items (#640) * Avoid duplicating code in OmPlatform (#591) * [iOS] Entry should not pass a newline to the next responder (#397) * UITextField should not return so that the next field does not get passed a newline * Added code sample * [XamlC] import members on x:Static and factories (#642) * [Xaml] support short Properties for PropertyCondition (#645) * Xamlc compile data triggers (#648) * [Xaml] DataTrigger and PropertyCondition no longer use a ServiceProvider * [XamlC] avoid generating ServiceProvider for unused ProvideValue * fix tests * Fix comment typo * [UWP] Fix TextBox style for foreground focus color (#618) * Adding image to use for CellsGalleryImageUrlCellList UI test * Update ImageCellListPage to use an image we control; Update CellsGalleryImageUrlCellList test to wait longer than 1s for images to load if necessary * fix nre when changing content in datepickerselected (#494) * Make CellsGalleryImageUrlCellList test finish early if possible * [iOS] Change keyboard type while keyboard is visible (#443) * Change keyboard while changing text * add sample code * [Android] Fix NavigationPage dispose crash when it parents a MasterDetailPage (#577) * fix navigation page dispose crash * changes after review * [XamlC] detect duplicate x:Name at compile time (#655) * [XamlC] detect duplicate x:Name at compile time * invoking methods with the right arguments produces better results * Make UWP toolbar display rules consistent with other platforms (#638) * Allow subscriber to be collected if MessagingCenter is the only reference to it (#617) * Repro * Make messaging center callbacks weak references * Preserve attribute * Fix test method name * Watch for collection of actual delegate target instead of wrapper delegate * Preserve the original platform instance when changing main page * Better tests for lambda situations * Update tests, make callback target a weakreference if it's the subscriber * Ensure old Platform MessagingCenter subs are gone before creating new Platform * [iOS] Prevent multiple ListView cells from being swiped simultaneously (#578) * disable multiple cell swipe * add sample code * refactored * convert to weakreference * remove null setting * change weakreference setting place * remove if * revert isopen changes * add instructions * [WinRT/UWP] Apply BackgroundColor to Stepper buttons (#581) * [WinRT/UWP] Apply BackgroundColor to Stepper buttons * Add explanatory text; use nameof * Move explanatory text to a label * Return group instead of internal class (#461) * [iOS/Android] Move Map camera to correct region on layout change (#548) * Move to region on layout change * remove visibility check * [iOS] Platform specifics for controlling Picker SelectedIndex change behavior (#540) * picker selected index could change when picker view is dismissed * use enum * [iOS] Ignore intermittent failing test on XTC (#666) * [UITest] Update to UITest 2.0.5 (#665) * Rebase the current branch onto upstream latest
2017-02-02 21:23:31 +03:00
using System;
2016-03-22 23:02:25 +03:00
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Xamarin.Forms.Internals;
2016-03-22 23:02:25 +03:00
using WThickness = Windows.UI.Xaml.Thickness;
using WButton = Windows.UI.Xaml.Controls.Button;
using WImage = Windows.UI.Xaml.Controls.Image;
Add pressed and released events to Button (#446) * Add pressed and released events to Button * Update ButtonRenderer.cs * Apply safely casting to android button renderer * Use safety casting for Android buttin renderer * [Windows] Fix modal pages being laid out below soft buttons (#395) * Add sample HanselForms and TwitterDemo to ControlGallery (#651) * [Controls] Add Hanselforms sample * Remove extra twitter sample * [Controls]Add TwitterDemo sample * [Controls] Fix build * Slider should show user-set value on initial load (#378) * [UWP] Use toolbar foreground color on primary items (#640) * Avoid duplicating code in OmPlatform (#591) * [iOS] Entry should not pass a newline to the next responder (#397) * UITextField should not return so that the next field does not get passed a newline * Added code sample * [XamlC] import members on x:Static and factories (#642) * [Xaml] support short Properties for PropertyCondition (#645) * Xamlc compile data triggers (#648) * [Xaml] DataTrigger and PropertyCondition no longer use a ServiceProvider * [XamlC] avoid generating ServiceProvider for unused ProvideValue * fix tests * Fix comment typo * [UWP] Fix TextBox style for foreground focus color (#618) * Adding image to use for CellsGalleryImageUrlCellList UI test * Update ImageCellListPage to use an image we control; Update CellsGalleryImageUrlCellList test to wait longer than 1s for images to load if necessary * fix nre when changing content in datepickerselected (#494) * Make CellsGalleryImageUrlCellList test finish early if possible * [iOS] Change keyboard type while keyboard is visible (#443) * Change keyboard while changing text * add sample code * [Android] Fix NavigationPage dispose crash when it parents a MasterDetailPage (#577) * fix navigation page dispose crash * changes after review * [XamlC] detect duplicate x:Name at compile time (#655) * [XamlC] detect duplicate x:Name at compile time * invoking methods with the right arguments produces better results * Make UWP toolbar display rules consistent with other platforms (#638) * Allow subscriber to be collected if MessagingCenter is the only reference to it (#617) * Repro * Make messaging center callbacks weak references * Preserve attribute * Fix test method name * Watch for collection of actual delegate target instead of wrapper delegate * Preserve the original platform instance when changing main page * Better tests for lambda situations * Update tests, make callback target a weakreference if it's the subscriber * Ensure old Platform MessagingCenter subs are gone before creating new Platform * [iOS] Prevent multiple ListView cells from being swiped simultaneously (#578) * disable multiple cell swipe * add sample code * refactored * convert to weakreference * remove null setting * change weakreference setting place * remove if * revert isopen changes * add instructions * [WinRT/UWP] Apply BackgroundColor to Stepper buttons (#581) * [WinRT/UWP] Apply BackgroundColor to Stepper buttons * Add explanatory text; use nameof * Move explanatory text to a label * Return group instead of internal class (#461) * [iOS/Android] Move Map camera to correct region on layout change (#548) * Move to region on layout change * remove visibility check * [iOS] Platform specifics for controlling Picker SelectedIndex change behavior (#540) * picker selected index could change when picker view is dismissed * use enum * [iOS] Ignore intermittent failing test on XTC (#666) * [UITest] Update to UITest 2.0.5 (#665) * Rebase the current branch onto upstream latest
2017-02-02 21:23:31 +03:00
using Windows.UI.Xaml.Input;
2016-03-22 23:02:25 +03:00
#if WINDOWS_UWP
namespace Xamarin.Forms.Platform.UWP
#else
namespace Xamarin.Forms.Platform.WinRT
#endif
{
public class ButtonRenderer : ViewRenderer<Button, FormsButton>
{
bool _fontApplied;
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control == null)
{
var button = new FormsButton();
button.Click += OnButtonClick;
Add pressed and released events to Button (#446) * Add pressed and released events to Button * Update ButtonRenderer.cs * Apply safely casting to android button renderer * Use safety casting for Android buttin renderer * [Windows] Fix modal pages being laid out below soft buttons (#395) * Add sample HanselForms and TwitterDemo to ControlGallery (#651) * [Controls] Add Hanselforms sample * Remove extra twitter sample * [Controls]Add TwitterDemo sample * [Controls] Fix build * Slider should show user-set value on initial load (#378) * [UWP] Use toolbar foreground color on primary items (#640) * Avoid duplicating code in OmPlatform (#591) * [iOS] Entry should not pass a newline to the next responder (#397) * UITextField should not return so that the next field does not get passed a newline * Added code sample * [XamlC] import members on x:Static and factories (#642) * [Xaml] support short Properties for PropertyCondition (#645) * Xamlc compile data triggers (#648) * [Xaml] DataTrigger and PropertyCondition no longer use a ServiceProvider * [XamlC] avoid generating ServiceProvider for unused ProvideValue * fix tests * Fix comment typo * [UWP] Fix TextBox style for foreground focus color (#618) * Adding image to use for CellsGalleryImageUrlCellList UI test * Update ImageCellListPage to use an image we control; Update CellsGalleryImageUrlCellList test to wait longer than 1s for images to load if necessary * fix nre when changing content in datepickerselected (#494) * Make CellsGalleryImageUrlCellList test finish early if possible * [iOS] Change keyboard type while keyboard is visible (#443) * Change keyboard while changing text * add sample code * [Android] Fix NavigationPage dispose crash when it parents a MasterDetailPage (#577) * fix navigation page dispose crash * changes after review * [XamlC] detect duplicate x:Name at compile time (#655) * [XamlC] detect duplicate x:Name at compile time * invoking methods with the right arguments produces better results * Make UWP toolbar display rules consistent with other platforms (#638) * Allow subscriber to be collected if MessagingCenter is the only reference to it (#617) * Repro * Make messaging center callbacks weak references * Preserve attribute * Fix test method name * Watch for collection of actual delegate target instead of wrapper delegate * Preserve the original platform instance when changing main page * Better tests for lambda situations * Update tests, make callback target a weakreference if it's the subscriber * Ensure old Platform MessagingCenter subs are gone before creating new Platform * [iOS] Prevent multiple ListView cells from being swiped simultaneously (#578) * disable multiple cell swipe * add sample code * refactored * convert to weakreference * remove null setting * change weakreference setting place * remove if * revert isopen changes * add instructions * [WinRT/UWP] Apply BackgroundColor to Stepper buttons (#581) * [WinRT/UWP] Apply BackgroundColor to Stepper buttons * Add explanatory text; use nameof * Move explanatory text to a label * Return group instead of internal class (#461) * [iOS/Android] Move Map camera to correct region on layout change (#548) * Move to region on layout change * remove visibility check * [iOS] Platform specifics for controlling Picker SelectedIndex change behavior (#540) * picker selected index could change when picker view is dismissed * use enum * [iOS] Ignore intermittent failing test on XTC (#666) * [UITest] Update to UITest 2.0.5 (#665) * Rebase the current branch onto upstream latest
2017-02-02 21:23:31 +03:00
button.AddHandler(PointerPressedEvent, new PointerEventHandler(OnPointerPressed), true);
2016-03-22 23:02:25 +03:00
SetNativeControl(button);
}
UpdateContent();
if (Element.BackgroundColor != Color.Default)
UpdateBackground();
if (Element.TextColor != Color.Default)
UpdateTextColor();
if (Element.BorderColor != Color.Default)
UpdateBorderColor();
if (Element.BorderWidth != (double)Button.BorderWidthProperty.DefaultValue)
2016-03-22 23:02:25 +03:00
UpdateBorderWidth();
if (Element.BorderRadius != (int)Button.BorderRadiusProperty.DefaultValue)
UpdateBorderRadius();
UpdateFont();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Button.TextProperty.PropertyName || e.PropertyName == Button.ImageProperty.PropertyName)
{
UpdateContent();
}
else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
{
UpdateBackground();
}
else if (e.PropertyName == Button.TextColorProperty.PropertyName)
{
UpdateTextColor();
}
else if (e.PropertyName == Button.FontProperty.PropertyName)
{
UpdateFont();
}
else if (e.PropertyName == Button.BorderColorProperty.PropertyName)
{
UpdateBorderColor();
}
else if (e.PropertyName == Button.BorderWidthProperty.PropertyName)
{
UpdateBorderWidth();
}
else if (e.PropertyName == Button.BorderRadiusProperty.PropertyName)
{
UpdateBorderRadius();
}
}
protected override void UpdateBackgroundColor()
{
// Button is a special case; we don't want to set the Control's background
// because it goes outside the bounds of the Border/ContentPresenter,
// which is where we might change the BorderRadius to create a rounded shape.
return;
}
protected override bool PreventGestureBubbling { get; set; } = true;
2016-03-22 23:02:25 +03:00
void OnButtonClick(object sender, RoutedEventArgs e)
{
Add pressed and released events to Button (#446) * Add pressed and released events to Button * Update ButtonRenderer.cs * Apply safely casting to android button renderer * Use safety casting for Android buttin renderer * [Windows] Fix modal pages being laid out below soft buttons (#395) * Add sample HanselForms and TwitterDemo to ControlGallery (#651) * [Controls] Add Hanselforms sample * Remove extra twitter sample * [Controls]Add TwitterDemo sample * [Controls] Fix build * Slider should show user-set value on initial load (#378) * [UWP] Use toolbar foreground color on primary items (#640) * Avoid duplicating code in OmPlatform (#591) * [iOS] Entry should not pass a newline to the next responder (#397) * UITextField should not return so that the next field does not get passed a newline * Added code sample * [XamlC] import members on x:Static and factories (#642) * [Xaml] support short Properties for PropertyCondition (#645) * Xamlc compile data triggers (#648) * [Xaml] DataTrigger and PropertyCondition no longer use a ServiceProvider * [XamlC] avoid generating ServiceProvider for unused ProvideValue * fix tests * Fix comment typo * [UWP] Fix TextBox style for foreground focus color (#618) * Adding image to use for CellsGalleryImageUrlCellList UI test * Update ImageCellListPage to use an image we control; Update CellsGalleryImageUrlCellList test to wait longer than 1s for images to load if necessary * fix nre when changing content in datepickerselected (#494) * Make CellsGalleryImageUrlCellList test finish early if possible * [iOS] Change keyboard type while keyboard is visible (#443) * Change keyboard while changing text * add sample code * [Android] Fix NavigationPage dispose crash when it parents a MasterDetailPage (#577) * fix navigation page dispose crash * changes after review * [XamlC] detect duplicate x:Name at compile time (#655) * [XamlC] detect duplicate x:Name at compile time * invoking methods with the right arguments produces better results * Make UWP toolbar display rules consistent with other platforms (#638) * Allow subscriber to be collected if MessagingCenter is the only reference to it (#617) * Repro * Make messaging center callbacks weak references * Preserve attribute * Fix test method name * Watch for collection of actual delegate target instead of wrapper delegate * Preserve the original platform instance when changing main page * Better tests for lambda situations * Update tests, make callback target a weakreference if it's the subscriber * Ensure old Platform MessagingCenter subs are gone before creating new Platform * [iOS] Prevent multiple ListView cells from being swiped simultaneously (#578) * disable multiple cell swipe * add sample code * refactored * convert to weakreference * remove null setting * change weakreference setting place * remove if * revert isopen changes * add instructions * [WinRT/UWP] Apply BackgroundColor to Stepper buttons (#581) * [WinRT/UWP] Apply BackgroundColor to Stepper buttons * Add explanatory text; use nameof * Move explanatory text to a label * Return group instead of internal class (#461) * [iOS/Android] Move Map camera to correct region on layout change (#548) * Move to region on layout change * remove visibility check * [iOS] Platform specifics for controlling Picker SelectedIndex change behavior (#540) * picker selected index could change when picker view is dismissed * use enum * [iOS] Ignore intermittent failing test on XTC (#666) * [UITest] Update to UITest 2.0.5 (#665) * Rebase the current branch onto upstream latest
2017-02-02 21:23:31 +03:00
((IButtonController)Element)?.SendReleased();
((IButtonController)Element)?.SendClicked();
2016-03-22 23:02:25 +03:00
}
Add pressed and released events to Button (#446) * Add pressed and released events to Button * Update ButtonRenderer.cs * Apply safely casting to android button renderer * Use safety casting for Android buttin renderer * [Windows] Fix modal pages being laid out below soft buttons (#395) * Add sample HanselForms and TwitterDemo to ControlGallery (#651) * [Controls] Add Hanselforms sample * Remove extra twitter sample * [Controls]Add TwitterDemo sample * [Controls] Fix build * Slider should show user-set value on initial load (#378) * [UWP] Use toolbar foreground color on primary items (#640) * Avoid duplicating code in OmPlatform (#591) * [iOS] Entry should not pass a newline to the next responder (#397) * UITextField should not return so that the next field does not get passed a newline * Added code sample * [XamlC] import members on x:Static and factories (#642) * [Xaml] support short Properties for PropertyCondition (#645) * Xamlc compile data triggers (#648) * [Xaml] DataTrigger and PropertyCondition no longer use a ServiceProvider * [XamlC] avoid generating ServiceProvider for unused ProvideValue * fix tests * Fix comment typo * [UWP] Fix TextBox style for foreground focus color (#618) * Adding image to use for CellsGalleryImageUrlCellList UI test * Update ImageCellListPage to use an image we control; Update CellsGalleryImageUrlCellList test to wait longer than 1s for images to load if necessary * fix nre when changing content in datepickerselected (#494) * Make CellsGalleryImageUrlCellList test finish early if possible * [iOS] Change keyboard type while keyboard is visible (#443) * Change keyboard while changing text * add sample code * [Android] Fix NavigationPage dispose crash when it parents a MasterDetailPage (#577) * fix navigation page dispose crash * changes after review * [XamlC] detect duplicate x:Name at compile time (#655) * [XamlC] detect duplicate x:Name at compile time * invoking methods with the right arguments produces better results * Make UWP toolbar display rules consistent with other platforms (#638) * Allow subscriber to be collected if MessagingCenter is the only reference to it (#617) * Repro * Make messaging center callbacks weak references * Preserve attribute * Fix test method name * Watch for collection of actual delegate target instead of wrapper delegate * Preserve the original platform instance when changing main page * Better tests for lambda situations * Update tests, make callback target a weakreference if it's the subscriber * Ensure old Platform MessagingCenter subs are gone before creating new Platform * [iOS] Prevent multiple ListView cells from being swiped simultaneously (#578) * disable multiple cell swipe * add sample code * refactored * convert to weakreference * remove null setting * change weakreference setting place * remove if * revert isopen changes * add instructions * [WinRT/UWP] Apply BackgroundColor to Stepper buttons (#581) * [WinRT/UWP] Apply BackgroundColor to Stepper buttons * Add explanatory text; use nameof * Move explanatory text to a label * Return group instead of internal class (#461) * [iOS/Android] Move Map camera to correct region on layout change (#548) * Move to region on layout change * remove visibility check * [iOS] Platform specifics for controlling Picker SelectedIndex change behavior (#540) * picker selected index could change when picker view is dismissed * use enum * [iOS] Ignore intermittent failing test on XTC (#666) * [UITest] Update to UITest 2.0.5 (#665) * Rebase the current branch onto upstream latest
2017-02-02 21:23:31 +03:00
void OnPointerPressed(object sender, RoutedEventArgs e)
{
((IButtonController)Element)?.SendPressed();
}
void UpdateBackground()
2016-03-22 23:02:25 +03:00
{
Control.BackgroundColor = Element.BackgroundColor != Color.Default ? Element.BackgroundColor.ToBrush() : (Brush)Windows.UI.Xaml.Application.Current.Resources["ButtonBackgroundThemeBrush"];
2016-03-22 23:02:25 +03:00
}
void UpdateBorderColor()
{
Control.BorderBrush = Element.BorderColor != Color.Default ? Element.BorderColor.ToBrush() : (Brush)Windows.UI.Xaml.Application.Current.Resources["ButtonBorderThemeBrush"];
}
void UpdateBorderRadius()
{
Control.BorderRadius = Element.BorderRadius;
}
void UpdateBorderWidth()
{
Control.BorderThickness = Element.BorderWidth == (double)Button.BorderWidthProperty.DefaultValue ? new WThickness(3) : new WThickness(Element.BorderWidth);
2016-03-22 23:02:25 +03:00
}
void UpdateContent()
{
var text = Element.Text;
var elementImage = Element.Image;
// No image, just the text
if (elementImage == null)
2016-03-22 23:02:25 +03:00
{
Control.Content = text;
return;
}
2016-03-22 23:02:25 +03:00
var bmp = new BitmapImage(new Uri("ms-appx:///" + elementImage.File));
var image = new WImage
{
Source = bmp,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
Stretch = Stretch.Uniform
};
bmp.ImageOpened += (sender, args) => {
image.Width = bmp.PixelWidth;
image.Height = bmp.PixelHeight;
Element.InvalidateMeasureNonVirtual(InvalidationTrigger.RendererReady);
};
// No text, just the image
if (string.IsNullOrEmpty(text))
{
Control.Content = image;
return;
}
2016-03-22 23:02:25 +03:00
// Both image and text, so we need to build a container for them
Control.Content = CreateContentContainer(Element.ContentLayout, image, text);
}
static StackPanel CreateContentContainer(Button.ButtonContentLayout layout, WImage image, string text)
{
var container = new StackPanel();
var textBlock = new TextBlock {
Text = text,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
};
2016-03-22 23:02:25 +03:00
var spacing = layout.Spacing;
container.HorizontalAlignment = HorizontalAlignment.Center;
container.VerticalAlignment = VerticalAlignment.Center;
switch (layout.Position)
2016-03-22 23:02:25 +03:00
{
case Button.ButtonContentLayout.ImagePosition.Top:
container.Orientation = Orientation.Vertical;
image.Margin = new WThickness(0, 0, 0, spacing);
container.Children.Add(image);
container.Children.Add(textBlock);
break;
case Button.ButtonContentLayout.ImagePosition.Bottom:
container.Orientation = Orientation.Vertical;
image.Margin = new WThickness(0, spacing, 0, 0);
container.Children.Add(textBlock);
container.Children.Add(image);
break;
case Button.ButtonContentLayout.ImagePosition.Right:
container.Orientation = Orientation.Horizontal;
image.Margin = new WThickness(spacing, 0, 0, 0);
container.Children.Add(textBlock);
container.Children.Add(image);
break;
default:
// Defaults to image on the left
container.Orientation = Orientation.Horizontal;
image.Margin = new WThickness(0, 0, spacing, 0);
container.Children.Add(image);
container.Children.Add(textBlock);
break;
2016-03-22 23:02:25 +03:00
}
return container;
2016-03-22 23:02:25 +03:00
}
void UpdateFont()
{
if (Control == null || Element == null)
return;
if (Element.Font == Font.Default && !_fontApplied)
return;
Font fontToApply = Element.Font == Font.Default ? Font.SystemFontOfSize(NamedSize.Medium) : Element.Font;
Control.ApplyFont(fontToApply);
_fontApplied = true;
}
void UpdateTextColor()
{
Control.Foreground = Element.TextColor != Color.Default ? Element.TextColor.ToBrush() : (Brush)Windows.UI.Xaml.Application.Current.Resources["DefaultTextForegroundThemeBrush"];
}
}
}