gpt4 book ai didi

c# - 无法解析资源 'ImageConverter'

转载 作者:太空狗 更新时间:2023-10-30 00:42:54 25 4
gpt4 key购买 nike

我是 Windows Phone 和 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。在这方面我真的需要帮助。

我的代码是:主页.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

关于c# - 无法解析资源 'ImageConverter',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13101652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com