稳扎稳打Silverlight(47) - 4.0UI之 *** 作剪切板,隐式样式,Compositetransform,拖放外部文件到程序中
作者: webabcd
介绍
Silverlight 4.0 用户界面(UI)相关:
*** 作剪切板 - 支持获取或设置剪切板中的文本信息 隐式样式(Implicit Style) - 将某种样式应用到某种类型的所有元素,即全局样式 Compositetransform - 将多种转换方式合而为一 拖动(Drag)外部文件,并将其放到(Drop) Silverlight 程序中
在线DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、演示如何 *** 作剪切板
Clipboard.xaml
代码 < navigation:Page x:Class ="Silverlight40.UI.Clipboard"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
Title ="Clipboard Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="left" >
< TextBox name ="txtMsg" WIDth ="200" Height ="50" margin ="5" />
<!-- 复制 txtMsg 中的文本 -->
< button name ="btncopy" Content ="复制" margin ="5" Click ="btncopy_Click" />
<!-- 将剪切板中的文本粘贴到 txtMsg -->
< button name ="btnPaste" Content ="粘贴" margin ="5" Click ="btnPaste_Click" />
</ StackPanel >
</ GrID >
</ navigation:Page >
Clipboard.xaml.cs
代码 /*
* Silverlight 4.0 支持对剪切板的访问,只支持获取或设置剪切板中的文本信息
* System.windows.Clipboard.SetText(string s) - 将指定的字符串复制到剪切板
* System.windows.Clipboard.GetText() - 获取剪切板中的文本信息
*
* 只有在 Click 或 KeyDown 事件的处理程序中才可以访问剪切板
* 访问剪切板时,Silverlight 会d出对话框要求用户进行确认,如果用户禁止了,则会引发异常。如果程序是“被信任的应用程序”则不会d出该对话框
*/
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;
using System.windows.Navigation;
namespace Silverlight40.UI
{
public partial class Clipboard : Page
{
public Clipboard()
{
InitializeComponent();
}
protected overrIDe voID OnNavigatedTo(NavigationEventArgs e)
{
}
private voID btncopy_Click( object sender, RoutedEventArgs e)
{
try
{
System.windows.Clipboard.SetText(txtMsg.Text);
}
catch (System.Security.SecurityException ex)
{
MessageBox.Show(ex.Message);
}
}
private voID btnPaste_Click( object sender, RoutedEventArgs e)
{
try
{
txtMsg.SelectedText = System.windows.Clipboard.GetText();
}
catch (System.Security.SecurityException ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
2、演示 Silverlight 4.0 对隐式样式(全局样式)的支持
ImplicitStyle.xaml 代码 < navigation:Page x:Class ="Silverlight40.UI.ImplicitStyle"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
Title ="ImplicitStyle Page" >
< GrID x:name ="LayoutRoot" >
<!--
隐式样式(Implicit Style) - 将某种样式应用到某种类型的所有元素,即全局样式
-->
< Togglebutton WIDth ="200" Height ="50" Content ="用于演示隐式样式(Implicit Style)。Togglebutton 的样式设置详见 Assets/TogglebuttonStyle.xaml" />
</ GrID >
</ navigation:Page >
App.xaml 代码 < Application
x:Class ="Silverlight40.App"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< Application.Resources >
< ResourceDictionary >
< ResourceDictionary.MergedDictionarIEs >
< ResourceDictionary Source ="Assets/TogglebuttonStyle.xaml" />
</ ResourceDictionary.MergedDictionarIEs >
</ ResourceDictionary >
</ Application.Resources >
</ Application >
TogglebuttonStyle.xaml 代码 < ResourceDictionary
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation" >
<!-- Togglebutton 的隐式样式(Implicit Style) -->
< Style targettype ="Togglebutton" >
< Setter Property ="Background" Value ="#FF1F3B53" />
< Setter Property ="Foreground" Value ="#FF000000" />
< Setter Property ="padding" Value ="3" />
< Setter Property ="borderThickness" Value ="1" />
< Setter Property ="borderBrush" >
< Setter.Value >
< linearGradIEntBrush EndPoint ="0.5,1" StartPoint ="0.5,0" >
< GradIEntStop color ="#FFA3AEB9" Offset ="0" />
< GradIEntStop color ="#FF8399A9" Offset ="0.375" />
< GradIEntStop color ="#FF718597" Offset ="0.375" />
< GradIEntStop color ="#FF617584" Offset ="1" />
</ linearGradIEntBrush >
</ Setter.Value >
</ Setter >
< Setter Property ="Template" >
< Setter.Value >
< ControlTemplate targettype ="Togglebutton" >
< GrID >
< visualstatemanager.VisualStateGroups >
< VisualStateGroup x:name ="CommonStates" >
< VisualState x:name ="normal" />
< VisualState x:name ="MouSEOver" >
< Storyboard >
< DoubleAnimationUsingKeyFrames Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.Targetname ="Background" >
< EasingDoubleKeyFrame KeyTime ="0" Value ="1" />
</ DoubleAnimationUsingKeyFrames >
</ Storyboard >
</ VisualState >
< VisualState x:name ="pressed" >
< Storyboard >
< colorAnimationUsingKeyFrames Storyboard.TargetProperty ="(border.Background).(SolIDcolorBrush.color)" Storyboard.Targetname ="Background" >
< SplinecolorKeyFrame KeyTime ="0" Value ="#FFffe575" />
</ colorAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.Targetname ="Background" >
< EasingDoubleKeyFrame KeyTime ="0" Value ="1" />
</ DoubleAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.Targetname ="grID" >
< EasingDoubleKeyFrame KeyTime ="0" Value ="0" />
</ DoubleAnimationUsingKeyFrames >
< colorAnimationUsingKeyFrames Storyboard.TargetProperty ="(border.borderBrush).(SolIDcolorBrush.color)" Storyboard.Targetname ="Background" >
< EasingcolorKeyFrame KeyTime ="0" Value ="#FFC28A30" />
</ colorAnimationUsingKeyFrames >
</ Storyboard >
</ VisualState >
< VisualState x:name ="Disabled" >
< Storyboard >
< DoubleAnimationUsingKeyFrames Storyboard.TargetProperty ="Opacity" Storyboard.Targetname ="DisabledVisualElement" >
< SplineDoubleKeyFrame KeyTime ="0" Value =".55" />
</ DoubleAnimationUsingKeyFrames >
</ Storyboard >
</ VisualState >
</ VisualStateGroup >
< VisualStateGroup x:name ="CheckStates" >
< VisualState x:name ="Checked" >
< Storyboard >
< colorAnimationUsingKeyFrames Storyboard.TargetProperty ="(border.Background).(SolIDcolorBrush.color)" Storyboard.Targetname ="Background2" >
< SplinecolorKeyFrame KeyTime ="0" Value ="#FFffe575" />
</ colorAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.Targetname ="Background2" >
< EasingDoubleKeyFrame KeyTime ="0" Value ="1" />
</ DoubleAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.Targetname ="grID2" >
< EasingDoubleKeyFrame KeyTime ="0" Value ="0" />
</ DoubleAnimationUsingKeyFrames >
< colorAnimationUsingKeyFrames Storyboard.TargetProperty ="(border.borderBrush).(SolIDcolorBrush.color)" Storyboard.Targetname ="Background2" >
< EasingcolorKeyFrame KeyTime ="0" Value ="#FFC28A30" />
</ colorAnimationUsingKeyFrames >
</ Storyboard >
</ VisualState >
< VisualState x:name ="Unchecked" />
</ VisualStateGroup >
< VisualStateGroup x:name ="Focusstates" >
< VisualState x:name ="Focused" >
</ VisualState >
< VisualState x:name ="Unfocused" />
</ VisualStateGroup >
</ visualstatemanager.VisualStateGroups >
< border x:name ="Background" borderThickness =" {TemplateBinding borderThickness} " Background ="#FFFDF7E2" CornerRadius ="3" borderBrush ="#FFF0C958" Opacity ="0" >
< GrID x:name ="grID" margin ="1" >
< GrID.Background >
< linearGradIEntBrush EndPoint ="0.5,0" >
< GradIEntStop color ="#FFFDF7E2" Offset ="1" />
< GradIEntStop color ="#FFFCEDB3" />
</ linearGradIEntBrush >
</ GrID.Background >
</ GrID >
</ border >
< border x:name ="Background2" borderThickness =" {TemplateBinding borderThickness} " Background ="#FFFDF7E2" CornerRadius ="3" borderBrush ="#FFF0C958" Opacity ="0" >
< GrID x:name ="grID2" margin ="1" >
< GrID.Background >
< linearGradIEntBrush EndPoint ="0.5,0" >
< GradIEntStop color ="#FFFDF7E2" Offset ="1" />
< GradIEntStop color ="#FFFCEDB3" />
</ linearGradIEntBrush >
</ GrID.Background >
</ GrID >
</ border >
< ContentPresenter x:name ="contentPresenter" ContentTemplate =" {TemplateBinding ContentTemplate} " Content =" {TemplateBinding Content} " HorizontalAlignment =" {TemplateBinding HorizontalContentAlignment} " margin =" {TemplateBinding padding} " VerticalAlignment =" {TemplateBinding VerticalContentAlignment} " />
< Rectangle x:name ="DisabledVisualElement" Fill ="#FFFFFFFF" IsHitTestVisible ="false" Opacity ="0" RadiusY ="3" RadiusX ="3" />
< Rectangle x:name ="FocusVisualElement" IsHitTestVisible ="false" margin ="1" Opacity ="0" RadiusY ="2" RadiusX ="2" stroke ="#FF6DBDD1" strokeThickness ="1" />
</ GrID >
</ ControlTemplate >
</ Setter.Value >
</ Setter >
</ Style >
</ ResourceDictionary >
3、演示 Compositetransform 的效果
Compositetransform.xaml 代码 < navigation:Page x:Class ="Silverlight40.UI.Compositetransform"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
Title ="Compositetransform Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="left" margin ="100,0" >
<!--
Compositetransform - 将多种转换方式合而为一
CenterX - 转换中心点的 X 坐标
CenterY - 转换中心点的 Y 坐标
Rotation - 顺时针旋转角度
ScaleX - 沿 X 轴方向上的缩放比例
ScaleY - 沿 Y 轴方向上的缩放比例
SkewX - X 轴扭曲角度
SkewY - Y 轴扭曲角度
TranslateX - 沿 X 轴方向上的平移距离
TranslateY - 沿 Y 轴方向上的平移距离
-->
< Rectangle Height ="100" WIDth ="100" Fill ="Red" >
< Rectangle.Rendertransform >
< Compositetransform SkewX ="30" Rotation ="60" ScaleX ="0.6" ScaleY ="0.3" />
</ Rectangle.Rendertransform >
</ Rectangle >
<!-- 用 transformGroup 的方式达到上面的 Compositetransform 的相同效果 -->
< Rectangle Height ="100" WIDth ="100" Fill ="Red" >
< Rectangle.Rendertransform >
< transformGroup >
< Scaletransform ScaleX ="0.6" ScaleY ="0.3" />
< Skewtransform AngleX ="30" />
< Rotatetransform Angle ="60" />
</ transformGroup >
</ Rectangle.Rendertransform >
</ Rectangle >
</ StackPanel >
</ GrID >
</ navigation:Page >
4、演示如何 Drag&Drop 外部文件到 Silverlight 程序中
DragAndDrop.xaml 代码 < navigation:Page x:Class ="Silverlight40.UI.DragAndDrop"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
Title ="DragAndDrop Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="left" >
<!-- 对 Silverlight 中的 UIElement 进行拖动的 Demo 详见这里 -->
< Hyperlinkbutton NavigateUri ="http://www.cnblogs.com/webabcd/archive/2008/11/13/1332450.HTML" Targetname ="_blank" Content ="在 Silverlight 中拖放 UIElement 的 Demo" />
<!-- 这是 DropTarget -->
< ListBox name ="ListBox" Background ="lightBlue" Height ="400" >
< ListBoxItem Content ="拖动外部文件到这里,以查看演示效果" />
</ ListBox >
</ StackPanel >
</ GrID >
</ navigation:Page >
DragAndDrop.xaml.cs 代码 /*
* 拖放 *** 作的 Demo - Silverlight 4.0 支持拖动外部文件到 Silverlight 程序中(支持多文件拖放,但是不支持文件夹拖放)
*
* UIElement.AllowDrop - 指定 UIElement 是否可以用作于 DropTarget(拖放 *** 作的放目标)。默认值为 false
* UIElement.dragenter - 拖动外部文件进入到 UIElement 时所触发的事件(事件参数类型为:DragEventArgs)
* UIElement.DragLeave - 拖动外部文件离开 UIElement 时所触发的事件(事件参数类型为:DragEventArgs)
* UIElement.DragOver - 拖动外部文件在 UIElement 中移动时所触发的事件(事件参数类型为:DragEventArgs)
* UIElement.Drop - 拖动外部文件在 UIElement 中放开时所触发的事件(事件参数类型为:DragEventArgs)
* DragEventArgs - 拖放 *** 作所触发的拖放事件的事件参数
* DragEventArgs.Data - 获取与拖放事件相关联的数据(IDataObject 类型)
* IDataObject.GetData(DataFormats.fileDrop) - 返回被拖放的外部文件的 fileInfo 数组
*/
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;
using System.windows.Navigation;
using System.IO;
using System.windows.Markup;
using System.windows.Media.Imaging;
namespace Silverlight40.UI
{
public partial class DragAndDrop : Page
{
public DragAndDrop()
{
InitializeComponent();
}
protected overrIDe voID OnNavigatedTo(NavigationEventArgs e)
{
ListBox.AllowDrop = true ;
ListBox.dragenter += new DragEventHandler(ListBox_dragenter);
ListBox.DragLeave += new DragEventHandler(ListBox_DragLeave);
ListBox.DragOver += new DragEventHandler(ListBox_DragOver);
ListBox.Drop += new DragEventHandler(ListBox_Drop);
}
voID ListBox_dragenter( object sender, DragEventArgs e)
{
ListBox.Items.Add( " dragenter " );
}
voID ListBox_DragLeave( object sender, DragEventArgs e)
{
ListBox.Items.Add( " DragLeave " );
}
voID ListBox_DragOver( object sender, DragEventArgs e)
{
// ListBox.Items.Add("DragOver");
}
voID ListBox_Drop( object sender, DragEventArgs e)
{
ListBox.Items.Add( " Drop " );
if (e.Data == null )
return ;
IDataObject dataObject = e.Data as IDataObject;
fileInfo[] files = dataObject.GetData(DataFormats.fileDrop) as fileInfo[];
foreach (fileInfo file in files)
{
if (file.Exists)
ListBox.Items.Add( " filename: " + file.name + " [fileSize: " + file.Length + " ] " );
else
ListBox.Items.Add( " 文件无效 " );
}
}
}
}
OK
[源码下载] 总结
以上是内存溢出为你收集整理的稳扎稳打Silverlight(47) - 4.0UI之 *** 作剪切板, 隐式样式, CompositeTransform, 拖放外部文件到程序中全部内容,希望文章能够帮你解决稳扎稳打Silverlight(47) - 4.0UI之 *** 作剪切板, 隐式样式, CompositeTransform, 拖放外部文件到程序中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)