Xamarin.Forms: 设置UWP中TextBox的边框颜色
Xamarin.Forms: Set border color of TextBox in UWP
我想更改 TextBox
的边框颜色以在 Xamarin.Forms 中使用它。这是通过自定义渲染器完成的。
第一次尝试:
自定义渲染器:
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
var control = this.Control as TextBox;
if (control != null)
{
control.BorderBrush = new SolidColorBrush(Windows.UI.ColorHelper.FromArgb(0, 8, 38, 85));
}
}
结果:边框颜色不见了,点进去后变成我的颜色TextBox
第二次尝试:
自定义渲染器(EntryRenderer
的子类):
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
if(this.Control == null)
{
var control = new ColoredTextBox();
SetNativeControl(control);
}
base.OnElementChanged(e);
}
自定义控件:
public class ColoredTextBox : FormsTextBox
{
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
var control = e.OriginalSource as TextBox;
control.BorderBrush = new SolidColorBrush(Windows.UI.ColorHelper.FromArgb(0, 88, 128, 85));
}
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
var control = e.OriginalSource as TextBox;
control.BorderBrush = new SolidColorBrush(Windows.UI.ColorHelper.FromArgb(0, 88, 128, 85));
}
}
结果:应用崩溃
Exception: Object reference not set to an instance of an object.
StackTrace (last line): at Xamarin.Forms.Platform.UWP.VisualElementRenderer`2.SetNativeControl(TNativeElement control)
第三次尝试:
自定义渲染器更改为 ViewRenderer
:
public class CustomEntryRenderer : ViewRenderer<CustomEntry, ColoredTextBox>
{
protected override void OnElementChanged(ElementChangedEventArgs<CustomEntry> e)
{
var view = Element as CustomEntry;
if (e.OldElement != null || view == null)
return;
var textBox = new ColoredTextBox();
SetNativeControl(textBox);
base.OnElementChanged(e);
}
}
结果:边框颜色消失了,点击 TextBox
后变成了我的颜色。此外,我在 XAML 中设置的文本未在新控件上设置(但这并不奇怪)。现在调用了 OnGotFocus()
和 OnLostFocus()
事件,但如前所述,它们没有带来预期的效果。有趣的是,如果我设置断点并且它不会停止调用,那么事件会一直被调用。
如何在 UWP 中以编程方式更改 TextBox
的边框颜色?
编辑:
关于重复:链接的问题通常针对强调色,但以 TextBox
为例。我的主要目标是以编程方式而不是 style/theme 来完成。由于目前似乎没有其他方法可以做到这一点,因此解决方案几乎相同。所以我会把它留给社区来决定它是否重复。
在你的UWP project
中App.Xaml
添加如下代码:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="#YourHexColourHere" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
这会覆盖 Xamarin.Forms
设置的样式 here
像这样
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if(Control != null)
{
if(Resources.ThemeDictionaries.ContainsKey("SystemControlHighlightAccentBrush"))
{
var borderBrush = Resources.ThemeDictionaries["SystemControlHighlightAccentBrush"] as SolidColorBrush;
borderBrush.Color = ###desired color!
};
}
}
我想更改 TextBox
的边框颜色以在 Xamarin.Forms 中使用它。这是通过自定义渲染器完成的。
第一次尝试:
自定义渲染器:
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
var control = this.Control as TextBox;
if (control != null)
{
control.BorderBrush = new SolidColorBrush(Windows.UI.ColorHelper.FromArgb(0, 8, 38, 85));
}
}
结果:边框颜色不见了,点进去后变成我的颜色TextBox
第二次尝试:
自定义渲染器(EntryRenderer
的子类):
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
if(this.Control == null)
{
var control = new ColoredTextBox();
SetNativeControl(control);
}
base.OnElementChanged(e);
}
自定义控件:
public class ColoredTextBox : FormsTextBox
{
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
var control = e.OriginalSource as TextBox;
control.BorderBrush = new SolidColorBrush(Windows.UI.ColorHelper.FromArgb(0, 88, 128, 85));
}
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
var control = e.OriginalSource as TextBox;
control.BorderBrush = new SolidColorBrush(Windows.UI.ColorHelper.FromArgb(0, 88, 128, 85));
}
}
结果:应用崩溃
Exception: Object reference not set to an instance of an object.
StackTrace (last line): at Xamarin.Forms.Platform.UWP.VisualElementRenderer`2.SetNativeControl(TNativeElement control)
第三次尝试:
自定义渲染器更改为 ViewRenderer
:
public class CustomEntryRenderer : ViewRenderer<CustomEntry, ColoredTextBox>
{
protected override void OnElementChanged(ElementChangedEventArgs<CustomEntry> e)
{
var view = Element as CustomEntry;
if (e.OldElement != null || view == null)
return;
var textBox = new ColoredTextBox();
SetNativeControl(textBox);
base.OnElementChanged(e);
}
}
结果:边框颜色消失了,点击 TextBox
后变成了我的颜色。此外,我在 XAML 中设置的文本未在新控件上设置(但这并不奇怪)。现在调用了 OnGotFocus()
和 OnLostFocus()
事件,但如前所述,它们没有带来预期的效果。有趣的是,如果我设置断点并且它不会停止调用,那么事件会一直被调用。
如何在 UWP 中以编程方式更改 TextBox
的边框颜色?
编辑:
关于重复:链接的问题通常针对强调色,但以 TextBox
为例。我的主要目标是以编程方式而不是 style/theme 来完成。由于目前似乎没有其他方法可以做到这一点,因此解决方案几乎相同。所以我会把它留给社区来决定它是否重复。
在你的UWP project
中App.Xaml
添加如下代码:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="#YourHexColourHere" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
这会覆盖 Xamarin.Forms
设置的样式 here
像这样
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if(Control != null)
{
if(Resources.ThemeDictionaries.ContainsKey("SystemControlHighlightAccentBrush"))
{
var borderBrush = Resources.ThemeDictionaries["SystemControlHighlightAccentBrush"] as SolidColorBrush;
borderBrush.Color = ###desired color!
};
}
}