簡體   English   中英

在WP7中使用轉換器綁定圖像

[英]Binding image using converter in wp7

我正在使用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>

你可以觀察到我正在綁定圖像

<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>

首先,您提供的XAML代碼中存在一些問題。 沒有定義pivotBackgroundMessageListForeground資源,但是也許您是在全局級別定義它們的,例如在App.xaml ,在這種情況下應該可以。

否則,您的代碼應該可以工作。 您可以通過檢查異常的詳細信息來獲取有關失敗的確切原因的更多信息:發生異常時(如您的屏幕快照所示),單擊“查看詳細信息”,然后展開並檢查MessageInnerException屬性的值:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM