3

私はこれに対する答えを見つけることができませんでした。

画像パス ("images/myimage.jpg") を含むデータベースがあります。これらのイメージは、SL をホストしている asp.net サイトにもあります。これらの画像を ListBox コントロールにバインドして、画像が表示されるようにします。

文字列値「images/myimage.jpg」があるので、それをビットマップ画像に変換する必要があることを読みました。私はこれをしました:

xaml:

 <Image Source="{Binding ImageFile, Converter={StaticResource ImageConverter}}"/>

ImageConverter クラス:

    public object Convert(object value, Type targetType,
                                  object parameter, CultureInfo culture)
            {
                try
                {
                    Uri source= new Uri(value.ToString());
                    return new BitmapImage(source);
                }
                catch(Exception ex)
                {
                    return new BitmapImage();
                }
            }

URI の作成時に「URI の形式を判別できませんでした」というエラーが表示されます。私は何を間違っていますか?http://localhost:49723/images/myimage.jpgのような Uri を作成すると、問題なく動作します。

「images/myimage.jpg」だけでは機能しないのはなぜですか?

4

5 に答える 5

5

XAPファイルの場所に関係なく機能する単純で動的なアプローチは、次のようになります。

//Get the root path for the XAP
string src = Application.Current.Host.Source.ToString();

//Get the application root, where 'ClientBin' is the known dir where the XAP is
string appRoot = src.Substring(0,src.IndexOf("ClientBin")); 

//Create the image / uri
BitmapImage img = new BitmapImage();
img.UriSource = new Uri(appRoot + "/Images/myImage.png", UriKind.Relative);

これは役に立ちますか?

于 2009-08-05T03:25:25.830 に答える
3

Silverlight のメディアへの相対パスは風変わりなので、WPF パスと同じ (風変わりな) 方法で機能します。相対パスは、アプリのルートではなく、XAP ファイルに対する相対パスです。

トリックの 1 つは、XAP を Web サイトのルートに移動することです。これにより、メディア パスはルートからの相対パスになります。

Silverlight の相対 URI に関する私の投稿を参照してください

于 2009-08-05T01:59:47.740 に答える
1

http://www.silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx

リソースを使用し、画像をコピーしないでください。次に、「SLapplicationName; component / mypathtoimage/image.png」を使用してください。

using System.Windows.Resources;      // StreamResourceInfo
using System.Windows.Media.Imaging;  // BitmapImage
....

StreamResourceInfo sr = Application.GetResourceStream(
    new Uri("SilverlightApplication1;component/MyImage.png", UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.SetSource(sr.Stream); 
于 2010-05-13T16:08:03.460 に答える
1

今日、私自身がこの問題に遭遇し、Jonが上記のように修正しました(ただし、あなたの投稿は表示されませんでした。時間を節約できた可能性があります)。また、オーバーロードを使用することで特定のエラーを解決できることも指摘します。

Uri source = new Uri("Path/Image.jpg", UriKind.Relative);

XAPファイルを移動せずにimagesサブディレクトリにアクセスすることはできませんが、エラーメッセージは解決されます。その時点で、プログラムはコンテンツのない画像を喜んで返し、FiddlerまたはWebDevHelperを使用して実際の問題を把握することができます。

于 2009-08-05T03:35:31.587 に答える
0

完全なサーバー アドレス (protocol://server:port/) を提供するメソッドを単純に記述し、それを使用して絶対 URL を作成できます。

public class Helper{
    public static string ServerAddress{
        get{
            if (_server == "")
                _server = _ServerAddress();
            return _server;
        }
    }

     private static string _ServerAddress(){
        HttpContext ctx = HttpContext.Current;
        if (ctx == null) return "";

        HttpRequest request = ctx.Request;
        if (request == null) return "";

      string srvr = request.ServerVariables["SERVER_NAME"];
      string port = string.IsNullOrEmpty(request.ServerVariables["SERVER_PORT"])?
            "" : ":" + request.ServerVariables["SERVER_PORT"];
      string protocol = "http://";//request.ServerVariables["SERVER_PROTOCOL"];
      return string.Format("{0}{1}{2}{3}", protocol, srvr, port, 
                   request.ApplicationPath);
    }
}    

Converter メソッドの行を次のように変更します。

Uri source= new Uri(value.ToString());

if(!string.IsNullOrEmpty(value.ToString()))
   Uri source= new Uri(Helper.WebAddress + value.ToString());
于 2009-08-05T02:17:31.663 に答える