控件事件触发,基本所有的控件都继承基类的控件,包含了一些 基类固定的事件属性 如 MouseEnter,MouseLeave, MouseLeftButtonDown,MouseLeftButtonUP,MouseMove,KeyDown,KeyUp等事件的通知.

 

  页面上事件注册声明

  在xaml的页面上 选择对应的控件的属性后,输入空格,选择对应的事件如MouseLeftButtonDown,然后在输入= ,系统会自动提示 建立事件或者选择已存在事件,你可以直接按tab ,它自动创建对应的事件,事件的代码区在这个页面对应的cs文件中

   


    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width
="400" Height="300">
    
<Grid x:Name="LayoutRoot" Background="White">
        
<Ellipse x:Name="ellipse" Fill="Aqua" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown"></Ellipse>
    
</Grid>
</UserControl>

xaml cs页面

 
private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SolidColorBrush newColor 
= new SolidColorBrush(Color.FromArgb(255200770));
            ellipse.Fill
=newColor;
        }

 

然后直接编译一下,运行浏览silverlightapplication3testpage.aspx页面即可,

SilverLight 2.0 事件触发

我们注意到在我们的web项目下有个ClientBin的文件夹下自动生成了 SilverlightApplication3.xap的文件,这个是对应的silverlightapplication3 项目自动生成的文件,当我们页面要使用silverlight的控件的时候,设置对应的访问的可以参考一下testpage已经存在的示例代码,一个silverlightapplication3 只会生成一个对应的.xap的文件,那怎么才能在访问到不同的页面上呢,这个我们下次再说。

如果你建立的项目没有web这个对应的网站的时候,你只要按ctrl+f5 即可看到对应的页面,或者直接在silverlightapplication3 下面加上html的页面对应的页面的代码为

 


    <title>Silverlight Project Test Page </title>

    
<style type="text/css">
    html, body {
        height: 
100%;
        overflow: auto;
    }
    body {
        padding: 
0;
        margin: 
0;
    }
    #silverlightControlHost {
        height: 
100%;
    }
    
</style>
    
    
<script type="text/javascript">
        function onSilverlightError(sender, args) {
        
            var appSource 
= "";
            
if (sender != null && sender != 0) {
                appSource 
= sender.getHost().Source;
            } 
            var errorType 
= args.ErrorType;
            var iErrorCode 
= args.ErrorCode;
            
            var errMsg 
= "Unhandled Error in Silverlight 2 Application " +  appSource + "\n" ;

            errMsg 
+= "Code: "+ iErrorCode + "    \n";
            errMsg 
+= "Category: " + errorType + "       \n";
            errMsg 
+= "Message: " + args.ErrorMessage + "     \n";

            
if (errorType == "ParserError")
            {
                errMsg 
+= "File: " + args.xamlFile + "     \n";
                errMsg 
+= "Line: " + args.lineNumber + "     \n";
                errMsg 
+= "Position: " + args.charPosition + "     \n";
            }
            
else if (errorType == "RuntimeError")
            {           
                
if (args.lineNumber != 0)
                {
                    errMsg 
+= "Line: " + args.lineNumber + "     \n";
                    errMsg 
+= "Position: " +  args.charPosition + "     \n";
                }
                errMsg 
+= "MethodName: " + args.methodName + "     \n";
            }

            
throw new Error(errMsg);
        }
    
</script>
</head>

<body>
    
<!-- Runtime errors from Silverlight will be displayed here.
    This will contain debugging information and should be removed or hidden when debugging 
is completed -->
    
<div id='errorLocation' style="font-size: small;color: Gray;"></div>

    
<div id="silverlightControlHost">
        
<object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%">
            
<param name="source" value="ClientBin/SilverlightApplication3.xap"/>
            
<param name="onerror" value="onSilverlightError" />
            
<param name="background" value="white" />
            
            
<a href="http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;">
                 
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
            
</a>
        
</object>
        
<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
    
</div>
</body>
</html>

 

不过这个页面里面的 <param name="source" value="ClientBin/SilverlightApplication3.xap"/>必须改为“<param name="source" value="Bin/Debug/SilverlightApplication3.xap"/>”,因为你现在的xap的文件已经生成到 bin的dubug的文件夹下面了。

这里我们顺便说一下html的这个页面的一些 object的一些元素的东西。object的type的属性指的就是silverlight的版本的信息application/x-silverlight-2-b2 是silverlight2.0 bate2 的版本 application/x-silverlight-2-b1 是2.0 bate1 的版本的信息。source这个是指定到silverlight项目生成的xap的文件。onerror一看就知道这个是发生错误的时候交给谁来处理。 后面的这个<a。。。>..</a> 这个指的是你silverlight的版本对应的下载地址和 他对应的版本图片的信息。这样才能根据你使用的版本下载到对应的silverlight的版本文件。

相关文章: