Silverlight学习笔记二(页面跳转与控件拖动效果实现)

接笔记一: http://www.cnblogs.com/greatverve/archive/2010/05/09/1731164.html
web上面最常见的就是从一个页面跳转到另一个页面,搜索了一下,原来Silverlight是这样实现页面跳转的。
一、新建一个接口,代码如下:
ExpandedBlockStart.gif 大气象
using  System;
using  System.Net;
using  System.Windows;
using  System.Windows.Controls;
using  System.Windows.Documents;
using  System.Windows.Ink;
using  System.Windows.Input;
using  System.Windows.Media;
using  System.Windows.Media.Animation;
using  System.Windows.Shapes;

namespace  FirstSilverlight
{
    
public   interface  IContent
    {
        UIElement Content { 
get set ; }
    }
}

 

新建一个Silverlight用户控件,代码如下:
ExpandedBlockStart.gif 大气象
< UserControl  xmlns:basics ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"   x:Class ="FirstSilverlight.FirstControl"
    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" >
    
< Button  Width ="100"  Height ="50"  Name ="btnGo"  Content ="跳转"  Click ="Button_Click" ></ Button >
    
</ Grid >
</ UserControl >

 

ExpandedBlockStart.gif 大气象
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;

namespace  FirstSilverlight
{
    
public   partial   class  FirstControl : UserControl, IContent // 继承接口
    {
        
public  FirstControl()
        {
            InitializeComponent();
        }
        
// 实现接口
         public   new  UIElement Content
        {
            
get
            {
                
return   base .Content;
            }
            
set
            {
                
base .Content  =  value;
            }
        } 

        
private   void  Button_Click( object  sender, RoutedEventArgs e)
        {
            
// 实现页面跳转
            (Application.Current.RootVisual  as  IContent).Content  =   new  DragControl();
        }
    }
}

 

另外发现一个小技巧,就好像WinForm的Main()函数里设置第一个窗口一样Application.Run(new MainForm());
在App.xaml.cs的Application_Startup()可以设置第一个场景,代码如下:
ExpandedBlockStart.gif 大气象
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;

namespace  FirstSilverlight
{
    
public   partial   class  App : Application
    {

        
public  App()
        {
            
this .Startup  +=   this .Application_Startup;
            
this .Exit  +=   this .Application_Exit;
            
this .UnhandledException  +=   this .Application_UnhandledException;

            InitializeComponent();
        }

        
private   void  Application_Startup( object  sender, StartupEventArgs e)
        {
            
// this.RootVisual = new Page();
             this .RootVisual  =   new  FirstControl(); // 设置第一个场景
        }

        
private   void  Application_Exit( object  sender, EventArgs e)
        {

        }
        
private   void  Application_UnhandledException( object  sender, ApplicationUnhandledExceptionEventArgs e)
        {
            
//  如果应用程序是在调试器外运行的,则使用浏览器的
            
//  异常机制报告该异常。在 IE 上,将在状态栏中用一个 
            
//  黄色警报图标来显示该异常,而 Firefox 则会显示一个脚本错误。
             if  ( ! System.Diagnostics.Debugger.IsAttached)
            {

                
//  注意: 这使应用程序可以在已引发异常但尚未处理该异常的情况下
                
//  继续运行。 
                
//  对于生产应用程序,此错误处理应替换为向网站报告错误
                
//  并停止应用程序。
                e.Handled  =   true ;
                Deployment.Current.Dispatcher.BeginInvoke(
delegate  { ReportErrorToDOM(e); });
            }
        }
        
private   void  ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            
try
            {
                
string  errorMsg  =  e.ExceptionObject.Message  +  e.ExceptionObject.StackTrace;
                errorMsg 
=  errorMsg.Replace( ' " ' ' \ '' ).Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval(
" throw new Error(\ " Unhandled Error  in  Silverlight  2  Application  "  + errorMsg +  " \ " ); " );
            }
            
catch  (Exception)
            {
            }
        }
    }
}

 

现在是拖动效果的实现,新建Silverlight用户控件DragControl.xaml
ExpandedBlockStart.gif 大气象
< UserControl  x:Class ="FirstSilverlight.DragControl"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"  
    Width
="400"  Height ="300" >
    
< Canvas  Background ="#46461F" >
        
<!-- 李会军这里用Button已经无法拖动 -->
        
< Image  Source ="smile_6.png"  
        MouseLeftButtonDown
="OnMouseDown"  
        MouseMove
="OnMouseMove"  
        MouseLeftButtonUp
="OnMouseUp" />
    
</ Canvas >
</ UserControl >

 

ExpandedBlockStart.gif 大气象
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;

namespace  FirstSilverlight
{
    
public   partial   class  DragControl : UserControl
    {
        
public  DragControl()
        {
            InitializeComponent();
        }
        
        
bool  trackingMouseMove  =   false ;
        Point mousePosition;

        
void  OnMouseDown( object  sender, MouseButtonEventArgs e)
        {
            FrameworkElement element 
=  sender  as  FrameworkElement;
            mousePosition 
=  e.GetPosition( null );
            trackingMouseMove 
=   true ;
            
if  ( null   !=  element)
            {
                element.CaptureMouse();
                element.Cursor 
=  Cursors.Hand;
            }
        }
        
void  OnMouseMove( object  sender, MouseEventArgs e)
        {
            FrameworkElement element 
=  sender  as  FrameworkElement;
            
if  (trackingMouseMove)
            {
                
double  deltaV  =  e.GetPosition( null ).Y  -  mousePosition.Y;
                
double  deltaH  =  e.GetPosition( null ).X  -  mousePosition.X;
                
double  newTop  =  deltaV  +  ( double )element.GetValue(Canvas.TopProperty);
                
double  newLeft  =  deltaH  +  ( double )element.GetValue(Canvas.LeftProperty);

                element.SetValue(Canvas.TopProperty, newTop);
                element.SetValue(Canvas.LeftProperty, newLeft);

                mousePosition 
=  e.GetPosition( null );
            }
        }
        
void  OnMouseUp( object  sender, MouseButtonEventArgs e)
        {
            FrameworkElement element 
=  sender  as  FrameworkElement;
            trackingMouseMove 
=   false ;
            element.ReleaseMouseCapture();

            mousePosition.X 
=  mousePosition.Y  =   0 ;
            element.Cursor 
=   null ;
        }
    }
}

 

有空就积累一点吧,欢迎大家指点。

转载于:https://www.cnblogs.com/greatverve/archive/2010/05/26/silverlight-note-2.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值