主要功能
1.增加登录用户判断
2.增加虚拟目录判断
3.效率显著提高
今天主要考虑到虚拟目录,SL暂时不支持虚拟目录,也就是如果超链接是相对路径,就会报错找不到路径,其实是SL不能自动添加虚拟目录这一路径,所以我在调用.XAP宿主页面时加了查询参数(?username=swk&second=jyoa),这里second=jyoa是虚拟目录名称,至于用户登录问题,SL不支持session,不过支持cookie,我这里用到的也是网址传递参数,用ASP.NET获取用户ID然后传递给SL给。效率比昨天的快很多,希望微软尽快支持联合查询,最好支持ADO.NET,至于界面修改方面有问题的可以参看微软的修改模板教程,其实修改界面我也不是太懂,很多朋友在问,不好意思了,这个我真的不怎么会...呵呵,有什么问题,或者有什么好的想法可以加我,大家一起讨论,毕竟是新技术,自己学起来很是郁闷...
CS文件代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using System.Windows.Media.Imaging;
- using System.Xml;
- using System.Xml.Linq;
- using navigate.navigateServes;
- using System.Data.Services.Client;
- using System.Windows.Browser;
- namespace navigate
- {
- public partial class MainPage : UserControl
- {
- int num = 0;
- int summun = 0;
- List<Models> ret = new List<Models>();
- navigateDataEntities svcContext;
- IDictionary<String, String> paras = null;
- public MainPage()
- {
- InitializeComponent();
- paras = HtmlPage.Document.QueryString;//获取网址中所有参数
- getCustomer();
- }
- private void resetBindingData()
- {
- svcContext = new navigateDataEntities(new Uri("DsNavigate.svc", UriKind.Relative));
- }
- /*通过Accounts_UserRoles表查处此用户的RoleID*/
- private void getCustomer()
- {
- resetBindingData();
- DataServiceQuery<Accounts_UserRoles> query = svcContext.Accounts_UserRoles.AddQueryOption("$filter", "UserID eq " + paras["username"] + "");//paras["username"]类似于asp.net中查询网址中的参数
- query.BeginExecute(onUserQueryComplete, query);
- }
- private void onUserQueryComplete(IAsyncResult result)
- {
- string fil = "";//定义查询条件
- int i = 0;
- DataServiceQuery<Accounts_UserRoles> query = result.AsyncState as DataServiceQuery<Accounts_UserRoles>;
- List<Accounts_UserRoles> returned = query.EndExecute(result).ToList();
- var roleid = from f in returned //Linq用于查询Accounts_UserRoles表中用户的RoleID
- select f;
- foreach (Accounts_UserRoles userRoles in returned)//RoleID不止一个,所以通过循环组成查询字符串
- {
- if (i == 0)
- {
- fil = "RoleID eq " + userRoles.RoleID.ToString() + "";
- i++;
- }
- fil += " or RoleID eq " + userRoles.RoleID.ToString() + "";
- }
- DataServiceQuery<Account_RolePermission> query2 = svcContext.Account_RolePermission.AddQueryOption("$filter", fil);
- query2.BeginExecute(onUsermoduleidComplete, query2);//查询出符合权限ID的菜单ModuleID
- }
- private void onUsermoduleidComplete(IAsyncResult result)
- {
- DataServiceQuery<Account_RolePermission> query = result.AsyncState as DataServiceQuery<Account_RolePermission>;
- List<Account_RolePermission> returned = query.EndExecute(result).ToList();
- var roleid = from f in returned //linq查出所有符合条件的菜单ID
- select f;
- string file = "(";
- int i = 0;
- int sumreturn = 0;
- int a = 0;
- num = returned.Count / 30;//查询出的总条数和下面每次查询的条数的整数值就是要要提交查询的次数
- sumreturn = returned.Count %30 - 1;
- /*这里循环的作用,因为silverlight暂时不支持联合查询,对url的长度又有所限制,
- * 所以我每次查询30条,当然在长度限制内越多越好,可以提高效率*/
- foreach (Account_RolePermission roleper in returned)
- {
- if (i <=30)
- {
- file += "ModuleID eq '" + roleper.ModuleID + "' or ";
- if (i == 30)
- {
- file += "ModuleID eq '0') and ShowMenu eq true";
- DataServiceQuery<Models> query3 = svcContext.Models.AddQueryOption("$filter", file);
- query3.BeginExecute(onCustomerQueryComplete, query3);
- i = 1;
- file = "(";
- a++;
- }
- //每次提交30条查询后可能还有剩余,所以要最后再提交一次
- else if (a == num && i == sumreturn)
- {
- file += "ModuleID eq '0') and ShowMenu eq true";
- DataServiceQuery<Models> query4 = svcContext.Models.AddQueryOption("$filter", file);
- query4.BeginExecute(onCustomerQueryComplete, query4);
- }
- i++;
- }
- }
- }
- private void onCustomerQueryComplete(IAsyncResult result)
- {
- DataServiceQuery<Models> query = result.AsyncState as DataServiceQuery<Models>;
- List<Models> returned = query.EndExecute(result).ToList();
- ret.AddRange(returned);//把所有查询出的List整合到一起,方便排序
- summun++;//用来累加查询完的次数
- /*菜单的组织思路就是,一级菜单每查处一个,就查找它的二级菜单*/
- if (summun == num)
- {
- var swk = from f in ret
- where f.ParentModuleID == Convert.ToString(0) //查询出一级菜单
- orderby f.sortid
- select f;
- foreach (Models navigate in swk)
- {
- var zy = from a in ret
- where a.ParentModuleID == navigate.ModuleID//查询出二级菜单
- select a;
- AccordionItem ai = new AccordionItem();//定义Accordion的子目录
- ai.AccordionButtonStyle = Application.Current.Resources["AccordionButtonStyle1"] as Style;
- ai.ExpandableContentControlStyle = Application.Current.Resources["ExpandableContentControlStyle1"] as Style;
- ai.Style = Application.Current.Resources["AccordionItemStyle1"] as Style;
- StackPanel sp = new StackPanel();
- sp.Orientation = Orientation.Horizontal;
- TextBlock tb = new TextBlock();
- tb.Text = navigate.ModuleName.Trim();//一级菜单名称
- Image ima = new Image();//一级菜单图片
- ima.Source = new BitmapImage(new Uri("/images/icon/" + navigate.ModuleName.Trim() + ".jpg", UriKind.Relative));
- ima.Width = 20;
- ima.Height = 20;
- sp.Children.Add(ima);
- sp.Children.Add(tb);
- ai.Header = sp;
- StackPanel sp2 = new StackPanel();
- sp2.Orientation = Orientation.Vertical;
- foreach (Models naviga in zy)
- {
- StackPanel sp1 = new StackPanel();
- sp1.Orientation = Orientation.Horizontal;
- Image ima1 = new Image();//二级菜单图片
- ima1.Source = new BitmapImage(new Uri("/images/icon/" + naviga.ModuleName.Trim() + ".jpg", UriKind.Relative));
- ima1.Width = 20;
- ima1.Height = 20;
- HyperlinkButton hlb = new HyperlinkButton();
- hlb.Style = Application.Current.Resources["HyperlinkButtonStyle1"] as Style;
- hlb.Content = naviga.ModuleName.Trim();//二级菜单名称
- hlb.NavigateUri = new Uri("/"+paras["second"] + naviga.MenuPath.ToString().Substring(2), UriKind.Relative);
- hlb.TargetName = "MainFrame";//此处是打开链接要插入的框架位置<frameset>
- sp1.Children.Add(ima1);
- sp1.Children.Add(hlb);
- sp2.Children.Add(sp1);
- }
- ai.Content = sp2;
- acc.Items.Add(ai);
- }
- }
- }
- }
- }
XAML代码:
- <UserControl
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- 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"
- >
- <Grid x:Name="LayoutRoot" >
- <Grid.Background>
- <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
- <GradientStop Color="white" Offset="0"/>
- <GradientStop Color="#FF2AE0C6" Offset="1"/>
- </LinearGradientBrush>
- </Grid.Background>
- <StackPanel>
- <layoutToolkit:Accordion x:Name="acc" Width="160" Margin="8,8,0,0" Style="{StaticResource AccordionStyle1}">
- </layoutToolkit:Accordion>
- </StackPanel>
- </Grid>
- </UserControl>
HTMLL代码:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <title>navigate</title>
- </head>
- <frameset Rows="100,*">
- <frame src="http://www.google.com" frameborder="0"></frame>
- <frameset Cols="170,*" >
- <frame src="treeSl.aspx?username=718&second=jyoa" name="sl" frameborder="0"></frame>
- <frame name="MainFrame" frameborder="0"></frame>
- </frameset>
- </frameset>
- </html>
数据库表和字段我给大家贴个图吧:
Account_RolePermission表:

Accounts_UserRoles表:

Models表:
