Silverlight 为什么以前渲染图像时不显示它?

Silverlight 为什么以前渲染图像时不显示它?,silverlight,windows-phone-7,bitmap,windows-phone-7.1,Silverlight,Windows Phone 7,Bitmap,Windows Phone 7.1,从新创建的项目开始,我向主页添加一个按钮,并在单击处理程序中执行以下操作: 我创建一个图像并指定一个BitmapImage作为其源。然后将图像添加到LayoutRoot。我的期望是在单击按钮后在GUI中看到图像 现在有一个转折点:我还想把这个图像渲染成一个可写的位图。因此,我创建了这样一个位图,并调用它的Render方法来渲染图像 问题是:当我注释掉Render调用时,我立即看到图像出现在我的主页上。当我包含渲染调用时,图像不会出现在第一次按钮单击上,而是出现在第二次按钮单击上。为什么? 代码如

从新创建的项目开始,我向主页添加一个按钮,并在单击处理程序中执行以下操作:

我创建一个图像并指定一个BitmapImage作为其源。然后将图像添加到LayoutRoot。我的期望是在单击按钮后在GUI中看到图像

现在有一个转折点:我还想把这个图像渲染成一个可写的位图。因此,我创建了这样一个位图,并调用它的Render方法来渲染图像

问题是:当我注释掉Render调用时,我立即看到图像出现在我的主页上。当我包含渲染调用时,图像不会出现在第一次按钮单击上,而是出现在第二次按钮单击上。为什么?

代码如下:

    private void button1_Click(object sender, RoutedEventArgs e)
    {

        WriteableBitmap wbmp = new WriteableBitmap(62, 62);
        BitmapImage bmp = new BitmapImage(new Uri("ApplicationIcon.png", UriKind.Relative));
        Image img = new Image() { Width = 62, Height = 62, Source = bmp };

        wbmp.Render(img, null); // <------ this line makes the difference

        LayoutRoot.Children.Add(img);
    }
试试这个

 private Image _img = new Image ();
    public Image img
    {
        get
        {
            return _img;
        }
    }
 public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String PropertyName)
    {
        if (null != PropertyChanged)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }
 private void button1_Click(object sender, RoutedEventArgs e)
{

    WriteableBitmap wbmp = new WriteableBitmap(62, 62);
    BitmapImage bmp = new BitmapImage(new Uri("ApplicationIcon.png", UriKind.Relative));
    Image img = new Image() { Width = 62, Height = 62, Source = bmp };

    wbmp.Render(img, null); // <------ this line makes the difference

    LayoutRoot.Children.Add(img);

      NotifyPropertyChanged("Image");
}
这应该行得通。。请再试一次…

使用渲染时可能会遇到以下问题之一:

调用此方法后,必须调用Invalidate才能呈现 位图

此方法不支持不属于的UIElement对象 视觉树。您需要致电测量并安排 调用之前,不在可视树中的UIElement对象 渲染


你说得对,我没有听懂这些话。但是这些都是关于渲染的功能,不应该影响下面添加到可视化树中的内容,对吗?我现在对渲染的结果不感兴趣。只是这个电话似乎对下一个电话有副作用。我还尝试在render调用之前添加Measure和Arrange,并在其之后添加Invalidate。行为没有更改。然后尝试使用INotifyPropertyChange。。可能图像已更新,但由于未通知用户界面已发生更改,因此未看到图像。。因此,更新仅在第二次单击时显示。。