简体   繁体   中英

Passing data between pages wp7

I have a little question. I have a data template like that :

    <phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="DangersItemTemplate">
        <Grid Height="102" Width="447" Tap="Grid_Tap">
            <Image Name="image" Source="{Binding Image}" HorizontalAlignment="Left" Width="90" />
            <TextBlock Name="text" Text="{Binding Nom}" TextWrapping="Wrap" Margin="102,16,16,22"/>
        </Grid>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

I want to use the Textlock control in the CSharp code to pass the Text property to the second xaml page, but I can't find the name of the control, it's inaccessible.

Any idea ?

I just use VB.Net but I am sure you can convert. Just use any shared variable in the application. Here you can see it in an SourceCode example:

http://www.activevb.de/rubriken/kolumne/kol_30/res/nachtscanner.zip

Just use Public Shared MyText As String in Application Class. This vairable you can access anywhere. You can also use INotifyPropertyChanged for that shared property to bind.

Alternatively you can use Navigation Query. Read more here:

How can I pass query string variables with NavigationService.Navigate?

Your binding the Text of the TextBlock so you must have the value in your datacontext. I'd add this value to the Querystirng and navigate to the page.

var text = ((Whatever) DataContext).Nom;
string page2Uri = string.Foramt("/PAge2.xaml?Nom={0}", text);
NavigationService.Navigate(new Uri(page2Uri, UriKind.Relative));

And then to get the data out of the querystring you can use the OnNavigatedTo method in your page, and then inspect this.NavigationContext.QueryString["nom"]

Or have a look into something like the MVVM pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM