使用Silverlight构建插件式应用程序(四)

www.cuface.cn/main.html

构建第二个插件:

我要构建的第二个插件是用来显示用户的关系图形显示的界面如下:


其中中心是登陆的当前用户,三个人的使用户的组别,最边上的图标是组别下的用户。用户每个节点都可以移动。

构建数据库:用户表,用户关系表(参见数据库结构)。

1.         新建立一个SilverLight的类库项目,名字叫 WindCloud.PlugIn.Users;

2.         删除掉默认的Class1.cs项目

3.         添加对公共项目 WindCloud.PubUnit的引用;

4.         添加对公共项目 WindCloud.PlugIn的引用;

5.         新建一个目录Controls

a)         添加一个类文件UserLine,描述连线的信息

public class UserLine

    {

        public string LineName { set; get; }

        public string NodeStart { set; get; }

        public string NodeEnd { set; get; }

        public Line NodeLine { set; get; }

    }

b)        添加一个用户控件UserNode,描述每个节点:

public string NodeName

        {

            set

            {

                this.nodeText.Text = value;

            }

            get

            {

                return this.nodeText.Text;

            }

        }

        public Point NodePoint { set; get; }

        public ImageSource NodeImg

        {

            set

            {

                this.nodeImg.Source = value;

            }

            get

            {

                return this.nodeImg.Source;

            }

        }

界面文件如下:

<Grid x:Name="LayoutRoot" Background="Transparent">

        <StackPanel HorizontalAlignment="Center">

            <Image x:Name="nodeImg" />

            <TextBlock x:Name="nodeText" Text="FriendName" FontSize="12" ></TextBlock>

        </StackPanel>

</Grid>

下面从数据库获取数据形成图形:

1.         建立两个对象,保存节点的个数和节点之间的连线:

        Dictionary<string, UserNode> userNodes = new Dictionary<string, UserNode>();

        Dictionary<string, UserLine> userLines = new Dictionary<string, UserLine>();

 

2.         使用WCF从数据库获取数据,获取指定用户的人际关系数据:

            WSMainClient ws = new WSMainClient(new System.ServiceModel.BasicHttpBinding(), new System.ServiceModel.EndpointAddress(serviceUri));

            ws.GetFriendsCompleted += new EventHandler<GetFriendsCompletedEventArgs>(ws_GetFriendsCompleted);

            ws.GetFriendsAsync("song");

3.         根据数据构建人际关系图形:

    //添加用户节点,

            foreach (UserRelaInfo ri in userRelaInfo)

            {

                string[] nodeLocal = ri.LocalStyle.Split(',');

                CreateUserNode(ri, new Point(Convert.ToInt32(nodeLocal[0]), Convert.ToInt32(nodeLocal[1])));

            }

//选择所有的用户组信息

            var colltions = from uri in userRelaInfo where uri.RelaType == "colltion" orderby uri.RelationId select uri;

            foreach (UserRelaInfo ri in colltions)

            {

                //添加用户和组之间的连线

                CreateUserLine(ri.RelationId.ToString(), "song", ri.FriendName);

                //添加每个组和朋友之间的连线

                var userCollection = from uc in userRelaInfo where uc.Relation == ri.FriendName orderby uc.RelationId select uc;

                foreach (UserRelaInfo tmpri in userCollection)

                {

                    //添加用户和组之间的连线

                    CreateUserLine(tmpri.RelationId.ToString(), ri.FriendName, tmpri.FriendName);

 

                }

           }

4.        把用户节点和连线添加到界面上:

foreach (KeyValuePair<string, UserLine> kvp in userLines)

                {

                    this.RelationMap.Children.Add(kvp.Value.NodeLine);

                }

                foreach (KeyValuePair<string, UserNode> kvp in userNodes)

                {

                    this.RelationMap.Children.Add(kvp.Value);

                    App.WriteDebug(kvp.Value.nodeText.Text);

                }

5.         用户拖动位置之后,保存新的位置到数据库:

    public void SaveLocalStyle(string userRelationId, string localStyle)

        {

            //用户拖动位置之后,保存节点位置

            Uri uri = System.Windows.Browser.HtmlPage.Document.DocumentUri;

            string host = uri.AbsoluteUri;

            host = host.Substring(0, host.Length - uri.LocalPath.Length);

            string servicePath = "/Services/WSMain.svc";

            string serviceUri = host + servicePath;

 

            WSMainClient ws = new WSMainClient(new System.ServiceModel.BasicHttpBinding(), new System.ServiceModel.EndpointAddress(serviceUri));

            ws.UpDateLocalStyleCompleted += (o, ev) =>

            {

 

            };

            ws.UpDateLocalStyleAsync(userRelationId, localStyle);

        }

 

至此,整个插件的主体部分全部构建完成。

代码下载:http://www.prolightsoft.com/downloads/windcloud.rar

演示地址:www.cuface.cn/main.html

欢迎对开源SiverLight有兴趣的朋友加入这个项目:msn songsgroup@hotmail.com


原文地址:https://www.cnblogs.com/songsgroup/p/1295337.html