【发布时间】:2013-06-18 09:13:39
【问题描述】:
我正在尝试在 wpf 应用程序中绑定图像。我正在使用 vs2010。
我在下面粘贴代码并解释我做了什么,什么有效,什么无效。
XAML 代码:
<Image Name="newImage" ImageFailed="newImage_ImageFailed" HorizontalAlignment="Right" Width="auto" Height="auto" Margin="5" Source="{Binding imgSource}">
C#代码:
public MainWindow()
{
InitializeComponent();
arraytoImage atim = new arraytoImage();
newImage.DataContext = atim;
}
下面的代码位于不同的命名空间中,其中实现了arraytoImage 类。此类采用 cuda 数组,创建位图,然后使用 memorystream 将其转换为位图图像。现在,我正在为所有像素设置随机颜色,只是为了看看该绑定是否有效。但事实并非如此。下面我粘贴了一个显示图像的代码。
我确信位图图像已正确创建。我认为问题是不正确的绑定。
class arraytoImage : INotifyPropertyChanged
{
// displays images (focused files)
private BitmapImage bitmapImage = new BitmapImage();
private BitmapImage testim = new BitmapImage();
public BitmapImage arraytoImageCon(cuFloatComplex[] dataIn, int wid, int ht)
{
//code that generates bitmapimage
}
public BitmapImage imgSource
{
get { return testim1; }
set
{
if (testim1 != value)
{
testim1 = value;
OnPropertyChanged("imgSource");
}
}
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
编辑:调用arrayToImageCon:
public class ReadRawFiles
{
//Tons of code
public void focusdata()
{
//tons of code
arraytoImage atoi = new arraytoImage();
BitmapImage tmp= atoi.arraytoImageCon(datafft_azi, nazimuth,nrange);
atoi.imgSource=tmp;
}
}
我的问题是,我做错了什么。
提前非常感谢。如果我遗漏了什么,请询问更多细节。
问候
【问题讨论】:
-
你的代码有点乱。你为什么不摆脱 testim1 并直接返回 bitmapImage 。另外你在哪里调用arrayToImage con?为什么不直接在那里调用 setter,以便正确提升属性?
-
试试
test = bitmapImage;而不是testim1 = bitmapImage; -
@dowhilefor 它的混乱,因为我处于学习的初始阶段。请详细一点。谢谢
-
不试试还是不行?^^
-
哈哈。我试过了。我给出了错误。错误:{无法评估表达式,因为当前线程处于堆栈溢出状态。}