Silverlightで画像をバインドするときにURIをフォーマットするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1230771

質問

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

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

文字列値" images / myimage.jpg"があるため、それをBitMap画像に変換する必要があることを読みました。私はこれをしました:

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の形式を判別できませんでした」。何が間違っていますか?次のようなUriを作成した場合: http:// localhost:49723 / images / myimage.jpg 、正常に動作します。

「images / myimage.jpg」だけではない理由動作しますか?

役に立ちましたか?

解決

Silverlightのメディアへの相対パスは奇抜なので、WPFパスと同じ(奇抜な)方法で動作できます。相対パスは、アプリのルートではなく、XAPファイルに相対的です。

1つの秘isは、 XAPをWebサイトのルートに移動するです。メディアパスはルートを基準にします。

Silverlightの相対URIの投稿を参照してくださいこちら

他のヒント

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);

これは役に立ちますか

今日、この問題に自分で遭遇し、Jonが上記の方法で修正しました(ただし、投稿は表示されません。時間を節約できたかもしれません)。特定のエラーはオーバーロードを使用して解決できることも指摘します:

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

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

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

リソース、および画像をコピーしないで、&quot; SLapplicationName; component / mypathtoimage / image.png&quot;

を使用します
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); 

サーバーの完全なアドレス(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);
    }
}    

そしてコンバータのメソッド行を変更します:

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

to

if(!string.IsNullOrEmpty(value.ToString()))
   Uri source= new Uri(Helper.WebAddress + value.ToString());
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top