我是Windows手机和Silverlight开发的新手.在我的学习练习中,我遇到了一个错误,这是我在这篇文章的标题上提到的.
我的主要目标是将图像文件保存并检索到SQLCE数据库,我使用了本教程http://antonswanevelder.com/2011/10/28/writing-an-image-to-sql-ce-linq-to-sql/
但是,我对此代码段有疑问
< Image Source =“{Binding ItemImage,Converter = {StaticResource ImageConverter}}”Stretch =“UniformToFill”/>
我的想法是编译器找不到资源ImageConverter.我真的需要一个帮助.
我的代码是:
MainPage.xaml中
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="CallListListBoxItemTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding CallName}" Foreground="DarkCyan" FontSize="{StaticResource PhoneFontSizeLarge}"
VerticalAlignment="Top" Margin="12,12,0,0"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="PersonalInfoListBoxItemTemplate">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image Source="{Binding PersonImage, Converter={StaticResource ImageConverters}}" Stretch="UniformToFill" Name="_personPhoto" />
MainPage.xaml.cs中
public class ImageConverters : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is byte[])
{
MemoryStream ms = new MemoryStream(value as byte[]);
WriteableBitmap wb = PictureDecoder.DecodeJpeg(ms, 100, 100);
return wb;
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
解决方法:
让我们考虑您的值转换器位于ProjectName.Converters命名空间中.
在xaml中,您需要添加命名空间:
<phone.PhoneApplicatinPage
.. all your code here
xmlns:converters="clr-namespace;ProjectName.Converters"
>
并在Resources标签中:
<phone:PhoneApplicationPage.Resources>
<converters:ImageConverters x:Key="ImageConverter"/>
<!- your DataTemplates here-->
和小教程让你更熟悉IValueConverter here
原文地址:https://codeday.me/bug/20190613/1231359.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。