英文:
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 .
<!--the overlay -->
<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>
I created a demo and achieved the similar function, you can refer to the following code:
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" />
<!--the overlay -->
<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");
}
}
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("Alert", "You have been alerted", "OK");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论