影响其他控件的 Xamarin 效果

我遇到了一个奇怪的错误,自定义效果也会影响其他控件,而我只针对一个特定控件。我能够在一个小型测试项目中重现该行为。我想要实现的是更改特定条目控件的颜色。它确实有效,但是当导航回上一页时,效果也会影响其他控件,而我只希望特定效果影响特定控件。我也仅使用 xaml 中的效果列表将效果添加到特定条目。


(请注意,我正在使用标准表单导航页面来像这样在我的 app.xaml 中定义导航。


MainPage = new NavigationPage(new MainPage());

主页.xaml


<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             xmlns:local="clr-namespace:App2"

             x:Class="App2.MainPage">

    <StackLayout>

        <!-- Place new controls here -->

        <Label Text="Welcome to Xamarin.Forms!" 

           HorizontalOptions="Center"

           VerticalOptions="CenterAndExpand" />

        <Entry></Entry>

        <Button Clicked="Button_Clicked"  Text="navigate"></Button>

        <Button Clicked="Button_Clicked2"  Text="navigate to other"></Button>

    </StackLayout>

</ContentPage>

MainPage.xaml 的代码隐藏


public partial class MainPage : ContentPage

{

    public MainPage()

    {

        InitializeComponent();

    }


    private void Button_Clicked(object sender, EventArgs e)

    {

        Application.Current.MainPage.Navigation.PushAsync(new Test());

    }


    private void Button_Clicked2(object sender, EventArgs e)

    {

        Application.Current.MainPage.Navigation.PushAsync(new Test2());

    }

}

测试.Xaml


<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 

             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             xmlns:local="clr-namespace:App2"

             x:Class="App2.Test">

    <ContentPage.Content>

        <Entry>

            <Entry.Effects>

                <local:EntryLineColorEffect></local:EntryLineColorEffect>

            </Entry.Effects>

        </Entry>

  </ContentPage.Content>

</ContentPage>


慕森王
浏览 47回答 1
1回答

叮当猫咪

最后 Xamarin 社区论坛上有人能够回答我的问题。=> 效果不会影响其他没有附加该效果的控件。它似乎Background.SetColorFilter(Color.DarkMagenta.ToAndroid(), Android.Graphics.PorterDuff.Mode.SrcAtop);改变了全局条目的下划线颜色。此设置之后显示的条目将应用此行为。所以你的 Test2 的 Entry 仍然有一个 attacted 风格。Background.Mutate().SetColorFilter(Color.DarkMagenta.ToAndroid(), Android.Graphics.PorterDuff.Mode.SrcAtop);改为使用更正:EditText control;protected override void OnAttached(){&nbsp; &nbsp; control = Control as EditText;&nbsp; &nbsp; UpdateLineColor();}protected override void OnDetached(){&nbsp; &nbsp; control = null;}private void UpdateLineColor(){&nbsp; &nbsp; if (control != null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; control.Background.Mutate().SetColorFilter(Color.DarkMagenta.ToAndroid(), Android.Graphics.PorterDuff.Mode.SrcAtop);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP