【问题标题】:Binding image using converter in wp7在 wp7 中使用转换器绑定图像
【发布时间】:2012-11-03 19:16:01
【问题描述】:

我正在使用 VS2012 和 C#(Windows 8.0 SDK)开发 Windows 手机应用程序,我是新手,我正在使用下面的代码使用 Converter 绑定 PNG 图像

xmlns:myproject="clr-namespace:SolutionName.Converters"

 <UserControl.Resources>
    <myproject:LoginHistoryImageConverter x:Key="LoginHistoryImageConverter"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="{StaticResource pivotBackground}">
        <ListBox x:Name="listBoxLogs">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid MinWidth="480" Height="88" Margin="12,0,12,0" HorizontalAlignment="Stretch">
                        <Image Width="80" Height="80" Source="{Binding device, Converter={StaticResource LoginHistoryImageConverter}}"/>
                        <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Margin="85,0,0,0" VerticalAlignment="Center">
                            <TextBlock FontSize="25" Text="{Binding date}" Foreground="{StaticResource MessageListForeground}" HorizontalAlignment="Left"/>
                            <TextBlock FontSize="25" Text="{Binding ip_address}" Foreground="{StaticResource MessageListForeground}" HorizontalAlignment="Left" MaxWidth="200"/>
                        </StackPanel>
                        <StackPanel Orientation="Vertical" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,38,0">
                            <TextBlock FontSize="18" Text="{Binding time}" Foreground="{StaticResource MessageListForeground}" HorizontalAlignment="Right"/>
                            <TextBlock FontSize="18" Text="{Binding location}" Foreground="{StaticResource MessageListForeground}" HorizontalAlignment="Right" MaxWidth="200"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

你可以看到我正在绑定图片

<Image Width="80" Height="80" Source="{Binding device, Converter={StaticResource LoginHistoryImageConverter}}"/>

我得到device 作为字符串“M”或“PC”,通过使用转换器,我返回了BitmapImage

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
        String text = (String)value;                        
        if (text.Equals("M"))
        {
            return new BitmapImage(new Uri("Resources/Assets/mobile.png",UriKind.Relative));
        }
        else
        {
            return new BitmapImage(new Uri("Resources/Assets/pc.png", UriKind.Relative));
        }
 }

当我得到字符串"M" 时,我需要显示mobile 图像否则PC 图像 但是当我导航到我的页面时,它会在"InitializeComponent()" 处触发XamlParseException,请注意图片

即使我尝试了下面的代码来绑定图像,但没有运气

<Image Width="80" Height="80">
    <Image.Source>
        <BitmapImage UriSource="{Binding device, Converter={StaticResource LoginHistoryImageConverter}}"/>
    </Image.Source>
</Image>

我是不是错过了什么。请帮帮我

完整的 XAML 代码

<phone:PhoneApplicationPage
    x:Class="SampleProject.Views.Settings.Logs.LoginHistory"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:myproject="clr-namespace:SampleProject.Converters"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <UserControl.Resources>
        <myproject:LoginHistoryImageConverter x:Key="LoginHistoryImageConverter"/>
    </UserControl.Resources>

        <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="{StaticResource pivotBackground}">
        <ListBox x:Name="listBoxLogs">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid MinWidth="480" Height="88" Margin="12,0,12,0" HorizontalAlignment="Stretch">
                        <Image Width="80" Height="80" Source="{Binding device, Converter={StaticResource LoginHistoryImageConverter}}"/>
                        <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Margin="85,0,0,0" VerticalAlignment="Center">
                            <TextBlock FontSize="25" Text="{Binding date}" Foreground="{StaticResource MessageListForeground}" HorizontalAlignment="Left"/>
                            <TextBlock FontSize="25" Text="{Binding ip_address}" Foreground="{StaticResource MessageListForeground}" HorizontalAlignment="Left" MaxWidth="200"/>
                        </StackPanel>
                        <StackPanel Orientation="Vertical" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,38,0">
                            <TextBlock FontSize="18" Text="{Binding time}" Foreground="{StaticResource MessageListForeground}" HorizontalAlignment="Right"/>
                            <TextBlock FontSize="18" Text="{Binding location}" Foreground="{StaticResource MessageListForeground}" HorizontalAlignment="Right" MaxWidth="200"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

</phone:PhoneApplicationPage>

【问题讨论】:

    标签: c# windows-phone-7 xaml windows-phone-7.1


    【解决方案1】:

    首先,您提供的 XAML 代码存在一些问题。 pivotBackgroundMessageListForeground 资源未定义,但也许您在全局级别定义了它们,例如在 App.xaml 中,在这种情况下应该没问题。

    否则,您的代码应该可以工作。您可以通过检查异常的详细信息来获取有关失败的确切原因的更多信息:当异常发生时,如您在屏幕截图中显示的那样,单击“查看详细信息”,然后展开并检查 Message 的值和InnerException属性:

    【讨论】:

    • 对不起 KooKiz,我现在编辑问题,实际上我引用的是同一个转换器。但没有成功
    • KooKiz,我在下面的代码中遇到了同样的问题,它是简单的滑块代码 但是当我添加 Converter = {StaticResource文本块上的 RoundOffConverter} 它抛出相同的异常,其中 RoundOffConverter 是 Math.round(value)
    • KooKiz,我已经用完整的 xaml 代码编辑了我的问题,请检查一下
    • @RajkumarMandera 编辑了我的答案,希望对您有所帮助
    • 是的,我在 App.xaml 中的全局级别定义了 MessageListForeground 和 pivotBackground,它们加载良好,没有问题。我将检查 InnerException 属性。谢谢KooKiz
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2011-06-13
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多