私はVS2012とC#(Windows 8.0 SDK)を使用してWindows phoneアプリを開発していますが、私はそれに慣れていません。以下のコードを使用して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>
I'm binding image を観察できます
<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
elsePC
画像しかし、自分のページに移動すると、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>