簡體   English   中英

WebView GestureRecognition 在 Xamarin 表單中不起作用

[英]WebView GestureRecognition not working in Xamarin forms

我有一個 webview 當這個 webview 被點擊時,我需要讓一個按鈕可見,問題是手勢識別不起作用

我的 Xaml

   <customRenderer:CustomWebView Uri="{Binding SelectedJournal.Uri}" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"  x:Name="CustomWebView" AbsoluteLayout.LayoutBounds="0,50,1,1" AbsoluteLayout.LayoutFlags="SizeProportional"  >
            <customRenderer:CustomWebView.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_OnTapped2" NumberOfTapsRequired="1" ></TapGestureRecognizer>
            </customRenderer:CustomWebView.GestureRecognizers>
        </customRenderer:CustomWebView>

        <customRenderer:NavigationImageButton ItemTapped="FullScreenOnTapped" Source="full.jpg" AbsoluteLayout.LayoutBounds="0.5,1,-1,-1" AbsoluteLayout.LayoutFlags="PositionProportional"  HeightRequest="60" WidthRequest="60" x:Name="FullScreenBtn" IsVisible="False" >

在后面的代碼中,我是這樣調用的

private void TapGestureRecognizer_OnTapped2(object sender, EventArgs e)
    {
        FullScreenBtn.IsVisible = true;
    }

這應該有效,但這不起作用

還有我的自定義渲染 web 視圖類

  public class CustomWebView : WebView
{
    public static readonly BindableProperty UriProperty = BindableProperty.Create<CustomWebView, string>(p => p.Uri, default(string));

    public string Uri
    {
        get { return (string)GetValue(UriProperty); }
        set { SetValue(UriProperty, value); }
    }
}

如何實現這一目標

您可以在視圖中使用“焦點”事件來顯示按鈕,而無需在Web視圖上使用手勢識別器。 您可以執行以下操作:

var wb = new WebView
{
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand,
    Source = "https://stackoverflow.com/questions/56320611/webview-gesturerecognition-not-working-in-xamarin-forms",
};

wb.Focused += (s, e) =>
{
   //Handle your logic here!
   wb.Unfocus();
 };

在這里,如果您希望在每次點擊Webview時都實現邏輯,則使用Unfocus()。

我的應用程序中使用WebView來顯示格式化文本。 點擊文本應該打開一個編輯器來修改文本。 建議的Focused事件適用於 Android 但不適用於 iOS(至少從 iOS 15.0 模擬器開始)。 我在https://bugzilla.xamarin.com/42/42596/bug.html#c4的評論中找到了解決方案。 向 xaml 添加一個空標簽作為WebView的透明覆蓋,注冊點擊事件並啟用手勢識別器。

<Grid HeightRequest="132">
    <WebView Grid.Column="0" Grid.Row="0"
             Source="{Binding FormattedDescription, Converter={tr:StringToWebViewSourceConverter}}"
             HeightRequest="132"/>
    <Label Grid.Column="0" Grid.Row="0" Text=""
           HeightRequest="132" HorizontalOptions="Fill">
        <Label.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding EditDescriptionCommand}"/>
        </Label.GestureRecognizers>
    </Label>
</Grid>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM