在webview商店应用中调用Javascript

时间:2013-08-27 10:28:56

标签: c# javascript html microsoft-metro windows-store-apps

我正在使用C#& amp; XAML,在我的webview中我正在加载本地HTML文件, 之后我尝试使用JavaScript进行水平Scrooling HTML。

在此之前,我试图将HTML高度调整到Windows高度。对我来说没什么好事,

我的代码:

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
      StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("split.html");
      string Filecontent = await FileIO.ReadTextAsync(file);
      webView.NavigateToString(Filecontent);
    }
    private async void webView_LoadCompleted(object sender, NavigationEventArgs e)
    {
        StorageFolder folder = Package.Current.InstalledLocation;
        StorageFile file = await folder.GetFileAsync("JavaScript.js");
        string js = await FileIO.ReadTextAsync(file);
        webVIew.InvokeScript("eval", new[] { js });
    }

JavaScript代码:

var h = window.innerHeight, w = window.innerWidth; window.resizeBy(w, h);

我不认为这是一个更好的解决方案,但我对此感到疑惑,如果任何人对此有任何建议,这是有用的。 感谢。

1 个答案:

答案 0 :(得分:0)

我做到了这一点,我做了两件事:

首先,在HTML中包含javascript,可能在head标记内:

<script type="text/javascript" src="JavaScript.js"/>

其次,您不需要致电&#34; eval&#34;。您可以直接调用JavaScript函数,包括多个参数,如果您愿意:

string result = webview->InvokeScript("myScriptName", new string[]{ parameterString });
string result = webview->InvokeScript("scriptTwo", new string[]{ param1, param2 });