【问题标题】:Xamarin Windows Entry background ColorXamarin Windows 条目背景颜色
【发布时间】:2016-06-09 04:55:24
【问题描述】:

在我的 Xamarin 表单应用程序中,当 Entry 控件出现焦点时,我需要更改它的背景颜色。我添加了自定义渲染器,但是当它在 Windows 应用程序中具有焦点时,默认的白色不会改变。它在 ios 和 android 中运行良好。这是我的 windows 自定义渲染器。

public class MyCustomEntryRenderer : EntryRenderer
{
   protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
   {
       if (Control != null)
       {
           Control.Foreground = new SolidColorBrush(Colors.Transparent);
           Control.Background = new SolidColorBrush(Colors.Transparent);
       }
       base.OnElementChanged(e);
   }

   protected override void OnElementPropertyChanged(object sender, 
                                            System.ComponentModel.PropertyChangedEventArgs e)
   {
       if (Control != null)
       {
           Control.Foreground = new SolidColorBrush(Colors.Transparent);
           Control.Background = new SolidColorBrush(Colors.Transparent);
       }
       base.OnElementPropertyChanged(sender, e);
   }
}

【问题讨论】:

  • 您使用的是哪个版本的 Xamarin.Forms?

标签: windows-phone-8 xamarin xamarin.ios xamarin.android xamarin.forms


【解决方案1】:

您需要覆盖默认的 TextBox 样式。 Here is the default style 您可以像这样在 App.xaml 中添加样式:

<Application.Resources>
        <ResourceDictionary>                 
            <SolidColorBrush x:Key="RedBrush" Color="Red"></SolidColorBrush>
            <!-- Default style for Windows.UI.Xaml.Controls.TextBox -->
            <Style x:Key="CustomTextBoxStyle" TargetType="TextBox" >
                <Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}" />
                <Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}" />
                <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
                <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundAltHighBrush}" />
                <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundChromeDisabledLowBrush}" />
                <Setter Property="SelectionHighlightColor" Value="{ThemeResource SystemControlHighlightAccentBrush}" />
                <Setter Property="BorderThickness" Value="{ThemeResource TextControlBorderThemeThickness}" />
                <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
                <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
                <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Auto" />
                <Setter Property="ScrollViewer.VerticalScrollMode" Value="Auto" />
                <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
                <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
                <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
                <Setter Property="Padding" Value="{ThemeResource TextControlThemePadding}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TextBox">
                            <Grid>
                                <Grid.Resources>
                                    <Style x:Name="DeleteButtonStyle" TargetType="Button">
                                        <Setter Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="Button">
                                                    <Grid x:Name="ButtonLayoutGrid" BorderBrush="{ThemeResource TextBoxButtonBorderThemeBrush}"
                                                  BorderThickness="{TemplateBinding BorderThickness}"
                                                  Background="{ThemeResource TextBoxButtonBackgroundThemeBrush}">
                                                        <VisualStateManager.VisualStateGroups>
                                                            <VisualStateGroup x:Name="CommonStates">
                                                                <VisualState x:Name="Normal" />
                                                                <VisualState x:Name="PointerOver">
                                                                    <Storyboard>
                                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="GlyphElement"
                                                           Storyboard.TargetProperty="Foreground">
                                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAccentBrush}" />
                                                                        </ObjectAnimationUsingKeyFrames>
                                                                    </Storyboard>
                                                                </VisualState>
                                                                <VisualState x:Name="Pressed">
                                                                    <Storyboard>
                                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonLayoutGrid"
                                                           Storyboard.TargetProperty="Background">
                                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAccentBrush}" />
                                                                        </ObjectAnimationUsingKeyFrames>
                                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="GlyphElement"
                                                           Storyboard.TargetProperty="Foreground">
                                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource RedBrush}" />
                                                                        </ObjectAnimationUsingKeyFrames>
                                                                    </Storyboard>
                                                                </VisualState>
                                                                <VisualState x:Name="Disabled">
                                                                    <Storyboard>
                                                                        <DoubleAnimation Storyboard.TargetName="ButtonLayoutGrid"
                                             Storyboard.TargetProperty="Opacity"
                                             To="0"
                                             Duration="0" />
                                                                    </Storyboard>
                                                                </VisualState>
                                                            </VisualStateGroup>
                                                        </VisualStateManager.VisualStateGroups>
                                                        <TextBlock x:Name="GlyphElement"
                                Foreground="{ThemeResource SystemControlForegroundChromeBlackMediumBrush}"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Center"
                                FontStyle="Normal"
                                FontSize="12"
                                Text="&#xE10A;"
                                FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                AutomationProperties.AccessibilityView="Raw"/>
                                                    </Grid>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </Grid.Resources>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="HeaderContentPresenter"
                                                 Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement"
                                               Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement"
                                               Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement"
                                               Storyboard.TargetProperty="BorderBrush">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement"
                                               Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledChromeDisabledLowBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PlaceholderTextContentPresenter"
                                               Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledChromeDisabledLowBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Normal" />
                                        <VisualState x:Name="PointerOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement"
                                               Storyboard.TargetProperty="BorderBrush">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightChromeAltLowBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement"
                                               Storyboard.TargetProperty="Opacity">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextControlBackgroundHoverOpacity}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Focused">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PlaceholderTextContentPresenter"
                                               Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlPageTextChromeBlackMediumLowBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <!--<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement"
                                               Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundChromeWhiteBrush}" />
                                                </ObjectAnimationUsingKeyFrames>-->

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement"
Storyboard.TargetProperty="Background">
    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource RedBrush}" />
