将图像从控制页绑定到主页WP7/8

本文关键字:主页 WP7 绑定 图像 控制 | 更新日期: 2023-09-27 18:13:16

我想将图像从控制页绑定到主页,但我无法让它工作。我试试这个:

XAML: <Image Source="{Binding myImage}" Height="150"  Name="photoPreview"...
绑定:

public Image myImage
{
    get;
    set;
}

有什么想法吗?

将图像从控制页绑定到主页WP7/8

您不能将Image对象绑定到图像控件的Source属性。Source性质为ImageSource型。在你的代码中使用BitmapImage并绑定它。

public BitmapImage myImage { get; set; }

或者如果图像文件包含在项目的资源中,您也可以绑定相对路径(作为字符串)

而不是使用Image类型property,你可以通过在Image Source中给出path直接bind image,像这样->

<Image Source = "{Binding Path = path}" Height="150"  Name="photoPreview"...

,其中路径(字符串类型)可以设置&得到

public String path
{
    get;
    set;
}

更多的细节从我的来源也许有人知道问题在哪里>

控件页面(POPUP)

 private void SaveToIsolatedStorage(Stream imageStream, string fileName)
    {
        using (IsolatedStorageFile myIsoStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsoStorage.FileExists(fileName))
            {
                myIsoStorage.DeleteFile(fileName);
            }
            IsolatedStorageFileStream fileStream = myIsoStorage.CreateFile(fileName);
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(imageStream);
            WriteableBitmap mywb = new WriteableBitmap(bitmap);
            mywb.SaveJpeg(fileStream, mywb.PixelWidth, mywb.PixelHeight, 0, 95);
            fileStream.Close();
        }
        this.ReadFromIsolatedStorage("myImage.jpg");
    }
    private void ReadFromIsolatedStorage(string fileName)
    {
        WriteableBitmap bitmap = new WriteableBitmap(200, 200);
        using (IsolatedStorageFile myIsoStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream fileStream = myIsoStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
            {
                bitmap = PictureDecoder.DecodeJpeg(fileStream);
            }
        }
        photoPreview.Source = bitmap;
    }
public String myNote { get; set; }

    public String path
    {
        get;
        set;
    }

控件页面弹出XAML

 <Image Source = "{Binding Path = path}" Height="150" HorizontalAlignment="Right"  Name="photoPreview"

新绑定类命名为Note.cs

公共类注意:INotifyPropertyChanged{

    public String myNote { get; set; }
    public String path
    {
        get;
        set;
    }...
主页

  var note = new Note();
            note.myNote = control.myNote;
            note.OnPropertyChanged("myNote");
            note.path = control.path;
            note.OnPropertyChanged("path");
            Notes.Add(note);
            OnPropertyChanged("Notes");

主页面。xaml

  <Image Width="100" Height="100" Stretch="Fill" Source = "{Binding Path = path}"></Image>

p。

从文本框中绑定文本myNote效果很好,但图像不行。