dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.96k stars 1.7k forks source link

WebView inside of a datatemplate #20895

Open Gekidoku opened 6 months ago

Gekidoku commented 6 months ago

Description

I have a view that uses a bindablelayout for ios (since collectionview itemsizing doesnt work properly). Now user generated content had more complex html than just bold and cursive so i decided to switch to using a WebView to render their text. (they want to use UL and OL elements and these render strange on ios)

Now this is my code. The page:

 var myscroll = new ScrollView()
 {
     Orientation = ScrollOrientation.Vertical,
     VerticalOptions = LayoutOptions.FillAndExpand
 };
 var mystack = new VerticalStackLayout()
 {

 }; 

 mystack.SetBinding(BindableLayout.ItemsSourceProperty, "Model");
 BindableLayout.SetItemTemplateSelector(mystack, new InstructionsSelector());
 myscroll.Content = mystack;
 Content = myscroll;

InstructionsSelector:

 //Simplefied
public class InstructionsSelector : DataTemplateSelector
{

    DataTemplate TextTemplate;

    public InstructionsSelector()
    {

        this.TextTemplate= new DataTemplate(typeof(InstructionsMSCell));

    }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        SurveillanceInstructionsCollectionItem roster = item as SurveillanceInstructionsCollectionItem;
        if (roster == null)
        {
            return null;
        }
        else
        {
          return TextTemplate;
        }
    }
}

InstructionsMSCell

 public partial class InstructionsMSCell : ContentView
{

    public InstructionsMSCell()
    {

        InitializeComponent();

        var InstructiesAlarmFrame = new Border()
        {
            FlowDirection = FlowDirection.LeftToRight,
            Style = Application.Current.Resources["BorderStyle"] as Style,
            VerticalOptions = LayoutOptions.Start
        };

        var InstructionAlarmGrid = new Grid();
        InstructionAlarmGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(100, GridUnitType.Star) });
        InstructionAlarmGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
        var graybox = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
            Style = Application.Current.Resources["StackHeaderBox"] as Style,
        };
        var ObjInstructiesLabel = new Label()
        {

            Style = Application.Current.Resources["CarHeaderLabel"] as Style,
        };
        ObjInstructiesLabel.SetBinding(Label.TextProperty, "Name");
        graybox.Add(ObjInstructiesLabel);
        InstructionAlarmGrid.Add(graybox, 0, 0);
        InstructionAlarmGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });

        var MyHtmlView = new WebView() {
            VerticalOptions = LayoutOptions.FillAndExpand
        };
        MyHtmlView.SetBinding(WebView.SourceProperty, "Source");

        InstructionAlarmGrid.Add(MyHtmlView, 0, 1);
        InstructiesAlarmFrame.Content = InstructionAlarmGrid;
        this.Content = InstructiesAlarmFrame;

    }

}

The source of Source

 public string ParsedText => HttpUtility.HtmlDecode(HttpUtility.HtmlDecode(RawText));
public string CleanText 
{
    get 
    {
        if(ParsedText != "" && ParsedText != null)
        {
            var temptext = ParsedText.Replace("rsquo;", "'").Replace("nbsp;", " ").Replace("lsquo;", "'");            
             if(DeviceInfo.Platform == DevicePlatform.iOS)
            {
                if (Application.Current.UserAppTheme == AppTheme.Light || Application.Current.UserAppTheme == AppTheme.Unspecified)
                {
                    temptext = $"<div style=\"font-size:22;font-family:Roboto, sans-serif;color:black\" >{temptext}</div>";

                }
                else
                {
                    temptext = $"<div style=\"font-size:22;font-family:Roboto, sans-serif;color:#f6f6f6\" >{temptext}</div>";
                }
            }
            return temptext;
        }
        else{
            return "";
        }

    }

}
 public HtmlWebViewSource Source
 {
     get
     {
         var item = new HtmlWebViewSource()
         {
             Html = CleanText
         };
         return item;
     }
 }

as far as i can see i have every element set as that it can expand or has automatic height. However the screen looks like this on ios. rn_image_picker_lib_temp_f6372ab6-de76-48c4-a8ba-a3b51d7c6c21

while the second and third block (Alarmopvolging and mobiele surveillance) are way longer than that text wise. when the webview is replaced by a label with its TextType set to Html it does expand its height. however li and ol items are rendered strange on ios

rn_image_picker_lib_temp_493b01de-c366-47e6-beb2-fe821da5e159

Tests with collectionView: On android Screenshot_20240228-144956

On ios its the same as using VerticalStackLayout.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.7 SR2

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

Gekidoku commented 6 months ago

Repro here. https://github.com/Gekidoku/UnfocusTest

The page WebView in collectionpage in the flyout menu

jaosnz-rep commented 5 months ago

Verified on VS for Mac 17.6.10 build 428 (8.0.7), can repro issue with sample project on iOS platform.

Gekidoku commented 5 months ago

Better yet, in release builds on my iPhone SE running ios 17.3 it crashes when the page is loading.