【发布时间】:2021-11-16 14:24:11
【问题描述】:
我正在尝试创建类似this 的东西,所以我编写了以下代码:
这是 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"
x:Class="alphatrial.MainPage"
BackgroundColor="#ffc40c"
xmlns:local="clr-namespace:alphatrial.Effects">
<ContentPage.Effects>
<local:StatusBarEffect
BackgroundColor="#ffc40c"/>
</ContentPage.Effects>
<StackLayout >
<Label Text="ISC - Alpha" TextColor="Black" FontSize="Large" FontAttributes="Bold" FontFamily="MyAwesomeCustomFont" HorizontalOptions="End" Margin="0,50,50,-14" />
<Line Stroke="black" X1="0" X2="340" StrokeThickness="3" />
<Label Text="App" TextColor="White" FontSize="Large" FontAttributes="Bold" FontFamily="MyAwesomeCustomFont" HorizontalOptions="End" Margin="0,-12,15,0"/>
<Frame Margin="0,80,0,0" BackgroundColor="Transparent">
<StackLayout >
<Label Text="Email" TextColor="Black" FontSize="Small" Margin="25,0,20,0" x:Name="emailAnimate" BindingContext="email" />
<Entry Margin="20,-40,20,0" x:Name="email"/>
</StackLayout>
</Frame>
<Frame Margin="0,-10,0,0" BackgroundColor="Transparent">
<StackLayout >
<Label Text="Password" TextColor="Black" FontSize="Small" Margin="25,0,20,0" x:Name="PassAnimate" />
<Entry Margin="20,-40,20,0" x:Name="password" IsPassword="True"/>
</StackLayout>
</Frame>
</StackLayout>
</ContentPage>
这是 xaml.cs 文件:
public MainPage()
{
InitializeComponent();
email.Focused += Email_Focused;
email.Unfocused += Email_Unfocused;
password.Focused += Password_Focused;
password.Unfocused += Password_Unfocused;
}
private void Password_Unfocused(object sender, FocusEventArgs e)
{
if (password.Text == "")
{
PassAnimate.TranslateTo(0, 0, 100);
PassAnimate.ScaleTo(1, 100);
}
}
private void Password_Focused(object sender, FocusEventArgs e)
{
if (password.IsFocused)
{
PassAnimate.ScaleTo(0.8, 150);
PassAnimate.TranslateTo(-25, -30, 150);
}
}
private void Email_Unfocused(object sender, FocusEventArgs e)
{
if(email.Text=="")
{
emailAnimate.TranslateTo(0, 0, 100);
emailAnimate.ScaleTo(1, 100);
}
}
private void Email_Focused(object sender, FocusEventArgs e)
{
if (email.IsFocused)
{
emailAnimate.ScaleTo(0.8,150);
emailAnimate.TranslateTo(-25, -30,150);
}
}
}
}
但是我得到了this。
当我从条目中移除焦点时,unfocus 函数不起作用。但是,当我在条目中写一些东西,然后清除它并移除焦点时,动画效果很好。有什么问题?
更新:
当我删除检查条目是否为空的代码时:
if(email.Text=="")
动画成功了。似乎这不是检查条目是否为空的正确方法。你能告诉我如何知道 entry 是空的吗?
【问题讨论】:
标签: xaml xamarin.forms label android-animation xamarin.forms.entry