繁体   English   中英

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

[英]Binding image from control page to main page WP7/8

我想将图像从控制页绑定到主页,但是无法正常工作。 我尝试这样:

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

捆绑:

public Image myImage
{
    get;
    set;
}

这里有什么主意吗?

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

public BitmapImage myImage { get; set; }

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

您可以通过在Image Source提供如下path直接bind image而不是使用Image type property

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

主页。 a

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

PS绑定文本框的文本myNote很好用,但图像却不能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM