2007年11月27日

Silverlight程序动态创建添加UI元素

我们都知道XAML标签元素在sliverlight运行时被转换成相应的对象. 程序运行时, 也可以动态的添加UI元素.

1.通过对象创建UI元素

Rectangle rc = new Rectangle();
rc.Width = 20;
rc.Height = 20;
rc.Fill = new SolidColorBrush(Colors.Red);
rc.SetValue<double>(Canvas.LeftProperty, 200);

this.Children.Add(rc);

2.通过XAML创建UI元素

通过XamlReader类的Load方法, 动态的创建UI元素

string xamlStr = "<Rectangle Canvas.Left=\"" + 20 + "\" Canvas.Top=\"" + 20 + "\""  
                 + " Width=\""+40+"\" Height=\""+40+"\""  
                 + " Fill =\"Red\" />";

try
{
       Rectangle rc = (Rectangle)XamlReader.Load(xamlStr);
       this.Children.Add(rc);
}
catch { }

posted @ 2007-11-27 20:17 cofd 阅读(227) 评论(0) 编辑

无废话Silverlight入门

1.Silverlight是什么

Silverlight是一个跨浏览器的、跨平台的插件,为网络带来下一代基于.NET媒体体验和丰富的交互式应用程序。Silverlight提供灵活的编程模型,支持AJAX, VB, C#, Python, Ruby等语言,并集成到现有的网络应用程序中。Silverlight对运行在Mac或Windows上的主流浏览器提供高质量视频信息的快速、低成本的传递

Microsoft® Silverlight™ is a cross-browser, cross-platform plug-in for delivering the next generation of .NET based media experiences and rich interactive applications for the Web. Silverlight offers a flexible programming model that supports AJAX, VB, C#, Python, and Ruby, and integrates with existing Web applications. Silverlight supports fast, cost-effective delivery of high-quality video to all major browsers running on the Mac OS or Windows.

2.开发环境

运行时:

Microsoft Silverlight 1.1Alpha Refresh

开发工具:

Microsoft Visula Studio 2008

Silverlight 1.1 Tools Alpha for Visual Studio 2008

设计工具:(可选)

Expression Blend 2

3.HelloWorld

打开VS, 新建一个Silverlight Project, 修改默认生成的Page.xaml文件为:

<Canvas x:Name="parentCanvas"
        xmlns="http://schemas.microsoft.com/client/2007"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Loaded="Page_Loaded"
        x:Class="SilverlightProject1.Page;assembly=ClientBin/SilverlightProject1.dll"
        Width="640"
        Height="480"
        Background="White"
        >
  <TextBlock x:Name="txt" Text="HelloWorld" MouseLeftButtonDown="SayHi"/>
</Canvas>

修改Page.xaml.cs为:

using System;
using System.Windows.Controls;
using System.Windows.Input;

namespace SilverlightProject1
{
    public partial class Page : Canvas
    {
        public void Page_Loaded(object o, EventArgs e)
        {
            // Required to initialize variables
            InitializeComponent();
        }

        protected void SayHi(object sener, MouseEventArgs e)
        {
            this.txt.Text = "HelloWorld to Silverlight";
        }
    }
}

按ctrl+F5运行.

:)

posted @ 2007-11-27 20:10 cofd 阅读(116) 评论(0) 编辑

导航

<2007年11月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

搜索

 
 

随笔分类(19)

随笔档案(19)

积分与排名

  • 积分 - 7674
  • 排名 - 10391

阅读排行榜