Silverlight数据绑定图像显示在工具提示中

时间:2012-07-31 19:00:33

标签: c# asp.net silverlight tooltip

这个问题是这个问题的后续问题。 silverlight 4 image preview from tooltip on datagrid

这是我的新问题,我一直试图获得一个工具提示,弹出我的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转换为持有工具提示的silverlight控件?

1 个答案:

答案 0 :(得分:1)

如果您的目标是在工具提示中显示图像(而不是在html窗口中),则以下内容将起作用:

首先是网络服务(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>