2015-11-23 97 views
1

我正在查看教程this link关于在Web应用程序中显示PowerBI图块的教程。我想在C#XAML应用程序中做类似的事情。在C#XAML应用程序中显示PowerBI图块

我能够检索有关仪表板和图块(包括embedUrl)的所有信息,并且我将embedUrl作为源传递给我的XAML应用中的WebView元素,但我没有看到任何显示在的WebView。

我知道我错过了某些东西 - “步骤2 - 提交身份验证令牌”中提到的部分 - accesstoken在JavaScript函数postActionLoadTile中返回的位置。

更具体地说,该链路上的例子有一定的HTML/JS代码,像一个web应用程序如下:

iframe.src = embedTileUrl + "&width=" + width + "&height=" + height; 

iframe.onload = postActionLoadTile; 

// post the auth token to the iFrame. 
function postActionLoadTile() { 
    // get the access token. 
    accessToken = document.getElementById('MainContent_accessTokenTextbox').value; 

    // return if accessToken is empty 
    if ("" === accessToken) 
     return; 

    var h = height; 
    var w = width; 

    // construct the push message structure 
    var m = { action: "loadTile", accessToken: accessToken, height: h, width: w}; 
    message = JSON.stringify(m); 

    // push the message. 
    iframe = document.getElementById('iFrameEmbedTile'); 
    iframe.contentWindow.postMessage(message, "*");; 
} 

我试图做类似的事情在使用以下代码XAML:

XAML:

<WebView x:Name="myWebView" FrameDOMContentLoaded="myWebView_FrameDOMContentLoaded"/> 

C#:

var str = string.Format(@"<iframe width='{0}' height='{1}' src='{2}'></iframe>", 
      myWebView.ActualWidth - 10, 
      myWebView.ActualHeight - 10, 
      struri); 

     myWebView.NavigateToString(str); 

但我不知道如何超越这一点。我会复制C#代码中JavaScript代码中发生的所有事情。

任何帮助,将不胜感激

+0

是否有错误?你的代码准确地发送到服务器是什么?它是否正确认证?你用小提琴来追踪结果吗? – WiredPrairie

+0

首先,在您的WebView中放置一个iframe可能没有意义。其次,你没有初始化瓷砖。第三,你没有展示你如何检索认证令牌。 如果您查看JS代码,它将导航iframe到嵌入网址,然后*它将消息发布到iframe /窗口,这可能是嵌入代码正在等待。该消息告诉它该做什么(以特定维度加载瓦片),并为其提供认证令牌。你似乎没有这样做。 –

+0

我正在评估Power BI。感谢您发布此问题。它突出了什么是混乱的事情。欢迎使用适用于企业开发人员的数据可视化和报告工具,要求您作为企业开发人员完全放弃您的XAML/VB/C#技能,恢复为javascript/typescript/css/nodejs并跳过这些环节与典型企业应用程序集成。闻起来像一个不了解他们的客户负责Power BI产品开发的人。这充其量令人失望。你解决了什么解决方案? –

回答

2

这听起来像你已经找到了如何让OAuth凭证,并得到一个嵌入网址的瓷砖。

我研究了如何通过在WebView内容中包含一些JS脚本并使用WebView.InvokeScriptAsync方法来调用脚本的概念的基本证明。

这里是我的解决方案的重要位的要点:https://gist.github.com/itsananderson/14a179174ea65e246ce7

这里是一个快速摘要:

private void SetEmbedUrl(string embedUrl) 
{ 
    myWebView.NavigateToString("<iframe id=\"iFrameEmbedTile\" src =\"" 
     + embedUrl + 
     "\" style=\"width: 400px; height: 405px\"></iframe>" + 
     "<script src=\"ms-appx-web:///postActionLoadTile.js\"></script>"); 
} 

private void myWebView_FrameDOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args) 
{ 
    await myWebView.InvokeScriptAsync("postActionLoadTile", new[] { myTextBox.Text, "400", "400" }); 
} 

这涉及以下postActionLoadTile.js文件,你应该添加到您的项目。

function postActionLoadTile(accessToken, h, w) { 
    // Generate a message for the iframe 
    var m = { action: "loadTile", accessToken: accessToken, height: h, width: w }; 
    var message = JSON.stringify(m); 

    // Push the message to the iframe 
    var iframe = document.getElementById('iFrameEmbedTile'); 
    iframe.contentWindow.postMessage(message, "*"); 
} 
+0

嗨如何将它移植到带有webBrowserContol的Windows Forms应用程序。我试过了。但我没有得到任何东西 – user2129013

相关问题