Can .NET MAUI have a transparent overlay that ignores events but has a child button which accepts events?

huangapple go评论59阅读模式
英文:

Can .NET MAUI have a transparent overlay that ignores events but has a child button which accepts events?

问题

我们在我们的活动内容之上有一个覆盖层,其中包含一个按钮。我们希望在覆盖层的其余部分发生事件时,按钮仍然处于活动状态,并且将事件传递给其后面的活动内容。

层次结构如下

[App]
  [Window]
    [NavigationPage]
      [OurPage]
        [Grid]
          ActiveContent[StackLayout]
            [ContentView]
          Overlay[ContentView]
            [Grid]
              OverlayButton[ImageButton]

我们尝试切换覆盖层上的InputTransparent属性,但这意味着覆盖层内的按钮不会接收事件。iOS具有一些用于视图的点击测试方法以及用于手势识别器的方法,允许此操作。在.NET MAUI中是否有类似的功能?

英文:

We have an overlay on top of our active content which contains a button. We would like the button to be active while events occurring in the rest of the overlay are passed to the active content behind.

The hierarchy looks like

[App]
  [Window]
    [NavigationPage]
      [OurPage]
        [Grid]
          ActiveContent[StackLayout]
            [ContentView]
          Overlay[ContentView]
            [Grid]
              OverlayButton[ImageButton]

We have tried toggling the InputTransparent property on the overlay, however, that means the overlay button within the overlay doesn't receive events. iOS has some hit test methods for views as well as methods for gesture recognizers that would allow this to work. Is there anything similar in .NET MAUI?

答案1

得分: -2

在.NET MAUI中是否有类似的东西?

在MAUI中,您可以尝试将TapGestureRecognizer添加到覆盖的ContentView

<!-- 覆盖内容视图 -->
<ContentView
    Grid.ColumnSpan="2"
    Grid.RowSpan="3"
    BackgroundColor="Transparent"
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand">
    <ContentView.GestureRecognizers>
        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1" />
    </ContentView.GestureRecognizers>

    <Button Clicked="Button_Clicked" HeightRequest="60" WidthRequest="100" />
</ContentView>

我创建了一个演示,并实现了类似的功能,您可以参考以下代码:

MainPage.xaml

<?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="MauiTapApp411.MainPage">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <BoxView Color="Green" />
        <Label Text="Row 0, Column 0"
               HorizontalOptions="Center"
               VerticalOptions="Center" />
        <BoxView Grid.Column="1" Color="Blue" />
        <Label Grid.Column="1" Text="Row 0, Column 1"
               HorizontalOptions="Center"
               VerticalOptions="Center" />

        <BoxView Grid.Row="1" Color="Teal" />
        <Label Grid.Row="1" Text="Row 1, Column 0"
               HorizontalOptions="Center"
               VerticalOptions="Center" />

        <BoxView Grid.Row="1" Grid.Column="1" Color="Purple" />
        <Button Grid.Row="1" Grid.Column="1" Text="Row1, Column 1"
               HorizontalOptions="Center"
               VerticalOptions="Center" />

        <!-- 覆盖内容视图 -->
        <ContentView
             Grid.ColumnSpan="2"
             Grid.RowSpan="3"
             BackgroundColor="Transparent"
             HorizontalOptions="FillAndExpand"
             VerticalOptions="FillAndExpand">
            <ContentView.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1" />
            </ContentView.GestureRecognizers>

            <Button Clicked="Button_Clicked" HeightRequest="60" WidthRequest="100" />
        </ContentView>
    </Grid>
</ContentPage>

MainPage.xaml.cs

public partial class MainPage : ContentPage 
{
    int count = 0;

    public MainPage()
    {
        InitializeComponent();
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        doSomething();
    }

    private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
    {
        doSomething();
    }

    private async void doSomething() {
        await DisplayAlert("Alert", "You have been alerted", "OK");
    }
}

请注意,您可以尝试监听覆盖ContentView的点击事件(例如TapGestureRecognizer_Tapped),并在TapGestureRecognizer_Tapped函数中添加与内部按钮的点击事件相同的必要代码。

private void Button_Clicked(object sender, EventArgs e)
{
    doSomething();
}

private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
{
    doSomething();
}

private async void doSomething() {
    await DisplayAlert("Alert", "You have been alerted", "OK");
}
英文:

> Is there anything similar in .NET MAUI?

On maui, you can try to add TapGestureRecognizer to the overlay ContentView .

    &lt;!--the overlay  --&gt;
