首先我们添加一个文件Menu.xml,xml内容如下:
<?
xml version="1.0" encoding="utf-8"
?>
<
Item
Id
="
10
"
Text
="
Monet
"
Help
="
Claude Monet : Thames at Westminster
"
/>
<
Item
Id
="
20
"
Text
="
Turner
"
Help
="
Joseph Turner : Oxford High Street
"
/>
<
Item
Id
="
30
"
Text
="
Renoir
"
Help
="
Auguste Renoir : Pont des arts, Paris
"
/>
<
Item
Id
="
40
"
Text
="
Pissaro
"
Help
="
Camille Pissaro : L'Hermitage
"
Selected
="
True
"
/>
<
Item
Id
="
50
"
Text
="
Vermeer
"
Help
="
Johannes Vermeer : View of Delft
"
/>
</
Menu
>
对于Silverlight,我基本只要用到数据,都会建立一个类:
public class MenuItem
{
public int Id { get; set; }
public string Text { get; set; }
public string Help { get; set; }
public bool Selected { get; set; }
}
下面我将从xml中取出数据,填充这个类,这里我采用linq语句,这里我说一句,linq语句 包含三大块.linq to sql 效率不是很高,但是linq to xml 还是推荐大家使用的.
下面是读取xml 然后赋值给类:
//定义一个集合,接收数据
List<MenuItem> tabItems;
下面是读取xml数据的固定的用法:
XElement xml = XElement.Load("Menu.xml");
//非常帅气的linq语句
var listItems = from p in xml.Elements("Item")
select new MenuItem
{
Text = (string)p.Attribute("Text").Value,
Id = (int)p.Attribute("Id"),
Help = (string)p.Attribute("Help"),
Selected = p.Attribute("Selected") != null ? true : false
};
tabItems = listItems.ToList();
//这句话的功能大家要记住,因为上面我是定义的var类型的,
前面我们读取到了xml中的数据 并保存到了集合中了,下面是读取集合中的数据,动态生成菜单
(这里要说明一下,现实中项目一般都是把xml数据放置于网页端,我们假设你已经从服务器取出数据保存到集合中了)
//动态生成菜单
for (int n = 0; n < N; n++)
{
TextBlock txtItem = new TextBlock();
//tabItems是前面我们定义的集合
txtItem.Text = tabItems[n].Text;
txtItem.VerticalAlignment = VerticalAlignment.Center; txtItem.HorizontalAlignment = HorizontalAlignment.Center;
switch (tabItems[n].Selected)
{
case true: txtItem.Foreground = new SolidColorBrush(Colors.Red); break;
case false: txtItem.Foreground = new SolidColorBrush(Color.FromArgb(255, 37, 36, 85)); break;
}
Rectangle rcItem = new Rectangle(); rcItem.Fill = new SolidColorBrush(Colors.Transparent);
rcItem.Fill = new SolidColorBrush(Colors.Transparent);
rcItem.Width = txtItem.ActualWidth + 6;
Grid gridItem = new Grid();
gridItem.Width = txtItem.ActualWidth + 55;
gridItem.Tag = n;
Line liSep = new Line();
liSep.X1 = gridItem.ActualWidth - 5; liSep.Y1 = 5;
liSep.X2 = gridItem.ActualWidth - 5; liSep.Y2 = 35; liSep.StrokeThickness = 5;
liSep.Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 245, 190));
gridItem.MouseLeave += new MouseEventHandler(c_MouseLeave);
gridItem.MouseEnter += new MouseEventHandler(c_MouseEnter);
gridItem.MouseLeftButtonDown += new MouseButtonEventHandler(c_MouseLeftButtonDown);
gridItem.Children.Add(rcItem); gridItem.Children.Add(txtItem); gridItem.Children.Add(liSep);
spMenu.Children.Add(gridItem);
if (tabItems[n].Selected) nItemSelected = n;
}
//下面是上面对应的事件
void c_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Grid gridItem = sender as Grid;
int nItem = (int)gridItem.Tag;
MenuEventArgs args = new MenuEventArgs(); args.ItemId = tabItems[nItem].Id;
TextBlock txtItem = gridItem.Children[1] as TextBlock;
txtItem.Foreground = new SolidColorBrush(Colors.Red);
if (nItemSelected != -1)
{
Grid gSel = spMenu.Children[nItemSelected] as Grid;
TextBlock txtSelItem = gSel.Children[1] as TextBlock;
txtSelItem.Foreground = new SolidColorBrush(Color.FromArgb(255, 37, 36, 85));
}
nItemSelected = nItem;
if (Click != null) Click(this, args);
}
//事件
void c_MouseEnter(object sender, MouseEventArgs e)
{
Grid gridItem = sender as Grid;
Rectangle rcItem = gridItem.Children[0] as Rectangle;
rcItem.Fill = new SolidColorBrush(Color.FromArgb(255, 203, 203, 239));
int nItem = (int)gridItem.Tag;
if (SB != null) SB.txtToolTip.Text = tabItems[nItem].Help;
}
//事件
void c_MouseLeave(object sender, MouseEventArgs e)
{
Grid gridItem = sender as Grid;
Rectangle rcItem = gridItem.Children[0] as Rectangle;
rcItem.Fill = new SolidColorBrush(Colors.Transparent);
if (SB != null) SB.txtToolTip.Text = "";
}
下面讲解 事件如何传递参数:
首先定义一个事件类:
事件定义:
大家知道事件都是和委托挂钩的:
1
public
delegate
void
MenuEventHandler(
object
sender, MenuEventArgs e);
2
public
event
MenuEventHandler Click;
定义类:
1
public
class
MenuEventArgs : EventArgs
2
{
3
public
int
ItemId {
get
;
set
; }
4
}
大家继续看下面的 一句话:
下面一句话是放置于一个事件中的,讲解了如何设置页面的事件并且传递参数:
MenuEventArgs args
=
new
MenuEventArgs();
args.ItemId
=
tabItems[nItemSelected].Id;
if
(Click
!=
null
) Click(
this
, args);
前面都写好了之后,如果另一个页面调用前面的页面,便可以很容易调用其事件,并且把参数也传递过去:
下面是第二个页面,这个页面调用了上面的页面的事件:
//前台,我们定义的用户控件:
<mnu:Menu x:Name="myMenu" Grid.Row="0" Click="myMenu_Click" />
//后台处理代码:这样我们通过 e 就可以得到 上面的Menu页面传递过来的参数了.(这个是企业级开发必须会的知识点)
private void myMenu_Click(object sender, MenuEventArgs e)
{
string s = "";
switch (e.ItemId)
{
case 10: s = "Monet"; break;
case 20: s = "Turner"; break;
case 30: s = "Renoir"; break;
case 40: s = "Pissaro"; break;
case 50: s = "Vermeer"; break;
}
img.Source = new BitmapImage(new Uri(s + ".jpg", UriKind.Relative));
}
本教程来自我的企业级开发项目,但是为了侵权,我找了一个类似的demo 给大家讲解.
demo讲解均原创,不懂可以留言.