欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

windows phone 7 用户控件页面跳转

程序员文章站 2023-01-23 12:02:11
做项目时遇到一个产品分类展示的页面,于是用pivot来实现,因为每个pivotitem现实的列表模板是一样的,所以就创建了一个用户控件封装一个案例列表,,pivot页面引用用户控件时传输对应数值得到...

做项目时遇到一个产品分类展示的页面,于是用pivot来实现,因为每个pivotitem现实的列表模板是一样的,所以就创建了一个用户控件封装一个案例列表,,pivot页面引用用户控件时传输对应数值得到不同类别的数据.做到这里都没问题,但做案例详细页面时问题出现了.用户控件页面不支持navigationservice.navigate(new uri("/views/caseinfo.xaml", urikind.relative)); 哎,这怎么办呢.网上搜索资料发现原理是这样来解决的  (app.current.rootvisual as phoneapplicationframe).navigate(new uri("/views/caseinfo.xaml?id=" + curcity.id, urikind.relative)); 呵呵,搞定.下面是页面和代码
 
  1.创建用户控件
   
<usercontrol.resources>
        <datatemplate x:key="casetemplate">
            <stackpanel width="436" orientation="horizontal" height="124">
                <stackpanel width="160">
                    <image source="{binding imageuri}" width="150" height="94" horizontalalignment="left" verticalalignment="top" />
                </stackpanel>
                <stackpanel width="285">
                    <textblock text="{binding title}"  fontweight="bold" fontsize="24"/>
                    <textblock text="{binding type}" fontweight="bold"  height="69" textwrapping="wrap"/>
                </stackpanel>
                <textblock text="{binding id}" foreground="#ff140303" visibility="collapsed"/>
            </stackpanel>
        </datatemplate>
    </usercontrol.resources>
 
    <grid x:name="layoutroot" >
        <listbox x:name="caselistbox" itemtemplate="{staticresource casetemplate}" width="440" foreground="white" selectionchanged="caseinfo_selectionchanged" />
        <!--<helpers:popupsplash x:name="pop" />-->
    </grid>

 
 用户控件.cs页面代码
 public int kind { get; set; }
        public caselistcontrol()
        {
            initializecomponent();
        }

        private void usercontrol_loaded(object sender, routedeventargs e)
        {
          
              webclient client = new webclient();
            client.downloadstringcompleted += new downloadstringcompletedeventhandler(client_downloadstringcompleted);
            string url = "aspx?kind">http://localhost/caselist.aspx?kind=" + kind;
            client.downloadstringasync(new uri(url));
        }

        public void client_downloadstringcompleted(object sender, downloadstringcompletedeventargs e)
        {
            if (e.error == null)
            {
                var xml = xelement.parse(e.result);
                var videostemp = (
                    from p in xml.descendants("caselist")
                    select new caselist()
                    {
                        title = p.element("title").value,
                        imageuri = p.element("imageuri").value,
                        logouri = p.element("logouri").value,
                        type = p.element("type").value,
                        id = p.element("id").value
                    }).tolist();

                caselistbox.items.clear();
                videostemp.foreach(p => caselistbox.items.add(p));
                //pop.visibility = visibility.collapsed;
            }
        }

        private void caseinfo_selectionchanged(object sender, selectionchangedeventargs e)
        {
            if (caselistbox.selectedindex != -1)
            {
                caselist curcity = (caselist)caselistbox.selecteditem;

                (app.current.rootvisual as phoneapplicationframe).navigate(new uri("/views/caseinfo.xaml?id=" + curcity.id, urikind.relative));
              
            }
        }