&lt;ContentView 
Grid.ColumnSpan=&quot;2&quot;
Grid.RowSpan=&quot;3&quot;
BackgroundColor=&quot;Transparent&quot;
HorizontalOptions=&quot;FillAndExpand&quot;
VerticalOptions=&quot;FillAndExpand&quot;&gt;
&lt;ContentView.GestureRecognizers&gt;
&lt;TapGestureRecognizer Tapped =&quot;TapGestureRecognizer_Tapped&quot; NumberOfTapsRequired=&quot;1&quot;
/&gt;
&lt;/ContentView.GestureRecognizers&gt;
&lt;Button  Clicked=&quot;Button_Clicked&quot;
HeightRequest=&quot;60&quot;  WidthRequest=&quot;100&quot; /&gt;
&lt;/ContentView&gt;

I created a demo and achieved the similar function, you can refer to the following code:

MainPage.xaml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt; 
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
x:Class=&quot;MauiTapApp411.MainPage&quot;&gt;
&lt;Grid&gt;
&lt;Grid.RowDefinitions&gt;
&lt;RowDefinition Height=&quot;*&quot; /&gt;
&lt;RowDefinition Height=&quot;100&quot; /&gt;
&lt;/Grid.RowDefinitions&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition /&gt;
&lt;ColumnDefinition /&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;BoxView Color=&quot;Green&quot; /&gt;
&lt;Label Text=&quot;Row 0, Column 0&quot;
HorizontalOptions=&quot;Center&quot;
VerticalOptions=&quot;Center&quot; /&gt;
&lt;BoxView Grid.Column=&quot;1&quot;
Color=&quot;Blue&quot; /&gt;
&lt;Label Grid.Column=&quot;1&quot;
Text=&quot;Row 0, Column 1&quot;
HorizontalOptions=&quot;Center&quot;
VerticalOptions=&quot;Center&quot; /&gt;
&lt;BoxView Grid.Row=&quot;1&quot;
Color=&quot;Teal&quot; /&gt;
&lt;Label Grid.Row=&quot;1&quot;
Text=&quot;Row 1, Column 0&quot;
HorizontalOptions=&quot;Center&quot;
VerticalOptions=&quot;Center&quot; /&gt;
&lt;BoxView Grid.Row=&quot;1&quot;
Grid.Column=&quot;1&quot;
Color=&quot;Purple&quot; /&gt;
&lt;Button Grid.Row=&quot;1&quot;
Grid.Column=&quot;1&quot;
Text=&quot;Row1, Column 1&quot;
HorizontalOptions=&quot;Center&quot;
VerticalOptions=&quot;Center&quot; /&gt;
&lt;!--the overlay  --&gt;
&lt;ContentView 
Grid.ColumnSpan=&quot;2&quot;
Grid.RowSpan=&quot;3&quot;
BackgroundColor=&quot;Transparent&quot;
HorizontalOptions=&quot;FillAndExpand&quot;
VerticalOptions=&quot;FillAndExpand&quot;&gt;
&lt;ContentView.GestureRecognizers&gt;
&lt;TapGestureRecognizer Tapped =&quot;TapGestureRecognizer_Tapped&quot; NumberOfTapsRequired=&quot;1&quot;
/&gt;
&lt;/ContentView.GestureRecognizers&gt;
&lt;Button  Clicked=&quot;Button_Clicked&quot;
HeightRequest=&quot;60&quot;  WidthRequest=&quot;100&quot; /&gt;
&lt;/ContentView&gt;
&lt;/Grid&gt;
&lt;/ContentPage&gt;

MainPage.xaml.cs

public partial class MainPage : ContentPage 
{
      int count = 0;
      public MainPage()
      {
            InitializeComponent();
      }
private  void Button_Clicked(object sender, EventArgs e)
{
doSomething();
}
private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
{
doSomething();
}
private async void doSomething() {
await DisplayAlert(&quot;Alert&quot;, &quot;You have been alerted&quot;, &quot;OK&quot;);
}
}

Note:

From above code, you can try to listen to the tap event of the overlay contentview(e.g. TapGestureRecognizer_Tapped), and add the necessary code to the function TapGestureRecognizer_Tapped which is the same to the code added to the click event of the inner button.

private  void Button_Clicked(object sender, EventArgs e)
{
doSomething();
}
private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
{
doSomething();
}
private async void doSomething() {
await DisplayAlert(&quot;Alert&quot;, &quot;You have been alerted&quot;, &quot;OK&quot;);
}

huangapple
  • 本文由 发表于 2023年4月11日 05:00:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定