[Bug] Fix ClearButtonVisibility.Never does not take effect on UWP (#9250)

* Setup Issue Page

* Update VSM to remove delete button

Fixes #8836
This commit is contained in:
Brian Macomber 2020-06-02 18:44:09 -05:00 коммит произвёл GitHub
Родитель f88335677a
Коммит c669481410
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 96 добавлений и 1 удалений

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

@ -0,0 +1,48 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 8836, "[Bug] ClearButtonVisibility.Never does not take effect on UWP", PlatformAffected.UWP)]
public class Issue8836 : TestContentPage
{
protected override void Init()
{
var stack = new StackLayout();
stack.Children.Add(new Label {Text = "Click the button to toggle the clear button visibility."});
Entry = new Entry
{
Text = "Clear Button: While Editing",
ClearButtonVisibility = ClearButtonVisibility.WhileEditing
};
stack.Children.Add(Entry);
var button = new Button { Text = "Toggle Clear Button State" };
button.Clicked += Button_Clicked;
stack.Children.Add(button);
Content = stack;
}
private void Button_Clicked(object sender, System.EventArgs e)
{
if (Entry.ClearButtonVisibility == ClearButtonVisibility.Never)
{
Entry.ClearButtonVisibility = ClearButtonVisibility.WhileEditing;
Entry.Text = "Clear Button: While Editing";
}
else
{
Entry.ClearButtonVisibility = ClearButtonVisibility.Never;
Entry.Text = "Clear Button: Never";
}
Entry.Focus();
}
public Entry Entry { get; set; }
}
}

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

@ -44,6 +44,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue8262.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8899.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8551.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8836.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8902.xaml.cs">
<DependentUpon>Issue8902.xaml</DependentUpon>
<SubType>Code</SubType>

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

@ -66,6 +66,9 @@ namespace Xamarin.Forms.Platform.UWP
UpdateReturnType();
UpdateIsReadOnly();
UpdateInputScope();
UpdateClearButtonVisibility();
if (_cursorPositionChangePending)
UpdateCursorPosition();
@ -148,6 +151,8 @@ namespace Xamarin.Forms.Platform.UWP
UpdateSelectionLength();
else if (e.PropertyName == InputView.IsReadOnlyProperty.PropertyName)
UpdateIsReadOnly();
else if (e.PropertyName == Entry.ClearButtonVisibilityProperty.PropertyName)
UpdateClearButtonVisibility();
}
protected override void UpdateBackgroundColor()
@ -236,6 +241,11 @@ namespace Xamarin.Forms.Platform.UWP
Control.CharacterSpacing = Element.CharacterSpacing.ToEm();
}
void UpdateClearButtonVisibility()
{
Control.ClearButtonVisible = Element.ClearButtonVisibility == ClearButtonVisibility.WhileEditing;
}
void UpdateInputScope()
{
Entry entry = Element;

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

@ -42,9 +42,15 @@ namespace Xamarin.Forms.Platform.UWP
public new static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text),
typeof(string), typeof(FormsTextBox), new PropertyMetadata("", TextPropertyChanged));
public static readonly DependencyProperty ClearButtonVisibleProperty = DependencyProperty.Register(nameof(ClearButtonVisible),
typeof(bool), typeof(FormsTextBox), new PropertyMetadata(true, ClearButtonVisibleChanged));
InputScope _passwordInputScope;
InputScope _numericPasswordInputScope;
Border _borderElement;
Windows.UI.Xaml.Controls.Grid _rootGrid;
Windows.UI.Xaml.VisualState _DeleteButtonVisibleState;
Windows.UI.Xaml.VisualStateGroup _DeleteButtonVisibleStateGroups;
InputScope _cachedInputScope;
bool _cachedPredictionsSetting;
bool _cachedSpellCheckSetting;
@ -64,6 +70,12 @@ namespace Xamarin.Forms.Platform.UWP
UpdateEnabled();
}
public bool ClearButtonVisible
{
get { return (bool)GetValue(ClearButtonVisibleProperty); }
set { SetValue(ClearButtonVisibleProperty, value);}
}
public Brush BackgroundFocusBrush
{
get { return (Brush)GetValue(BackgroundFocusBrushProperty); }
@ -146,6 +158,15 @@ namespace Xamarin.Forms.Platform.UWP
// so we can manually handle its background when focused
_borderElement = (Border)GetTemplateChild("BorderElement");
}
_rootGrid = (Windows.UI.Xaml.Controls.Grid)GetTemplateChild("RootGrid");
if (_rootGrid != null)
{
var stateGroups = WVisualStateManager.GetVisualStateGroups(_rootGrid).ToList();
_DeleteButtonVisibleStateGroups = stateGroups.SingleOrDefault(sg => sg.Name == "ButtonStates");
if (_DeleteButtonVisibleStateGroups != null)
_DeleteButtonVisibleState = _DeleteButtonVisibleStateGroups.States.SingleOrDefault(s => s.Name == "ButtonVisible");
}
}
void DelayObfuscation()
@ -346,6 +367,21 @@ namespace Xamarin.Forms.Platform.UWP
SelectionStart = base.Text.Length;
}
static void ClearButtonVisibleChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var textBox = (FormsTextBox)dependencyObject;
var visibleState = textBox._DeleteButtonVisibleState;
var states = textBox._DeleteButtonVisibleStateGroups?.States;
if (states != null && visibleState != null)
{
if (textBox.ClearButtonVisible && !states.Contains(visibleState))
states.Add(visibleState);
else
states.Remove(visibleState);
}
}
static void TextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var textBox = (FormsTextBox)dependencyObject;

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

@ -26,7 +26,7 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="uwp:FormsTextBox">
<Grid>
<Grid x:Name="RootGrid">
<Grid.Resources>
<Style x:Name="DeleteButtonStyle" TargetType="Button">
<Setter Property="Template">