2015-11-23 32 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

相關問題