简体   繁体   English

在页面之间传递数据WP7

[英]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. 我想在CSharp代码中使用Textlock控件将Text属性传递给第二个xaml页面,但是我找不到该控件的名称,它无法访问。

Any idea ? 任何想法 ?

I just use VB.Net but I am sure you can convert. 我只是使用VB.Net,但我相信您可以进行转换。 Just use any shared variable in the application. 只需在应用程序中使用任何共享变量即可。 Here you can see it in an SourceCode example: 在这里,您可以在SourceCode示例中看到它:

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

Just use Public Shared MyText As String in Application Class. 只需在应用程序类中使用“ Public Shared MyText As String This vairable you can access anywhere. 您可以在任何地方访问此可变项。 You can also use INotifyPropertyChanged for that shared property to bind. 您也可以使用INotifyPropertyChanged绑定该共享属性。

Alternatively you can use Navigation Query. 或者,您可以使用导航查询。 Read more here: 在这里阅读更多:

How can I pass query string variables with NavigationService.Navigate? 如何通过NavigationService.Navigate传递查询字符串变量?

Your binding the Text of the TextBlock so you must have the value in your datacontext. 您绑定了TextBlock的Text,因此您必须在datacontext中具有该值。 I'd add this value to the Querystirng and navigate to the page. 我将此值添加到Querystirng并导航到页面。

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"] 然后,要从查询字符串中获取数据,可以在页面中使用OnNavigatedTo方法,然后检查this.NavigationContext.QueryString [“ nom”]

Or have a look into something like the MVVM pattern. 或者看看类似MVVM模式的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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