【问题标题】:Windows phone null image errorWindows phone 空图像错误
【发布时间】:2014-09-25 16:38:58
【问题描述】:

我正在开发一个适用于 windows phone 的应用程序,它基本上包括电话联系人。我正在使用 Contacts 类获取所有电话联系人,并将联系人数据存储在隔离存储中。由于我无法序列化图像,因此我在序列化之前将它们转换为 byte[]。我的代码是:

foreach (var result in e.Results)
{
    if (result.PhoneNumbers.FirstOrDefault() != null)
    {

        BitmapImage bmp2 = new BitmapImage();
        bmp2.SetSource(result.GetPicture());


        listobj.Add(new AddressBook()
           {
               FirstName = result.DisplayName ?? "",
               imageBytes = AddressBook.imageConvert(bmp2),
               EmailAddress = "",
               LastName = "",
               Phone = result.PhoneNumbers.FirstOrDefault().PhoneNumber ?? "",
           });
    }
}

当Contact没有图片时,会在一行显示Argument null异常错误:

bmp2.SetSource(result.GetPicture());

因此,当联系人图像为空时,我想使用一些自定义图像(“/Images/ci2.png”或任何空白图像也可以使用)。 我的 xml 代码是:

<StackPanel Margin="0,0,0,2" Orientation="Horizontal">
    <StackPanel Width="80" Orientation="Horizontal" Height="80">
        <Ellipse Margin="0" Height="70" Width="70" HorizontalAlignment="Left" Stroke="{x:Null}">
            <Ellipse.Fill>
                <ImageBrush Stretch="Fill" ImageSource="{Binding imageByte, Converter={StaticResource BytesToImageConverter}}"/>
            </Ellipse.Fill>
        </Ellipse>
    </StackPanel>
    <StackPanel Height="80" Margin="0" Width="380" HorizontalAlignment="Left">
        <TextBlock FontWeight="Bold"  Text="{Binding FirstName}" FontFamily="Segoe WP Semibold" FontSize="30" VerticalAlignment="Top" Margin="5,0,0,0" HorizontalAlignment="Left" />
        <TextBlock Text="{Binding Phone}" FontFamily="Segoe WP" FontSize="24" Margin="5,0,0,-12" Width="320" HorizontalAlignment="Left" VerticalAlignment="Top">
            <TextBlock.Foreground>
                <SolidColorBrush Color="#FFCFC9C9"/>
            </TextBlock.Foreground></TextBlock>
    </StackPanel>
</StackPanel>

我的问题是,我怎样才能使用自定义图像时

 bmp2.SetSource(result.GetPicture());

为空?谢谢

【问题讨论】:

    标签: c# wpf xaml windows-phone-8


    【解决方案1】:

    看着这么快,你就不能这样做吗:

    if (result.GetPicture() != null)
    {
         bmp2.SetSource(result.GetPicture());
    }
    else
    {
         bmp2.SetSource(Application.GetResourceStream(new Uri(@"Images/ci2.png", UriKind.Relative)).Stream);
    }
    

    如果没有,我已经为此实施了不同的解决方案,并且可能可以发布更多细节。

    【讨论】:

    • 感谢您的回复。上述方法不起作用,然后我删除了除 bmp2.SetSource(Application.GetResourceStream(new Uri(@"/Images/ci2.png", UriKind.Relative)).Stream); 之外的所有代码.Still 我得到同样的错误。看起来这条线根本没有效果。还有什么建议吗?
    • 当我在手机上部署它时,它抛出了一个错误:“system.outofMemoryException”有什么想法吗?
    • 在我的脑海中 - 可能是您的图像转换器代码,但我还要检查 ci2.png 是否已部署到设备并位于正确的位置。你用的是什么设备?是低端的吗?
    • 是的,我使用的是低端设备,这可能就是为什么它在我的模拟器上运行良好但在我的手机上运行良好的原因。有什么办法可以避免这个异常?
    • 提示 - 获取 IsoStoreSpy 以查看设备上的文件:isostorespy.codeplex.com
    【解决方案2】:

    我发现了两个问题。

    <Ellipse.Fill>
        <ImageBrush Stretch="Fill" ImageSource="{Binding imageByte, Converter={StaticResource BytesToImageConverter}}"/>
    </Ellipse.Fill>
    

    你有一个{Binding imageByte},但在 C# 代码中你有

    listobj.Add(new AddressBook()
    {
        FirstName = result.DisplayName ?? "",
        imageBytes = AddressBook.imageConvert(bmp2),
        // ...
    });
    

    imageBytes != imageByte

    除非你没有展示完整的代码。

    就像其他海报所说的,从资源文件中设置它。他的代码会导致错误,因为它包含一个额外的“/”所以将其更改为....

    if (result.GetPicture() != null)
    {
         bmp2.SetSource(result.GetPicture());
    }
    else
    {
         bmp2.SetSource(Application.GetResourceStream(new Uri(@"Images/ci2.png", UriKind.Relative)).Stream);
    }
    

    【讨论】:

    • @Chubosaurus Software 谢谢它的工作,但是当我将它部署到我的手机上时,它抛出了一个错误:“system.outofMemoryException”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多