我想向StackLayout GestureRecognizers添加一些效果,以便用户知道他们已经选择了按钮,但是我看不到这样做的方法吗?我试图为StackLayout更改BackgroundColor,但是那是行不通的。
我可以更改BackgroundColor还是可以使用一个长时间保持的事件?
xaml代码
<StackLayout VerticalOptions="Center"
x:Name="slpatient"
Grid.Row="4"
BackgroundColor="#8cb8e1"
Orientation="Horizontal">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="Button_Clicked" />
</StackLayout.GestureRecognizers>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="user_34.png"
Grid.Column="1"
VerticalOptions="Center" />
<Label Text="Click me to change the BackgroundColor!"
Grid.Column="2"
TextColor="White"
LineBreakMode="WordWrap"
VerticalOptions="FillAndExpand"
VerticalTextAlignment="Center"/>
</Grid>
</StackLayout>
cs代码
private void Button_Clicked_Clicked(object sender, System.EventArgs e)
{
slpatient.BackgroundColor = Color.Black;
var masterDetailPage = Application.Current.MainPage as MasterDetailPage;
masterDetailPage.Detail = new NavigationPage((new SearchPage("DrugName")));
}
答案 0 :(得分:1)
更改颜色功能可能发生在非UI线程上。因此,您可以将其封装在Device.BeginInvokeOnMainThread中,以便在UI线程中调用它,如下所示:
Device.BeginInvokeOnMainThread(() =>
{
slpatient.BackgroundColor = Color.Black;
});
要回答第二个问题,是的,您也可以添加一个计时器:
private void Button_Clicked_Clicked(object sender, System.EventArgs e)
{
slpatient.BackgroundColor = Color.Black;
var masterDetailPage = Application.Current.MainPage as MasterDetailPage;
Device.StartTimer(TimeSpan.FromSeconds(10), () =>
{
masterDetailPage.Detail = new NavigationPage((new SearchPage("DrugName")));
return false; // True = Repeat again, False = Stop the timer
});
}
答案 1 :(得分:0)
我试图为StackLayout更改BackgroundColor,但是那是行不通的。
如果要更改背景颜色,建议您考虑使用Xameffects,可以从nuget包中安装xameffects。更改 TouchEffect.Color 。
<ContentPage
x:Class="App4.Page21"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xe="clr-namespace:XamEffects;assembly=XamEffects">
<ContentPage.Content>
<StackLayout
xe:TouchEffect.Color="Red"
HorizontalOptions="Center"
VerticalOptions="Center">
<Button
x:Name="btn1"
Clicked="Btn1_Clicked"
HorizontalOptions="Center"
Text="btn1"
VerticalOptions="Center" />
</StackLayout>
</ContentPage.Content>
更多详细信息,您可以看一下: