【发布时间】:2022-01-03 15:13:23
【问题描述】:
我正在尝试为 Entry 元素设置边框颜色,以便在 Windows 平台上显示。为了实现这一点,我在项目中添加了一个效果,但是当使用 BorderBrush 属性时,它不会保留为 Focused 状态选择的颜色。相反,它会更改为 Windows 强调颜色。您需要在控件的所有状态下控制颜色。
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:effects="clr-namespace:XamarinTestApp.Effects"
x:Class="XamarinTestApp.MainPage">
<StackLayout Margin="20,100,20,0" >
<Label Text="Input element" />
<Entry x:Name="InputEntry">
<Entry.Effects>
<effects:EntryEffect />
</Entry.Effects>
</Entry>
</StackLayout>
</ContentPage>
通用代码中的EntryEffect.cs
using Xamarin.Forms;
namespace XamarinTestApp.Effects
{
public class EntryEffect : RoutingEffect
{
public EntryEffect() : base("EffectGroup.EntryEffect") { }
}
}
UWP 代码中的EntryEffect.cs
using Effects;
using System;
using Windows.UI.Xaml.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;
[assembly: ResolutionGroupName("EffectGroup")]
[assembly: ExportEffect(typeof(EntryEffect), "EntryEffect")]
namespace Effects
{
public class EntryEffect : PlatformEffect
{
protected override void OnAttached()
{
try
{
var control = Control as Control;
var color = Color.Magenta.ToWindowsColor();
control.BorderBrush = new Windows.UI.Xaml.Media.SolidColorBrush(color);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
protected override void OnDetached() { }
}
}
当它处于悬停状态时,我也会遇到同样的问题。 谢谢你的帮助!!
【问题讨论】:
标签: c# xaml xamarin uwp effects