English 中文(简体)
Adjusting the height of the webview inside the listview
原标题:

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

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签