我正在使用 VS2012 和 C#(Windows 8.0 SDK) 开发 Windows 手机应用程序,我是新手,我正在使用以下代码绑定 PNG 图像Converter
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
图像但是当我导航到我的页面时,它会触发XamlParseException
at "InitializeComponent()"
,请观察图像
即使我尝试了下面的代码来绑定图像,但没有运气
<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>