热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

Accordion滑动边栏高效率版

发布时间:2010年03月17日点击数: 未知

主要功能

1.增加登录用户判断

2.增加虚拟目录判断

3.效率显著提高

今天主要考虑到虚拟目录,SL暂时不支持虚拟目录,也就是如果超链接是相对路径,就会报错找不到路径,其实是SL不能自动添加虚拟目录这一路径,所以我在调用.XAP宿主页面时加了查询参数(?username=swk&second=jyoa),这里second=jyoa是虚拟目录名称,至于用户登录问题,SL不支持session,不过支持cookie,我这里用到的也是网址传递参数,用ASP.NET获取用户ID然后传递给SL给。效率比昨天的快很多,希望微软尽快支持联合查询,最好支持ADO.NET,至于界面修改方面有问题的可以参看微软的修改模板教程,其实修改界面我也不是太懂,很多朋友在问,不好意思了,这个我真的不怎么会...呵呵,有什么问题,或者有什么好的想法可以加我,大家一起讨论,毕竟是新技术,自己学起来很是郁闷...

CS文件代码:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Xml;  
  14. using System.Xml.Linq;  
  15. using navigate.navigateServes;  
  16. using System.Data.Services.Client;  
  17. using System.Windows.Browser;  
  18. namespace navigate  
  19. {  
  20.     public partial class MainPage : UserControl  
  21.     {  
  22.         int num = 0;  
  23.         int summun = 0;  
  24.         List<Models> ret = new List<Models>();  
  25.         navigateDataEntities svcContext;  
  26.         IDictionary<String, String> paras = null;  
  27.         public MainPage()  
  28.         {  
  29.             InitializeComponent();  
  30.             paras = HtmlPage.Document.QueryString;//获取网址中所有参数  
  31.             getCustomer();  
  32.         }  
  33.         private void resetBindingData()  
  34.         {  
  35.             svcContext = new navigateDataEntities(new Uri("DsNavigate.svc", UriKind.Relative));  
  36.         }  
  37.         /*通过Accounts_UserRoles表查处此用户的RoleID*/ 
  38.         private void getCustomer()  
  39.         {  
  40.             resetBindingData();  
  41.             DataServiceQuery<Accounts_UserRoles> query = svcContext.Accounts_UserRoles.AddQueryOption("$filter""UserID eq " + paras["username"] + "");//paras["username"]类似于asp.net中查询网址中的参数  
  42.             query.BeginExecute(onUserQueryComplete, query);  
  43.         }  
  44.  
  45.         private void onUserQueryComplete(IAsyncResult result)  
  46.         {  
  47.             string fil = "";//定义查询条件  
  48.             int i = 0;  
  49.             DataServiceQuery<Accounts_UserRoles> query = result.AsyncState as DataServiceQuery<Accounts_UserRoles>;  
  50.             List<Accounts_UserRoles> returned = query.EndExecute(result).ToList();  
  51.             var roleid = from f in returned //Linq用于查询Accounts_UserRoles表中用户的RoleID  
  52.                          select f;  
  53.             foreach (Accounts_UserRoles userRoles in returned)//RoleID不止一个,所以通过循环组成查询字符串  
  54.             {  
  55.                 if (i == 0)  
  56.                 {  
  57.                     fil = "RoleID eq " + userRoles.RoleID.ToString() + "";  
  58.                     i++;  
  59.                 }  
  60.                 fil += " or RoleID eq " + userRoles.RoleID.ToString() + "";  
  61.             }  
  62.             DataServiceQuery<Account_RolePermission> query2 = svcContext.Account_RolePermission.AddQueryOption("$filter", fil);  
  63.             query2.BeginExecute(onUsermoduleidComplete, query2);//查询出符合权限ID的菜单ModuleID  
  64.         }  
  65.         private void onUsermoduleidComplete(IAsyncResult result)  
  66.         {  
  67.             DataServiceQuery<Account_RolePermission> query = result.AsyncState as DataServiceQuery<Account_RolePermission>;  
  68.             List<Account_RolePermission> returned = query.EndExecute(result).ToList();  
  69.             var roleid = from f in returned //linq查出所有符合条件的菜单ID  
  70.                          select f;  
  71.             string file = "(";  
  72.             int i = 0;  
  73.             int sumreturn = 0;  
  74.             int a = 0;  
  75.             num = returned.Count / 30;//查询出的总条数和下面每次查询的条数的整数值就是要要提交查询的次数  
  76.             sumreturn = returned.Count %30 - 1;  
  77.             /*这里循环的作用,因为silverlight暂时不支持联合查询,对url的长度又有所限制,
  78.              * 所以我每次查询30条,当然在长度限制内越多越好,可以提高效率*/ 
  79.             foreach (Account_RolePermission roleper in returned)  
  80.             {  
  81.                 if (i <=30)  
  82.                 {  
  83.                     file += "ModuleID eq '" + roleper.ModuleID + "' or ";  
  84.                     if (i == 30)  
  85.                     {  
  86.                         file += "ModuleID eq '0') and ShowMenu eq true";  
  87.                         DataServiceQuery<Models> query3 = svcContext.Models.AddQueryOption("$filter", file);  
  88.                         query3.BeginExecute(onCustomerQueryComplete, query3);  
  89.                         i = 1;  
  90.                         file = "(";  
  91.                         a++;  
  92.                     }  
  93.                         //每次提交30条查询后可能还有剩余,所以要最后再提交一次  
  94.                     else if (a == num && i == sumreturn)  
  95.                     {  
  96.                         file += "ModuleID eq '0') and ShowMenu eq true";  
  97.                         DataServiceQuery<Models> query4 = svcContext.Models.AddQueryOption("$filter", file);  
  98.                         query4.BeginExecute(onCustomerQueryComplete, query4);  
  99.                     }  
  100.                     i++;  
  101.                 }  
  102.             }  
  103.         }  
  104.         private void onCustomerQueryComplete(IAsyncResult result)  
  105.         {  
  106.  
  107.             DataServiceQuery<Models> query = result.AsyncState as DataServiceQuery<Models>;  
  108.             List<Models> returned = query.EndExecute(result).ToList();  
  109.             ret.AddRange(returned);//把所有查询出的List整合到一起,方便排序  
  110.             summun++;//用来累加查询完的次数  
  111.             /*菜单的组织思路就是,一级菜单每查处一个,就查找它的二级菜单*/ 
  112.             if (summun == num)  
  113.             {  
  114.                 var swk = from f in ret  
  115.                           where f.ParentModuleID == Convert.ToString(0) //查询出一级菜单  
  116.                           orderby f.sortid  
  117.                           select f;  
  118.  
  119.                 foreach (Models navigate in swk)  
  120.                 {  
  121.                     var zy = from a in ret  
  122.                              where a.ParentModuleID == navigate.ModuleID//查询出二级菜单  
  123.                              select a;  
  124.                     AccordionItem ai = new AccordionItem();//定义Accordion的子目录  
  125.                     ai.AccordionButtonStyle = Application.Current.Resources["AccordionButtonStyle1"as Style;  
  126.                     ai.ExpandableContentControlStyle = Application.Current.Resources["ExpandableContentControlStyle1"as Style;  
  127.                     ai.Style = Application.Current.Resources["AccordionItemStyle1"as Style;  
  128.                     StackPanel sp = new StackPanel();  
  129.                     sp.Orientation = Orientation.Horizontal;  
  130.                     TextBlock tb = new TextBlock();  
  131.                     tb.Text = navigate.ModuleName.Trim();//一级菜单名称  
  132.                     Image ima = new Image();//一级菜单图片  
  133.                     ima.Source = new BitmapImage(new Uri("/images/icon/" + navigate.ModuleName.Trim() + ".jpg", UriKind.Relative));  
  134.                     ima.Width = 20;  
  135.                     ima.Height = 20;  
  136.                     sp.Children.Add(ima);  
  137.                     sp.Children.Add(tb);  
  138.                     ai.Header = sp;  
  139.  
  140.  
  141.                     StackPanel sp2 = new StackPanel();  
  142.                     sp2.Orientation = Orientation.Vertical;  
  143.                     foreach (Models naviga in zy)  
  144.                     {  
  145.                         StackPanel sp1 = new StackPanel();  
  146.                         sp1.Orientation = Orientation.Horizontal;  
  147.                         Image ima1 = new Image();//二级菜单图片  
  148.                         ima1.Source = new BitmapImage(new Uri("/images/icon/" + naviga.ModuleName.Trim() + ".jpg", UriKind.Relative));  
  149.                         ima1.Width = 20;  
  150.                         ima1.Height = 20;  
  151.                         HyperlinkButton hlb = new HyperlinkButton();  
  152.                         hlb.Style = Application.Current.Resources["HyperlinkButtonStyle1"as Style;  
  153.                         hlb.Content = naviga.ModuleName.Trim();//二级菜单名称  
  154.                         hlb.NavigateUri = new Uri("/"+paras["second"] + naviga.MenuPath.ToString().Substring(2), UriKind.Relative);  
  155.                         hlb.TargetName = "MainFrame";//此处是打开链接要插入的框架位置<frameset>  
  156.  
  157.                         sp1.Children.Add(ima1);  
  158.                         sp1.Children.Add(hlb);  
  159.  
  160.                         sp2.Children.Add(sp1);  
  161.                     }  
  162.  
  163.                     ai.Content = sp2;  
  164.                     acc.Items.Add(ai);  
  165.                 }  
  166.  
  167.  
  168.             }  
  169.         }  
  170.     }  
  171. }  

XAML代码:

  1. <UserControl 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  5.     mc:Ignorable="d" xmlns:layoutToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit" xmlns:layoutPrimitivesToolkit="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Layout.Toolkit" x:Class="navigate.MainPage" 
  6.     > 
  7.     <Grid x:Name="LayoutRoot" > 
  8.             <Grid.Background> 
  9.             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
  10.                 <GradientStop Color="white" Offset="0"/> 
  11.                 <GradientStop Color="#FF2AE0C6" Offset="1"/> 
  12.             </LinearGradientBrush> 
  13.         </Grid.Background>    
  14.         <StackPanel> 
  15.             <layoutToolkit:Accordion x:Name="acc"  Width="160" Margin="8,8,0,0"  Style="{StaticResource AccordionStyle1}"> 
  16.             </layoutToolkit:Accordion> 
  17.         </StackPanel>   
  18.     </Grid> 
  19. </UserControl> 
  20.  

HTMLL代码:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml" > 
  3.  
  4. <head> 
  5.     <title>navigate</title> 
  6.  
  7.       
  8. </head> 
  9.     <frameset Rows="100,*"> 
  10.         <frame src="http://www.google.com" frameborder="0"></frame> 
  11.         <frameset Cols="170,*" >   
  12.         <frame src="treeSl.aspx?username=718&second=jyoa" name="sl" frameborder="0"></frame> 
  13.         <frame  name="MainFrame" frameborder="0"></frame> 
  14.     </frameset> 
  15.     </frameset> 
  16. </html> 
  17.  

数据库表和字段我给大家贴个图吧:

Account_RolePermission表:

Accounts_UserRoles表:

Models表:

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作