如何使用Windows Phone中的按钮增加或减少Web浏览器的字体大小?

时间:2013-05-06 07:36:40

标签: javascript windows-phone-7 c#-4.0 xml-parsing

您正在使用下面给出的代码。我想使用A +和A-按钮在Web浏览器上增加或减少文本字体大小。我从xml feed获取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);
    }
}

我正在使用此代码,但没有任何内容可以更改字体大小。请帮助我解决此解决方案中的任何问题。

1 个答案:

答案 0 :(得分: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);
}

查看this post on MSDN.