开发者

Windows phone 7, grabbing listboxitem name and passing it to another xaml page

开发者 https://www.devze.com 2023-03-19 10:46 出处:网络
I have two XAML pages. One page (named Page1)has a listbox named \"strangers\" and within this ListBox, ListBoxItems.

I have two XAML pages. One page (named Page1) has a listbox named "strangers" and within this ListBox, ListBoxItems.

My second page is named: Page2.

Whenever someone taps a ListBoxItem from Page1, the name of the ListBoxItem is transferred over to the other XAML page, Page2, where Page2 will read the ListboxItem name, and then based on the LisBoxItem name, will populate the page with text.

By the way, I want to go through hundreds of listboxitems all calling the same function General_MouseLeftButtonDown, and dynamically changing the content of the page based on the name of the listboxitem that was clicked.

All the content will be in the phone upon launch.

However there seems to be a problem -- I cannot get this to function. Here is my code, and hopefully you can all help.

Page1 Xaml info:

<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# info:

    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.OnN开发者_开发百科avigatedTo(e);

        object something="";

        if (phoneAppService.State.ContainsKey("theperson"))
        {
            if (phoneAppService.State.TryGetValue("theperson", out something))
            {
                string namers = something.ToString();
                Textblock.Text = namers;
            }
        }
    }

However when I run this, the error I get is after I click the listboxitem to transition pages. I get the following error:

NullReferenceException

on the

namers= (sender as ListBoxItem).Name.ToString();

Am I doing this XAML data transfer correctly? Obviously something is wrong, but I cannot quite grasp what or why.


You should use QueryStrings instead of State. The latter is designed to be used when your app gets tombstoned and you want to return to the same page without losing any information that was entered. QueryStrings are used to transport data, in a string format, across to another page.

So, Page 1 C#:

private void General_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var namers="";

    namers= (sender as ListBoxItem).Name.ToString();



    NavigationService.Navigate(new Uri("/Page2.xaml?name=" + namers, UriKind.Relative));
}

Page 2

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    string namers="";

    NavigationContext.QueryString.TryGetValue("name", out namers)

    Textblock.Text = namers;

}

What I've done in Page 2 is retrieve the value of name (which is what I used as the key in the querystring, but you can use anything). Using TryGetValue, instead of directly checking the index (NavigationContext.QueryString["name"]) means that if it fails, it will return a false instead of throwing an exception. You can check the returned bool value if you want, but there doesn't seem to be a need since namers is already initialised to "".

0

精彩评论

暂无评论...
验证码 换一张
取 消