</ObjectAnimationUsingKeyFrames>



<!--<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement"
Storyboard.TargetProperty="Opacity">
    <DiscreteObjectKeyFrame KeyTime="0" Value="0" />
</ObjectAnimationUsingKeyFrames>-->
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement"
                                               Storyboard.TargetProperty="BorderBrush">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAccentBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement"
                                               Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlForegroundChromeBlackHighBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement"
                                               Storyboard.TargetProperty="RequestedTheme">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Light" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                    <VisualStateGroup x:Name="ButtonStates">
                                        <VisualState x:Name="ButtonVisible">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DeleteButton"
                                               Storyboard.TargetProperty="Visibility">
                                                    <DiscreteObjectKeyFrame KeyTime="0">
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <Visibility>Visible</Visibility>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="ButtonCollapsed" />
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Border x:Name="BackgroundElement"
                                    Grid.Row="1"
                                    Background="{TemplateBinding Background}"
                                    Margin="{TemplateBinding BorderThickness}"
                                    Opacity="{ThemeResource TextControlBackgroundRestOpacity}"
                                    Grid.ColumnSpan="2"
                                    Grid.RowSpan="1"/>
                                <Border x:Name="BorderElement"
                                    Grid.Row="1"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Grid.ColumnSpan="2"
                                    Grid.RowSpan="1"/>
                                <ContentPresenter x:Name="HeaderContentPresenter"
                                    x:DeferLoadStrategy="Lazy"
                                    Visibility="Collapsed"
                                    Grid.Row="0"
                                    Foreground="{ThemeResource SystemControlForegroundBaseHighBrush}"
                                    Margin="0,0,0,8"
                                    Grid.ColumnSpan="2"
                                    Content="{TemplateBinding Header}"
                                    ContentTemplate="{TemplateBinding HeaderTemplate}"
                                    FontWeight="Normal" />
                                <ScrollViewer x:Name="ContentElement"
                                    Grid.Row="1"
                                    HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                                    HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                    VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                                    VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                                    IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
                                    IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
                                    IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                                    Margin="{TemplateBinding BorderThickness}"
                                    Padding="{TemplateBinding Padding}"
                                    IsTabStop="False"
                                    AutomationProperties.AccessibilityView="Raw"
                                    ZoomMode="Disabled" />
                                <ContentControl x:Name="PlaceholderTextContentPresenter"
                                    Grid.Row="1"
                                    Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}"
                                    Margin="{TemplateBinding BorderThickness}"
                                    Padding="{TemplateBinding Padding}"
                                    IsTabStop="False"
                                    Grid.ColumnSpan="2"
                                    Content="{TemplateBinding PlaceholderText}"
                                    IsHitTestVisible="False"/>
                                <Button x:Name="DeleteButton"
                                    Grid.Row="1"
                                    Style="{StaticResource DeleteButtonStyle}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Margin="{ThemeResource HelperButtonThemePadding}"
                                    IsTabStop="False"
                                    Grid.Column="1"
                                    Visibility="Collapsed"
                                    FontSize="{TemplateBinding FontSize}"
                                    MinWidth="34"
                                    VerticalAlignment="Stretch"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </Application.Resources>

现在您可以覆盖“Focused”VisualState:

  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement"
Storyboard.TargetProperty="Background">
    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource RedBrush}" />
</ObjectAnimationUsingKeyFrames>

并评论、删除或覆盖“不透明度”

<!--<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement"
Storyboard.TargetProperty="Opacity">
    <DiscreteObjectKeyFrame KeyTime="0" Value="0" />
</ObjectAnimationUsingKeyFrames>-->

现在您需要在渲染器中加载样式并为您的文本框添加:

private TextBox nativeControl;
private CustomEntry customControl;
protected override void 
OnElementChanged(ElementChangedEventArgs<CustomEntry> e)
{
  base.OnElementChanged(e);

  this.customControl = e.NewElement;
  this.nativeControl = new TextBox();

  var style  = App.Current.Resources["CustomTextBoxStyle"] as    Windows.UI.Xaml.Style;
  this.nativeControl.Style = style;
  this.SetNativeControl(nativeControl);
}

如果 TextBox 被聚焦,现在你的背景会变成红色

【讨论】:

  • 成功了。但在那之后,视图模型的绑定属性就不起作用了。
  • 您应该在渲染器中连接自定义控件和本机控件的属性。处理本机控件(在渲染器类中)的事件和状态,然后设置您的自定义控件。如果原生控件的文本属性发生变化(TextChanged事件),设置自定义控件的文本属性:this.customControl.Text = this.nativeControl.Text;
  • 它适用于按钮控件,但对于文本框我得到以下错误 this.SetNativeControl 只接受 FormsTextBox 类型的对象。
  • 您的 Renderer 应该从 ViewRenderer 派生以在 SetNativeControl 方法中使用自定义控件
【解决方案2】:

WindowsPhone 在这个上有点奇怪,因为它在某些情况下会重置背景颜色。

要解决此问题,您还需要挂钩特定于平台的控件本身的 GotFocus 和 LostFocus 事件。

【讨论】:

    猜你喜欢
    • 2013-07-12
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2014-04-16
    相关资源
    最近更新 更多