2.pivot(案例分类展示页面)
 <grid x:name="layoutroot" background="transparent">
        <!--枢轴控件-->
        <controls:pivot title="案例共享" name="casepivot" loadingpivotitem="pivot_loadingpivotitem" selectionchanged="casepivot_selectionchanged">
            <!--枢轴项一-->
            <controls:pivotitem header="文化传播">
                <grid>
                </grid>
            </controls:pivotitem>

            <!--枢轴项二-->
            <controls:pivotitem header="医疗">
                <grid>
                
                </grid>
            </controls:pivotitem>
           
            <!--枢轴项三-->
            <controls:pivotitem header="教育">
                <grid>
                  
                </grid>
            </controls:pivotitem>

            <!--枢轴项四-->
            <controls:pivotitem header="*机构">
                <grid/>
            </controls:pivotitem>
           
            <!--枢轴项五-->
            <controls:pivotitem header="建筑规划">
                <grid/>
            </controls:pivotitem>
           
            <!--枢轴项六-->
            <controls:pivotitem header="广告">
                <grid/>
            </controls:pivotitem>

            <!--枢轴项七-->
            <controls:pivotitem header="能源">
                <grid/>
            </controls:pivotitem>

            <!--枢轴项八-->
            <controls:pivotitem header="it">
                <grid/>
            </controls:pivotitem>
        </controls:pivot>
    </grid>


pivot(案例分类展示页面).cs页面代码
private void pivot_loadingpivotitem(object sender, pivotitemeventargs e)
        {
            int kind = convert.toint32(navigationcontext.querystring["kind"]);
            switch (kind)
            {
                case 13:
                    (casepivot.items[0] as pivotitem).content = new caselistcontrol() { kind = 13 };
                    break;
                case 12:
                    (casepivot.items[1] as pivotitem).content = new caselistcontrol() { kind = 12 };
                    break;
                case 11:
                    (casepivot.items[2] as pivotitem).content = new caselistcontrol() { kind = 11 };
                    break;
                case 10:
                    (casepivot.items[3] as pivotitem).content = new caselistcontrol() { kind = 10 };
                    break;
                case 9:
                    (casepivot.items[4] as pivotitem).content = new caselistcontrol() { kind = 9 };
                    break;
                case 8:
                    (casepivot.items[5] as pivotitem).content = new caselistcontrol() { kind = 8 };
                    break;
                case 7:
                    (casepivot.items[6] as pivotitem).content = new caselistcontrol() { kind = 7 };
                    break;
                case 4:
                    (casepivot.items[7] as pivotitem).content = new caselistcontrol() { kind = 4 };
                    break;
                default:
                    break;
            }

           
        }

        private void phoneapplicationpage_loaded(object sender, routedeventargs e)
        {
            int kind = convert.toint32(navigationcontext.querystring["kind"]);
            switch (kind)
            {
                case 13:
                    casepivot.selectedindex = 0;
                    break;
                case 12:
                    casepivot.selectedindex = 1;
                    break;
                case 11:
                    casepivot.selectedindex = 2;
                    break;
                case 10:
                    casepivot.selectedindex = 3;
                    break;
                case 9:
                    casepivot.selectedindex = 4;
                    break;
                case 8:
                    casepivot.selectedindex = 5;
                    break;
                case 7:
                    casepivot.selectedindex = 6;
                    break;
                case 4:
                    casepivot.selectedindex = 7;
                    break;
                default:
                    break;
            }
        }

        private void casepivot_selectionchanged(object sender, selectionchangedeventargs e)
        {
            int item = casepivot.selectedindex;
            switch (item)
            {
                case 0:
                    (casepivot.items[0] as pivotitem).content = new caselistcontrol() { kind = 13 };
                    break;
                case 1:
                    (casepivot.items[1] as pivotitem).content = new caselistcontrol() { kind = 12 };
                    break;
                case 2:
                    (casepivot.items[2] as pivotitem).content = new caselistcontrol() { kind = 11 };
                    break;
                case 3:
                    (casepivot.items[3] as pivotitem).content = new caselistcontrol() { kind = 10 };
                    break;
                case 4:
                    (casepivot.items[4] as pivotitem).content = new caselistcontrol() { kind = 9 };
                    break;
                case 5:
                    (casepivot.items[5] as pivotitem).content = new caselistcontrol() { kind = 8 };
                    break;
                case 6:
                    (casepivot.items[6] as pivotitem).content = new caselistcontrol() { kind = 7 };
                    break;
                case 7:
                    (casepivot.items[7] as pivotitem).content = new caselistcontrol() { kind = 4 };
                    break;
                default:
                    break;
            }

        }


最终效果图:

 windows phone 7 用户控件页面跳转

windows phone 7 用户控件页面跳转


摘自 大眼怪