从listview获取每个文本块

时间:2015-05-25 09:14:39

标签: c# xaml

我正在构建# -*- coding: utf-8 -*- import urllib2 import urllib import cookielib from bs4 import BeautifulSoup import datetime import time from urlgrabber.keepalive import HTTPHandler def get_ViewState(soup): view_input = soup.find(id="__VIEWSTATE") return (view_input['value']) def get_EventValidation(soup): event_input = soup.find(id="__EVENTVALIDATION") return event_input['value'] cookie = cookielib.CookieJar() keepalive_handler = HTTPHandler() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie),keepalive_handler) urllib2.install_opener(opener) __url = 'http://url.of.the.page' opener.addheaders = [('User-agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36') ,('Connection', 'Keep-Alive') ,('Referer',__url)] page = urllib.urlopen(__url).read() soup = BeautifulSoup(page) viewstate = get_ViewState(soup) eventvalidation = get_EventValidation(soup) postdata = urllib.urlencode({ '__EVENTTARGET':'', '__EVENTARGUMENT:':'', '__VIEWSTATE':viewstate, 'TxtStudentId':'xxxxxxx', 'TxtPassword':'xxxxxxx', 'BtnLogin':'login', '__EVENTVALIDATION':eventvalidation }) req = urllib2.Request( url = __url, data = postdata ) result = opener.open(req) print result.read() # result = opener.open(req) # print result.info() # print result # print result.read() print "------------------------------------------------" #after login, I need to get the scores table __queryUrl = 'http://url.of.the.page?key=0' now = datetime.datetime.now() opener.addheaders = [('Referer', 'http://url.of.the.page?i='+now.strftime('%H:%M:%S'))] result = opener.open(__queryUrl) print result.read() for item in cookie: print 'Cookie:Name = '+item.name print 'Cookie:Value = '+item.value 个应用,并且我试图从我的UWP获取每个文本块。

这是我的listview

listview

这是我尝试这样做的:

                <ListView Grid.Row="1" BorderBrush="#0062AD" BorderThickness="1" ItemsSource="{Binding BusRoutes}" x:Name="Routes1" SelectionMode="None"   IsItemClickEnabled="False"  Padding="0 10 0 0">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid Width="400">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TextBlock x:Name="startingPoint" Grid.Row="0" Grid.Column="0"  Padding="0 10 0 10" TextAlignment="Center" HorizontalAlignment="Center" Text="{Binding hours}"/>
                            <TextBlock TextAlignment="Center"  Grid.Row="0" Grid.Column="1"  Padding="0 10 0 10" x:Name="endingPoint" HorizontalAlignment="Center" Text="{Binding hours2}" />
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

在我的ItemClick事件中,我做到了这一点:

    public T FindElementByName<T>(DependencyObject element, string sChildName) where T : FrameworkElement
    {
        T childElement = null;
        var nChildCount = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < nChildCount; i++)
        {
            FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

            if (child == null)
                continue;

            if (child is T && child.Name.Equals(sChildName))
            {
                childElement = (T)child;
                break;
            }

            childElement = FindElementByName<T>(child, sChildName);

            if (childElement != null)
                break;
        }
        return childElement;
    }

我正在 this.UpdateLayout(); for (int i = 0; i < Routes1.Items.Count; i++) { var container = this.Routes1.ContainerFromIndex(i); TextBlock endingPoint = FindElementByName<TextBlock>(container, "endingPoint"); endingPoint.Visibility = Visibility.Collapsed; } for (int i = 0; i < Routes1.Items.Count; i++) { var container = this.Routes1.ContainerFromIndex(i); TextBlock startingPoint = FindElementByName<TextBlock>(container, "startingPoint"); startingPoint.SetValue(Grid.ColumnSpanProperty, 2); } 。奇怪的是,当我试图拿一个System.ArgumentException例如textblock

我可以正常获得第一个文本块。为什么会这样?

我忘了提到这条例外情况爆发了:

var container = this.Routes1.ContainerFromIndex(0);

0 个答案:

没有答案