Windows Store apps开发[30]移动控件位置
来源:互联网 发布:mcs-51单片机寻址方式 编辑:程序博客网 时间:2024/06/09 19:43
注:本文由BeyondVincent(破船)原创首发
转载请注明出处:BeyondVincent(破船)@DevDiv.com
更多内容请查看下面的帖子
[DevDiv原创]Windows 8 开发Step by Step
今天看到坛子里有人问如何移动图片
http://www.devdiv.com/thread-135266-1-1.html
刚好小弟我略懂,就为大家写一个小示例:如何在画面中用手势移动一个控件的位置,就不多说原理,直接上代码和效果图,其实原理也很简单哦。要是不懂的话,跟帖,我会随时来解答滴。
这里有官方参考资料
http://msdn.microsoft.com/en-us/library/windows/apps/hh761498.aspx
1、新建一个Blank App
2、在MainPage.xaml文件中添加如下代码
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Image Source="/Assets/1.jpg" ManipulationStarted="Image_ManipulationStarted_1" ManipulationDelta="Image_ManipulationDelta_1" ManipulationCompleted="Image_ManipulationCompleted_1" ManipulationMode="All" Margin="342,34,591,337"> <Image.RenderTransform> <TranslateTransform x:Name="translateTransform"/> </Image.RenderTransform> </Image> <Image Source="/Assets/BeyondVincentWin8Logo.png" Height="100" Margin="568,658,0,10" ManipulationStarted="Image_ManipulationStarted_1" ManipulationDelta="Image_ManipulationDelta_1" ManipulationCompleted="Image_ManipulationCompleted_1" ManipulationMode="All" > <Image.RenderTransform> <TranslateTransform x:Name="translateTransform1"/> </Image.RenderTransform> </Image></Grid>
代码中要注意下面三个东西是添加事件处理
ManipulationStarted
ManipulationDelta
ManipulationCompleted
记得再加上ManipulationMode="All"
3、MainPage.xaml.cs文件中,添加如下代码
private void Image_ManipulationStarted_1(object sender, ManipulationStartedRoutedEventArgs e){ e.Handled = true;}private void Image_ManipulationDelta_1(object sender, ManipulationDeltaRoutedEventArgs e){ Image image = sender as Image; if (image.RenderTransform.Equals(translateTransform)) { this.translateTransform.X += e.Delta.Translation.X; this.translateTransform.Y += e.Delta.Translation.Y; } else { this.translateTransform1.X += e.Delta.Translation.X; this.translateTransform1.Y += e.Delta.Translation.Y; }}private void Image_ManipulationCompleted_1(object sender, ManipulationCompletedRoutedEventArgs e){ e.Handled = true;}
4、运行效果如下:
用鼠标可以拖动图片
5、代码下载地址
- Windows Store apps开发[30]移动控件位置
- Windows Store apps开发[52]Button控件的自定义——模仿开始屏幕中磁贴的按下、移动效果
- Windows Store apps开发[61]Windows 8 开发31日-第04日-新控件
- Windows Store apps开发[25]Metro style apps改名为Windows Store apps
- Windows Store apps开发[1]平台简介
- Windows Store apps开发[13]应用程序生命周期
- Windows Store apps开发[44]AppBar总结
- Windows Store apps开发[49]自定义MessageDialog
- Windows Store apps开发[50]自定义Popup
- Windows Store apps开发[23]创建Windows Store应用程序包
- Windows Store apps开发[41]FlipView控件的介绍和使用
- Windows Store apps开发[35]如何创建自己的Windows Store账号来提交Apps
- Windows Store apps开发[57]Windows 8 开发31日
- Windows Store apps开发[39]Windows 8中的Semantic Zoom(语义缩放)(2)SemanticZoom控件的使用
- Windows Store apps开发[2]开发环境搭建与HelloWorld
- Windows Store apps开发[0]Step by Step
- Windows Store apps开发[3]应用程序栏(AppBar)的使用
- Windows Store apps开发[4]弹出画面(Flayouts)
- java减少数据访问层代码—反射3—数据访问层就一个接口一个实现
- 十字翻转棋的解法(高斯消元法)
- 对Bridge模式的理解
- ASP.net--Web服务端控件
- 写得蛮好的linux学习笔记!!!
- Windows Store apps开发[30]移动控件位置
- Sublime Text 快捷键
- 用户量决定一切......
- 杭电OJ——1006 Tick and Tick
- JVM调优系列(一):总结 -Xms -Xmx -Xmn -Xss
- 《大话设计模式》读书笔记:原型模式与Java的对象克隆
- Linux权限详解(转)
- 完整无线视频传输程序(已封装)
- WinForm 控件实现半透明效果,并可层叠显示