I have a listview that contains a webview that contains strings taken from data binding. I want if the webview contains an image, then the height of the webview is adjusted to the height of the image. If it does not contain an image (only text), then the height of the webview is 60.
XAML:
<ListView
Name="ListSingleOption"
Height="auto"
Margin="5,0,10,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Grid
Margin="10,10,10,10"
HorizontalAlignment="Stretch"
Height="auto"
Background="#FFDFF6EE">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<WebView
x:Name="option"
Width="auto"
Height="auto"
Margin="5,5,5,5"
HorizontalAlignment="Stretch"
local:MyProperties.HtmlString="{Binding Name}"
DefaultBackgroundColor="Transparent"
ScriptNotify="option_ScriptNotify"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</Listview>
Code:
string h1= "";
string urlPath = "";
var httpClient = new HttpClient(new HttpClientHandler());
string jsonText = await response.Content.ReadAsStringAsync();
string choiceA = questionObject.ContainsKey("choice_a") && questionObject["choice_a"] != null ? questionObject["choice_a"].GetString() : string.Empty;
h1 = choiceA;
_items = new List<MyClass>();
_items.Clear();
_items.Add(new MyClass() { Name = choiceA });
private async void option_ScriptNotify(object sender, NotifyEventArgs e)
{
WebView wv = (WebView)sender;
if (h1.Contains("height"))
{
var heightString = await wv.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
int height = 0;
if (int.TryParse(heightString, out height))
{
wv.Height = height;
}
}
else
{
wv.Height = 60;
}
}
MyClass.cs
public class MyClass
{
public string Name { get; set; }
public override string ToString()
{
return this.Name;
}
}
Example of JSON:
{"choice_a":"<p><img src="https://ujian.study.id/uploads/questions/image/637ef20551301.jpg" alt="" width="200" height="100" /></p>"}}
But from the code above, if the webview contains images, then the height of the webview will not adjust to the size of the image height. How to handle it?
Note:
I ve tried using option_NavigationCompleted
too, but it still doesn t work