【发布时间】:2011-07-10 23:38:12
【问题描述】:
我有两个 XAML 页面。一页(名为 Page1)有一个名为“strangers”的列表框,在该列表框内有 ListBoxItems。
我的第二个页面被命名为:Page2。
每当有人从 Page1 中点击 ListBoxItem 时,ListBoxItem 的名称就会转移到另一个 XAML 页面 Page2,Page2 将在其中读取 ListboxItem 名称,然后根据 LisBoxItem 名称,用文本填充页面。
顺便说一下,我想遍历数百个listboxitem,它们都调用同一个函数General_MouseLeftButtonDown,并根据被点击的listboxitem的名称动态改变页面的内容。
所有内容将在启动时出现在手机中。
但是似乎有一个问题——我无法让它发挥作用。这是我的代码,希望对大家有所帮助。
Page1 Xaml 信息:
<ListBox x:Name="Strangers" Margin="0,0,-12,0">
<ListBoxItem x:Name="Peter">
<StackPanel Orientation="Horizontal" Margin="0,0,0,17" MouseLeftButtonDown="General_MouseLeftButtonDown">
</StackPanel>
</ListBoxItem>
Page1 C# 信息:
PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
private void General_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var namers="";
namers= (sender as ListBoxItem).Name.ToString();
phoneAppService.State["theperson"] = namers;
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
}
Page2 C#:
PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
object something="";
if (phoneAppService.State.ContainsKey("theperson"))
{
if (phoneAppService.State.TryGetValue("theperson", out something))
{
string namers = something.ToString();
Textblock.Text = namers;
}
}
}
但是,当我运行它时,我得到的错误是在我单击 listboxitem 以转换页面之后。我收到以下错误:
NullReferenceException
关于
namers= (sender as ListBoxItem).Name.ToString();
我是否正确执行此 XAML 数据传输?显然出了点问题,但我不能完全理解是什么或为什么。
【问题讨论】:
标签: c# windows-phone-7 transition isolatedstorage