简体   繁体   English

在WP7中使用转换器绑定图像

[英]Binding image using converter in wp7

I'm developing Windows phone apps using VS2012 and C#(Windows 8.0 SDK) and I'm new to it, I'm using the below code to bind PNG image using Converter 我正在使用VS2012和C#(Windows 8.0 SDK)开发Windows Phone应用,但我是新手,我使用下面的代码使用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>

you can observe I'm binding image 你可以观察到我正在绑定图像

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

I'm getting device as string "M" or "PC", by using converter, I returned BitmapImage 我通过使用转换器将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));
        }
 }

when i get string "M" i need to display mobile image else PC image But when i navigate to my page it fires an XamlParseException at "InitializeComponent()" , Please observe image 当我得到字符串"M"我需要显示mobile图像,否则需要显示PC图像。但是,当我导航到页面时,它将在"InitializeComponent()"处触发XamlParseException ,请观察图像 在此处输入图片说明

Even i tried below code for binding image, but no luck 即使我尝试下面的代码来绑定图像,但也没有运气

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

Did I miss something. 我错过了什么。 Please help me 请帮我

Full XAML Code 完整的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>

First, there's a few issues in the XAML code you provided. 首先,您提供的XAML代码中存在一些问题。 The pivotBackground and MessageListForeground resources aren't defined but maybe you defined them at a global level, for instance in the App.xaml , in which case it should fine. 没有定义pivotBackgroundMessageListForeground资源,但是也许您是在全局级别定义它们的,例如在App.xaml ,在这种情况下应该可以。

Otherwise, your code should work. 否则,您的代码应该可以工作。 You can get more information on the precise cause of failure by checking the details of the exception: when the exception occurs, like you showed in your screenshot, click on "View details", then expand and check the value of the Message and InnerException properties: 您可以通过检查异常的详细信息来获取有关失败的确切原因的更多信息:发生异常时(如您的屏幕快照所示),单击“查看详细信息”,然后展开并检查MessageInnerException属性的值:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM