1
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
    // TODO: Assign a bindable collection of items to this.DefaultViewModel["Items"]

    if (Convert.ToInt32(navigationParameter) >= 0)
    {
        BindData();
        BindData();
        txt1.Text = data[Convert.ToInt32(navigationParameter)].Name;
        Img.Source = data[Convert.ToInt32(navigationParameter)].ImagePath;
    }

我想将 img 源设置为字符串。

4

2 回答 2

1

你可以写

Img.Source = new BitmapImage(new Uri(data[Convert.ToInt32(navigationParameter)].ImagePath));

更新

如果ImagePath是相对路径,您可以这样写:

var path = data[Convert.ToInt32(navigationParameter)].ImagePath;
var uri = new Uri(path, UriKind.Relative);
Img.Source = new BitmapImage(uri);
于 2013-01-05T17:48:05.773 回答
0
myImage.Source = ImageFromRelativePath(this, "relative_path_to_file_make_sure_build_set_to_content");

public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path)
{
    var uri = new Uri(parent.BaseUri, path);
    BitmapImage result = new BitmapImage();
    result.UriSource = uri;
    return result;
}
于 2013-01-10T11:54:03.520 回答