2012-07-31 106 views
0

這個問題是來自這個問題的後續。 silverlight 4 image preview from tooltip on datagrid工具提示中顯示的Silverlight數據綁定圖像

這是我的新問題,我一直在試圖獲得一個工具提示,以彈出我的Silverlight應用程序的搜索結果中返回的文檔預覽。我已經鏈接了圖像,它出現了正確的圖像,但是它會在新的或單獨的窗口中打開,而不是在工具提示本身中打開。這裏是我的代碼背後..

private void PPTImageToolTip(object sender, RoutedEventArgs e) 
    { 
     string docname = ((FrameworkElement)sender).DataContext.ToString(); 
     string baseUri = "http://localhost:58904/ShowDocument.aspx?DocumentName=" + docname + "-ppt" + "&type=jpg"; 
     var hostingWindow = HtmlPage.Window; 
     hostingWindow.Navigate(new Uri(baseUri, UriKind.Absolute), "_parent"); 
    } 

這是設置去負責處理此操作我ShowDocument.aspx網頁..

else if (File.Exists(strFullFilePath) && sType == "jpg") 
        { 
         fileStream = new FileStream(strFullFilePath, FileMode.Open, FileAccess.Read); 
         buffer = new byte[fileStream.Length]; 
         fileStream.Read(buffer, 0, Convert.ToInt32(fileStream.Length)); 
         try 
         { 
          Response.ClearHeaders(); 
          Response.ClearContent(); 
          Response.ContentType = "image/jpeg"; 
          Response.BinaryWrite(buffer); 

         } 
         catch (Exception ex) 
         { } 
        } 

我意識到它轉移到另一個頁面',但我沒有能夠獲得該圖像或該頁面顯示在工具提示本身而不是填充新窗口。這是因爲我的HtmlPage.window代碼?或者因爲ShowDocument.aspx頁面已經被調用並且不能回調?是否有一個可行的解決方案,讓圖像在工具提示內部填充?或者有沒有辦法將repsonse.redirect轉換爲持有tooltip的silverlight控件?

回答

1

如果你的目的是要顯示工具提示內的圖像(而不是HTML窗口),下面的工作:

首先,Web服務(ASHX)

public class MyHandler : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     String fileName = @"c:\PathToMyFile\Myfile.jpg"; 
     using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) 
     { 
      var buffer = new byte[fileStream.Length]; 
      fileStream.Read(buffer, 0, Convert.ToInt32(fileStream.Length)); 
      context.Response.ContentType = "image/jpeg"; 
      context.Response.BinaryWrite(buffer); 
     } 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

然後調用從Silverlight客戶端服務:

public partial class MainPage : UserControl 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     WebClient client = new WebClient(); 
     client.OpenReadCompleted += (s, e) => 
      { 
       using (Stream stream = e.Result) 
       { 
        BitmapImage img = new BitmapImage(); 
        img.SetSource(stream); 

        // Update MyImage.Source. Use the Dispatcher to ensure this happens on the UI Thread 
        Dispatcher.BeginInvoke(() => 
         { 
          MyImage.Source = img; 
         }); 

       } 
      }; 
     client.OpenReadAsync(new Uri(String.Format(BaseURL + "MyHandler.ashx"))); 
    } 
} 

最後的XAML的觀點:

<Border x:Name="MyBorder" Width="100" Height="100" Background="Black"> 
     <ToolTipService.ToolTip> 
      <Image x:Name="MyImage" /> 
     </ToolTipService.ToolTip> 

</Border> 
+0

我將此標記爲答案,因爲它將我設置在正確的方向,但我對該行感到困惑//必須在UI線程部分設置MyImage.Source?我需要爲那部分做些什麼? – jcc 2012-08-01 15:23:15

+0

如果您嘗試在不使用Dispatcher的情況下設置「MyImage.Source = img;」,則會得到一個跨線程訪問異常。所以你不需要做比樣本更多的事情,只是不要刪除Dispatcher位。我已經更新了我的答案,以澄清這一點。 – 2012-08-01 17:22:05

+0

好吧,我玩了這個,最終發現了,但我現在的主要問題是,我的'MyImage'嵌套在我的網格深處,代碼隱藏無法找到它,只能找到它的列in。 – jcc 2012-08-01 17:28:56