我使用LINQ將圖像從XML解析爲單個頁面。我想在用戶單擊某個特定按鈕時只顯示一張圖片。問題是列表框一次顯示XML中的所有圖片,而不是我希望顯示的圖片。如何使用NavigationContext.QueryString僅顯示與特定按鈕關聯的圖片?我嘗試過,但它仍然顯示多張照片。使用查詢字符串從XML中檢索圖片
下面是XML文件的樣本:
<?xml version="1.0" encoding="utf-8" ?>
<Exercises>
<Exercise name = "Exercise 1">
<image>/Images/BeginnerEX/ex1.jpg</image>
<audio>/Audio/ex1.mp3</audio>
</Exercise>
<Exercise name = "Exercise 2">
<image>/Images/BeginnerEX/ex2.jpg</image>
<audio>"/Audio/ex2.mp3"</audio>
</Exercise>
<Exercise name = "Exercise 3">
<image>/Images/BeginnerEX/ex3.jpg</image>
<audio>"/Audio/ex3.mp3"</audio>
</Exercise>
</Exercises>
按鈕單擊事件:
private void begButton1_Click(object sender, RoutedEventArgs e)
{
string name = "Exercise 1";
NavigationService.Navigate(new Uri(string.Format("/BeginnerExercisePage.xaml?Exercise={0}", name), UriKind.Relative));
}
我要顯示一個畫面的頁:
public partial class BeginnerExercisePage : PhoneApplicationPage
{
public BeginnerExercisePage()
{
InitializeComponent();
XDocument beginnerExerciseData = XDocument.Load("BeginnerXML.xml");
var data = from query in beginnerExerciseData.Descendants("Exercise")
select new Exercise
{
ExImage = (string)query.Element("image"),
ExAudio = (string)query.Element("audio")
};
lbBegExPage.ItemsSource = data;
}
public class Exercise
{
string image;
string audio;
public string ExAudio
{
get { return audio; }
set { audio = value; }
}
public string ExImage
{
get { return image; }
set { image = value; }
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string name = string.Empty;
if (NavigationContext.QueryString.TryGetValue("name", out name))
{
this.lbBegExPage.ItemsSource = name;
}
}
}
XAML:`這裏是XAML:
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Orientation="Horizontal">
<ListBox x:Name="lbBegExPage">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10">
<Image Source="{Binding ExImage}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
`
我試過了你說的,但是當我把它放入列表框的時候,圖片消失了。 – 2011-12-23 19:16:24
不是。更新你的問題,讓我們看看它的格式。 – 2011-12-23 19:17:21
發佈更新了xaml – 2011-12-23 19:20:47