Windows Phone 8.1 - Handle WebView vertical scroll by outer ScrollViewer element

后端 未结 3 1377
失恋的感觉
失恋的感觉 2021-01-01 14:58

Problem

I have to show a WebView inside a ScrollViewer in Windows Phone 8.1 application, with the following requirements:

  1. <
3条回答
  •  再見小時候
    2021-01-01 15:36

    I've been facing this problem, and i got some workaround partially (excluding the text selection).

    Basically to be able to use the scrollviewer containing the webview, you need something to be dragged, so I use a rectangle with the height and width same as the webview.

    
        
            
                
                
                
            
            
                
                             
            
                
                    
                    
                
                 
                
            
            
                  
                
            
     
    

    Now we need to inject some css and javascript to make the webview height adapting to the content (I found some code example on another stackoverflow thread, forget to track where it comes from)

    string htmlFragment = @"
                                        
                                            
                                            
                                                
                                            
                                            
                                            
                                                
    " + original + @"
    ";

    Then added webview webscript notify event to determine the rendered height of the webview (and rectangle):

    void WebviewContent_ScriptNotify(object sender, NotifyEventArgs e)
    {
        if (e.Value.Contains("rendered_height"))
        {
            if (valuePair != null && valuePair[0] == "rendered_height")
            {
                double renderedHeight = double.Parse(valuePair[1]);                 
                WebviewContent.Height = renderedHeight;
    
            }
        }
     }
    

    I manage to allow this solution to be able to click some link in the content using this method (the loadLink javascript method):

    private async void RecWeb_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Point p = e.GetPosition(WebviewContent);
            int x = Convert.ToInt32(p.X);
            int y = Convert.ToInt32(p.Y);
            string[] size = { x.ToString(), y.ToString() };
            await WebviewContent.InvokeScriptAsync("loadLink",size);
        }
    

    Maybe you can take it from there to add an event handler for your selecting text problem. The point using this solution is also to find an equivalent event handler in javascript.

提交回复
热议问题