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 source设置为字符串。
答案 0 :(得分: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);
答案 1 :(得分: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;
}