Silverlight学习笔记二(页面跳转与控件拖动效果实现)
接笔记一:http://www.cnblogs.com/greatverve/archive/2010/05/09/1731164.html
web上面最常见的就是从一个页面跳转到另一个页面,搜索了一下,原来Silverlight是这样实现页面跳转的。
一、新建一个接口,代码如下:
在App.xaml.cs的Application_Startup()可以设置第一个场景,代码如下:
web上面最常见的就是从一个页面跳转到另一个页面,搜索了一下,原来Silverlight是这样实现页面跳转的。
一、新建一个接口,代码如下:
大气象
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; }
}
}
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用户控件,代码如下:
大气象
<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>
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>
大气象
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();
}
}
}
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()可以设置第一个场景,代码如下:
大气象
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)
{
}
}
}
}
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
大气象
<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>
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>
大气象
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;
}
}
}
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;
}
}
}
有空就积累一点吧,欢迎大家指点。
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。