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源设置为字符串。
你可以写
Img.Source = new BitmapImage(new Uri(data[Convert.ToInt32(navigationParameter)].ImagePath));
UPDATE
如果ImagePath
是一个相对路径,你可以这样写:
var path = data[Convert.ToInt32(navigationParameter)].ImagePath;
var uri = new Uri(path, UriKind.Relative);
Img.Source = new BitmapImage(uri);
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;
}