0

嗨,我正在使用下面给出的代码。我想使用 A+ 和 A- 按钮增加或减少 Web 浏览器上的文本字体大小。我正在从 xml 提要获取 html 文件。所以任何人都可以帮助解决这个解决方案。

<phone:WebBrowser x:Name="webBrowser" Height="592"  IsScriptEnabled="True" />
<Button BorderThickness="0"   Margin="0,0,18,0" Height="88" HorizontalAlignment="Right" Width="96" x:Uid="#aPlus" Click="A-_Click" >

private void A-_Click(object sender, RoutedEventArgs e)
{
    if (i > 2)
    {
        webBrowser.FontSize -= 2;
        i++;
        j = i;
    }
}

private void A+_Click(object sender, RoutedEventArgs e)
{
    if (i < 3)
    {
        webBrowser.FontSize += 2;
        i++;
        j = i;
    }
}

<Fullcontent>
    <html>
        <body>
            <p>When worn right, there&rsquo;s nothing quite like gold and Shilpa seems to have pulled off the look in style on Nach Baliye 5. Other stars spotted were Ajay Devgn and Akshay Kumar with Kajal Agarwal promoting their film Special 26. Though Kajal looked pretty in an Anarkali, she could not quite compete with Shilpa.</p>
        </body>
    </html>
</Fullcontent>

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    string selectedIndex = "";
    if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
    {
        webBrowser.NavigateToString(App.CurrentArticle.FullContent);
    }
}

我正在使用此代码,但没有更改字体大小。所以请帮助我如何解决此解决方案。

4

1 回答 1

1

您必须使用 WebBrowser.InvokeScript:

// Initial text size
int textSize = 100; // Percentage

private void A+_Click(object sender, EventArgs e)
{
    textSize *= 2; // Can modify to not increase so much each time
    string szfn = "{styleText = \"body { -ms-text-size-adjust:" + textSize + "% }\";styleTextNode = document.createTextNode(styleText);styleNode = document.createElement(\"style\");styleNode.appendChild(styleTextNode);document.getElementsByTagName(\"head\")[0].appendChild(styleNode);};";
    webBrowser.InvokeScript("eval", szfn);
}

private void A-_Click(object sender, EventArgs e)
{
    textSize /= 2; // Can modify to not decrease so much each time
    string szfn = "{styleText = \"body { -ms-text-size-adjust:" + textSize + "% }\";styleTextNode = document.createTextNode(styleText);styleNode = document.createElement(\"style\");styleNode.appendChild(styleTextNode);document.getElementsByTagName(\"head\")[0].appendChild(styleNode);};";
    webBrowser.InvokeScript("eval", szfn);
}

查看MSDN 上的这篇文章。

于 2013-12-07T22:16:25.533 回答