Updated SearchBarRenderer / SearchBarControl

* Added PlaceHolder of SearchBarControl
* Refactoring
This commit is contained in:
aosoft 2018-02-16 01:18:33 +09:00
Родитель 8c7222c49d
Коммит 3e79d299d6
6 изменённых файлов: 267 добавлений и 60 удалений

Просмотреть файл

@ -1,4 +1,6 @@

using WDrawing = System.Drawing;
using WForms = System.Windows.Forms;
namespace Xamarin.Forms.Platform.WinForms
{
internal static class ConvertExtensions
@ -10,32 +12,48 @@ namespace Xamarin.Forms.Platform.WinForms
}
*/
public static System.Drawing.Color ToWindowsColor(this Color color)
public static WForms.HorizontalAlignment ToWindowsHorizontalAlignment(this TextAlignment horizonatal)
{
switch (horizonatal)
{
case TextAlignment.Start: return WForms.HorizontalAlignment.Left;
case TextAlignment.Center: return WForms.HorizontalAlignment.Center;
case TextAlignment.End: return WForms.HorizontalAlignment.Right;
}
return WForms.HorizontalAlignment.Left;
}
public static WDrawing.Color ToWindowsColor(this Color color)
{
return color.ToWindowsColor(WDrawing.SystemColors.Control);
}
public static WDrawing.Color ToWindowsColor(this Color color, WDrawing.Color defaultColor)
{
return
color == Color.Default ?
System.Drawing.SystemColors.Control :
System.Drawing.Color.FromArgb((byte)(color.A * 255), (byte)(color.R * 255), (byte)(color.G * 255), (byte)(color.B * 255));
defaultColor :
WDrawing.Color.FromArgb((byte)(color.A * 255), (byte)(color.R * 255), (byte)(color.G * 255), (byte)(color.B * 255));
}
public static System.Drawing.FontStyle ToWindowsFontStyle(this FontAttributes self)
public static WDrawing.FontStyle ToWindowsFontStyle(this FontAttributes self)
{
switch (self)
{
case FontAttributes.Bold:
{
return System.Drawing.FontStyle.Bold;
return WDrawing.FontStyle.Bold;
}
case FontAttributes.Italic:
{
return System.Drawing.FontStyle.Italic;
return WDrawing.FontStyle.Italic;
}
}
return System.Drawing.FontStyle.Regular;
return WDrawing.FontStyle.Regular;
}
public static Rectangle ToXamarinRectangle(this System.Drawing.Rectangle self)
public static Rectangle ToXamarinRectangle(this WDrawing.Rectangle self)
{
return new Rectangle(self.Left, self.Top, self.Width, self.Height);
}

Просмотреть файл

