XAML 为什么我的图像按钮在AbsoluteLayout中不起作用?在.NET MAUI中

fnvucqvd  于 2022-12-28  发布在  .NET
关注(0)|答案(1)|浏览(191)

我在网格中有一个ImageButton。在ImageButton周围有一个<AbsoluteLayout>。当单击时,没有任何React或方法没有执行。
我的XAML:

<VerticalStackLayout
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand"
    BackgroundColor="#24D4A3">
    <Grid BackgroundColor="White">
        <AbsoluteLayout>
            <ImageButton 
                                Clicked="CreateNote"                     
                                Source="add.png"
                                BorderColor="#2b3c3c" 
                                BorderWidth="0" 
                                BackgroundColor="#34A4EB" 
                                CornerRadius="35" 
                                WidthRequest="70" 
                                HeightRequest="70" 
                                AbsoluteLayout.LayoutBounds="313,625" 
                                Padding="2,0,0,0"/>
        </AbsoluteLayout>
    </Grid>
</VerticalStackLayout>

创建注解方法:

private async void CreateNote(object sender, EventArgs e)
{
    await Shell.Current.GoToAsync("//CreateNote");
}

我将感激任何帮助!!

pobjuy32

pobjuy321#

我复制了你的代码到我的应用程序,并做了测试,我发现你的图像按钮上方的空间太大,我无法看到它在我的模拟器。
所以我在VerticalStackLayout中添加了一个ScrollView,这样我就可以在滚动页面后看到ImageButton,并且还可以触发ImageButton的点击事件ImageButton_Clicked

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1226.MainPage">

    <ScrollView>
        <VerticalStackLayout
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand"
    BackgroundColor="#24D4A3">
            <Grid BackgroundColor="White">
                <AbsoluteLayout>
                    <ImageButton 
                                Clicked="ImageButton_Clicked"                     
                                Source="icon.png"
                                BorderColor="#2b3c3c" 
                                BorderWidth="0" 
                                BackgroundColor="#34A4EB" 
                                CornerRadius="35" 
                                WidthRequest="70" 
                                HeightRequest="70" 
                                AbsoluteLayout.LayoutBounds="313,625" 
                                Padding="2,0,0,0"/>
                </AbsoluteLayout>
            </Grid>

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

函数ImageButton_Clicked

private void ImageButton_Clicked(object sender, EventArgs e) 
      {
            System.Diagnostics.Debug.WriteLine(" click this ImageButton ");
      }

相关问题