WP7 在页面之间传递 XML 数据

发布于 2024-12-12 14:49:48 字数 1466 浏览 0 评论 0原文

只是需要一些指导。让我先发布代码:

XAML:

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel>
            <TextBlock Name="subjectBlock" Text="{Binding SubjectName}" FontSize="26" TextWrapping="Wrap" Foreground="{StaticResource PhoneAccentBrush}" Tap="subjectBlock_Tap" />
            <TextBlock Text="{Binding LecturerName}" FontSize="24" IsHitTestVisible="False" />
            <TextBlock Text=" "/>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

C#代码:

XDocument xmlCourse = XDocument.Load(xmlFile);

var xmlData = from subject in xmlCourse.Descendants("subject")
              select new Subject
              {
                  SubjectID    = subject.Element("subjectID").Value,
                  SubjectName  = subject.Element("subjectName").Value,
                  LecturerName = subject.Element("lecturerName").Value
              };

subjectList.ItemsSource = xmlData;

xmlFile.Close();

...

private void subjectBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{            
    NavigationService.Navigate(new Uri("/eIICS;component/Pages/Coursework.xaml?type=2&subject=" + subjectid, UriKind.Relative));
}

我试图通过点击事件将SubjectID传递到另一个页面。我通过 xml 提取数据,每个单独的主题都有一个 ID,我想知道是否有办法直接/间接传递该 ID?

但我似乎还不能理解它。

一些指导真的很好。谢谢。我自己刚刚开始使用 WP7。

干杯。

just need some guidance. lemme post the codes first:

XAML:

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel>
            <TextBlock Name="subjectBlock" Text="{Binding SubjectName}" FontSize="26" TextWrapping="Wrap" Foreground="{StaticResource PhoneAccentBrush}" Tap="subjectBlock_Tap" />
            <TextBlock Text="{Binding LecturerName}" FontSize="24" IsHitTestVisible="False" />
            <TextBlock Text=" "/>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

C# code:

XDocument xmlCourse = XDocument.Load(xmlFile);

var xmlData = from subject in xmlCourse.Descendants("subject")
              select new Subject
              {
                  SubjectID    = subject.Element("subjectID").Value,
                  SubjectName  = subject.Element("subjectName").Value,
                  LecturerName = subject.Element("lecturerName").Value
              };

subjectList.ItemsSource = xmlData;

xmlFile.Close();

...

private void subjectBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{            
    NavigationService.Navigate(new Uri("/eIICS;component/Pages/Coursework.xaml?type=2&subject=" + subjectid, UriKind.Relative));
}

im trying to pass the SubjectID to another page via the tapped event. im pulling the data via xml, each individual Subject has an ID and im wondering if there is a way to pass that ID directly/indirectly?

but i cant seem to wrap my head around it just yet.

some guidance would be really nice. thanks. just starting out on WP7 myself.

cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-12-19 14:49:49

解决了。感谢 Leo Tse 给我指点。我搜索了更多,发现了这个 - http://mobile. tutsplus.com/tutorials/windows/windows-phone-7-silverlight-tutorial/

因为我使用的是列表框,所以我只想传递主题ID。使用 ListBox.SelectedItem 作为对象效果很好。虽然我不确定这是否很hacky,但它似乎是正确的。

private void subjectBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{   
    var subject = subjectList.SelectedItem as Subject;
    if (null != subject) 
    {
        string subjectid = subject.SubjectID;
        NavigationService.Navigate(new Uri("/eIICS;component/Pages/Coursework.xaml?type=2&subjectid=" + subjectid, UriKind.Relative));
    } 
}

还在学习WP7。到目前为止它已经爆炸了!

SOLVED. Thanks to Leo Tse for giving me pointers. I searched more and I found this - http://mobile.tutsplus.com/tutorials/windows/windows-phone-7-silverlight-tutorial/

Since I was using a ListBox and I just wanted to pass on the SubjectID. Using ListBox.SelectedItem as an object worked well. Though I'm not sure if this is hacky but it seems right.

private void subjectBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{   
    var subject = subjectList.SelectedItem as Subject;
    if (null != subject) 
    {
        string subjectid = subject.SubjectID;
        NavigationService.Navigate(new Uri("/eIICS;component/Pages/Coursework.xaml?type=2&subjectid=" + subjectid, UriKind.Relative));
    } 
}

Still learning WP7. Its been blast so far!

瑾夏年华 2024-12-19 14:49:48

根据您到目前为止所实现的内容,最简单的方法是从文本块对象中检索数据上下文并将其传递到下一页。添加到上面编写的处理程序:

private void subjectBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
  var textblock = sender as TextBlock;
  if(null != textblock) {
      var subject = textblock.DataContext as Subject;
      if(null != subject) {
        string subjectid = subject.SubjectID;
        NavigationService.Navigate(new Uri("/eIICS;component/Pages/Coursework.xaml?type=2&subject=" + subjectid, UriKind.Relative));
      }
    }

但是,我还想指出,这可能不是最好的解决方案。使用事件处理程序来处理所有用户交互可能会使您的代码难以超时维护,并且为事件处理程序编写测试用例确实很困难。您可以考虑使用 MVVMLight 等 MVVM 框架来帮助提高代码的可测试性。该框架附带一个命令系统,可将视图 (XAML) 与代码 (.cs) 解耦。这将为您省去很多麻烦 =)

MVVMLight 项目链接:http://www.galasoft .ch/mvvm/

一个关于指挥框架的帖子:http://blog.galasoft.ch /archive/2009/09/26/using-relaycommands-in-silverlight-and-wpf.aspx

Based on what you have implemented so far, the easiest way is to retrieve the data context from the textblock object and pass it to the next page. Adding to the handler you wrote above:

private void subjectBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
  var textblock = sender as TextBlock;
  if(null != textblock) {
      var subject = textblock.DataContext as Subject;
      if(null != subject) {
        string subjectid = subject.SubjectID;
        NavigationService.Navigate(new Uri("/eIICS;component/Pages/Coursework.xaml?type=2&subject=" + subjectid, UriKind.Relative));
      }
    }

However, I also want to note that this might not be the best solution. Using event handlers to handle all user interactions might make your code hard to maintain overtime, and it is really hard to write test cases for event handlers. You can consider using MVVM frameworks like MVVMLight to help improve the testibility of your code. The framework comes with a commanding system that de-couples the view (XAML) from the code (.cs). It will save you a lot of headaches down the road =)

Link to the MVVMLight project: http://www.galasoft.ch/mvvm/

A post on the commanding framework: http://blog.galasoft.ch/archive/2009/09/26/using-relaycommands-in-silverlight-and-wpf.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文