@ -16,7 +16,6 @@ namespace Xamarin.Forms.Platform.WinForms
if (Control == null)
{
SetNativeControl(new WForms.Button());
Control.Click += OnClick;
}
UpdateText();
@ -27,6 +26,20 @@ namespace Xamarin.Forms.Platform.WinForms
base.OnElementChanged(e);
}
protected override void OnNativeElementChanged(NativeElementChangedEventArgs<WForms.Button> e)
{
base.OnNativeElementChanged(e);
if (e.OldControl != null)
{
e.OldControl.Click -= OnClick;
}
if (e.NewControl != null)
{
e.NewControl.Click += OnClick;
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == Button.TextProperty.PropertyName)

Просмотреть файл

@ -78,37 +78,21 @@ namespace Xamarin.Forms.Platform.WinForms
void UpdateText()
{
var nativeElement = Control;
if (nativeElement != null)
{
nativeElement.Text = Element.Text;
}
UpdatePropertyHelper((element, control) => control.Text = element.Text);
}
void UpdateTextColor()
{
var nativeElement = Control;
if (nativeElement != null)
{
var color = Element.TextColor;
nativeElement.ForeColor =
color == Color.Default ?
System.Drawing.SystemColors.ControlText :
color.ToWindowsColor();
}
UpdatePropertyHelper((element, control) => control.ForeColor = element.TextColor.ToWindowsColor());
}
void UpdateFont()
{
var nativeElement = Control;
var element = Element;
if (nativeElement != null)
{
nativeElement.Font = new System.Drawing.Font(
UpdatePropertyHelper((element, control) =>
control.Font = new System.Drawing.Font(
element.FontFamily,
(float)element.FontSize,
element.FontAttributes.ToWindowsFontStyle());
}
Math.Max((float)element.FontSize, 1.0f),
element.FontAttributes.ToWindowsFontStyle()));
}
#endregion

Просмотреть файл

@ -77,37 +77,21 @@ namespace Xamarin.Forms.Platform.WinForms
void UpdateText()
{
var nativeElement = Control;
if (nativeElement != null)
{
nativeElement.Text = Element.Text;
}
UpdatePropertyHelper((element, control) => control.Text = element.Text);
}
void UpdateTextColor()
{
var nativeElement = Control;
if (nativeElement != null)
{
var color = Element.TextColor;
nativeElement.ForeColor =
color == Color.Default ?
System.Drawing.SystemColors.ControlText :
color.ToWindowsColor();
}
UpdatePropertyHelper((element, control) => control.ForeColor = element.TextColor.ToWindowsColor());
}
void UpdateFont()
{
var nativeElement = Control;
var element = Element;
if (nativeElement != null)
{
nativeElement.Font = new System.Drawing.Font(
UpdatePropertyHelper((element, control) =>
control.Font = new System.Drawing.Font(
element.FontFamily,
(float)element.FontSize,
element.FontAttributes.ToWindowsFontStyle());
}
Math.Max((float)element.FontSize, 1.0f),
element.FontAttributes.ToWindowsFontStyle()));
}
#endregion

Просмотреть файл

@ -1,17 +1,76 @@
using System;
using System.ComponentModel;
using WDrawing = System.Drawing;
using WForms = System.Windows.Forms;
namespace Xamarin.Forms.Platform.WinForms
{
public class SearchBarControl : WForms.UserControl
{
WForms.TextBox _textbox;
public class CustomTextBox : WForms.TextBox
{
string _placeholder = "Search";
WDrawing.Color _placeholderColor = WDrawing.SystemColors.InactiveCaptionText;
WDrawing.Brush _brush = null;
public CustomTextBox()
{
}
public string Placeholder
{
get => _placeholder;
set
{
if (!string.Equals(value, _placeholder))
{
_placeholder = value;
Invalidate();
}
}
}
public WDrawing.Color PlaceholderColor
{
get => _placeholderColor;
set
{
if (value != _placeholderColor)
{
_placeholderColor = value;
_brush?.Dispose();
_brush = null;
Invalidate();
}
}
}
protected override void WndProc(ref WForms.Message m)
{
if (m.Msg == 15 &&
Enabled &&
string.IsNullOrEmpty(Text) &&
!string.IsNullOrEmpty(_placeholder))
{
using (var g = CreateGraphics())
{
if (_brush == null)
{
_brush = new WDrawing.SolidBrush(_placeholderColor);
}
g.DrawString(_placeholder, Font, _brush, 1, 1);
}
}
base.WndProc(ref m);
}
}
CustomTextBox _textbox;
WForms.Button _btnSearch;
public SearchBarControl()
{
_textbox = new WForms.TextBox()
_textbox = new CustomTextBox()
{
Anchor = WForms.AnchorStyles.None,
Parent = this,
@ -25,6 +84,34 @@ namespace Xamarin.Forms.Platform.WinForms
Anchor = WForms.AnchorStyles.None,
Parent = this,
};
_btnSearch.Click += (s, e) =>
{
SearchButtonClick?.Invoke(this, e);
};
_textbox.KeyDown += (s, e) =>
{
switch (e.KeyCode)
{
case WForms.Keys.Enter:
{
SearchButtonClick?.Invoke(this, e);
}
break;
case WForms.Keys.Escape:
{
CancelButtonClick?.Invoke(this, e);
}
break;
};
};
_textbox.TextChanged += (s, e) =>
{
SearchTextChanged?.Invoke(this, e);
};
}
protected override void Dispose(bool disposing)
@ -39,6 +126,39 @@ namespace Xamarin.Forms.Platform.WinForms
base.Dispose(disposing);
}
public string SearchText
{
get => _textbox.Text;
set => _textbox.Text = value;
}
public WDrawing.Color SearchTextColor
{
get => _textbox.ForeColor;
set => _textbox.ForeColor = value;
}
public WForms.HorizontalAlignment SearchTextAlign
{
get => _textbox.TextAlign;
set => _textbox.TextAlign = value;
}
public string PlaceHolder
{
get => _textbox.Placeholder;
set => _textbox.Placeholder = value;
}
public WDrawing.Color PlaceHolderColor
{
get => _textbox.PlaceholderColor;
set => _textbox.PlaceholderColor = value;
}
public event EventHandler SearchButtonClick;
public event EventHandler CancelButtonClick;
public event EventHandler SearchTextChanged;
protected override void OnLoad(EventArgs e)
{

Просмотреть файл

@ -1,11 +1,14 @@
using System;
using System.ComponentModel;
using WForms = System.Windows.Forms;
using WDrawing = System.Drawing;
namespace Xamarin.Forms.Platform.WinForms
{
public class SearchBarRenderer : ViewRenderer<SearchBar, SearchBarControl>
{
const string DefaultPlaceholder = "Search";
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
if (e.NewElement != null)
@ -13,15 +16,100 @@ namespace Xamarin.Forms.Platform.WinForms
if (Control == null)
{
SetNativeControl(new SearchBarControl());
//Control.Click += OnClick;
}
//UpdateText();
//UpdateTextColor();
//UpdateFont();
UpdateText();
UpdatePlaceholder();
UpdateAlignment();
UpdateFont();
UpdatePlaceholderColor();
UpdateTextColor();
}
base.OnElementChanged(e);
}
protected override void OnNativeElementChanged(NativeElementChangedEventArgs<SearchBarControl> e)
{
base.OnNativeElementChanged(e);
if (e.OldControl != null)
{
e.OldControl.SearchButtonClick -= OnSearchButtonClick;
e.OldControl.CancelButtonClick -= OnCancelButtonClick;
}
if (e.NewControl != null)
{
e.NewControl.SearchButtonClick += OnSearchButtonClick;
e.NewControl.CancelButtonClick += OnCancelButtonClick;
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == SearchBar.TextProperty.PropertyName)
UpdateText();
else if (e.PropertyName == SearchBar.PlaceholderProperty.PropertyName)
UpdatePlaceholder();
else if (e.PropertyName == SearchBar.FontAttributesProperty.PropertyName ||
e.PropertyName == SearchBar.FontFamilyProperty.PropertyName ||
e.PropertyName == SearchBar.FontSizeProperty.PropertyName)
UpdateFont();
else if (e.PropertyName == SearchBar.HorizontalTextAlignmentProperty.PropertyName)
UpdateAlignment();
else if (e.PropertyName == SearchBar.PlaceholderColorProperty.PropertyName)
UpdatePlaceholderColor();
else if (e.PropertyName == SearchBar.TextColorProperty.PropertyName)
UpdateTextColor();
}
void OnSearchButtonClick(object sender, EventArgs e)
{
}
void OnCancelButtonClick(object sender, EventArgs e)
{
}
void UpdateText()
{
UpdatePropertyHelper((element, control) => control.SearchText = element.Text);
}
void UpdatePlaceholder()
{
UpdatePropertyHelper((element, control) =>
{
control.PlaceHolder = element.Placeholder ?? DefaultPlaceholder;
});
}
void UpdateFont()
{
UpdatePropertyHelper((element, control) =>
control.Font = new System.Drawing.Font(
element.FontFamily,
Math.Max((float)element.FontSize, 1.0f),
element.FontAttributes.ToWindowsFontStyle()));
}
void UpdateAlignment()
{
UpdatePropertyHelper((element, control) => control.SearchTextAlign =
element.HorizontalTextAlignment.ToWindowsHorizontalAlignment());
}
void UpdatePlaceholderColor()
{
UpdatePropertyHelper((element, control) => control.PlaceHolderColor = element.PlaceholderColor.ToWindowsColor(WDrawing.SystemColors.GrayText));
}
void UpdateTextColor()
{
UpdatePropertyHelper((element, control) => control.SearchTextColor = element.TextColor.ToWindowsColor(WDrawing.SystemColors.ControlText));
}
}
}