win8/Metro开发系九 win8特性 share search title snap等

摘要:最后一节,win8中设置特有的一些新特性,让自己的程序也炫起来!!介绍简单的方法具体的可以下载微软的sample来拿过来参考!!这里起抛砖引玉的作用,让你可以快速的了解并做出一个winRT的metro应用!

share 契约

可以分享问题图片及富媒体,看一看api都提供了

大概实现方式下面给出:

首先注册事件

DataTransferManager.GetForCurrentView().DataRequested += DuiLianS_DataRequested;

View Code
 1  void DuiLianS_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
 2         {
 3 
 4             if (obj!=null)
 5             {
 6                 var request = args.Request;
 7                 request.Data.Properties.Title = "来自:妙笔生花";
 8                 request.Data.Properties.Description = "作者:星 对联生成";
 9                 // Share recipe text
10                 var recipe = "\r\n上联:\r\n";
11                 recipe += String.Join("\r\n", input.Text);
12                 if (obj.XialianSystemGeneratedSets.Count == 2)
13                 {
14                     recipe += (getArray(obj.XialianSystemGeneratedSets[0].XialianCandidates));
15                     recipe += (getArray(obj.XialianSystemGeneratedSets[1].XialianCandidates));
16                 }
17                 if (obj.XialianSystemGeneratedSets.Count == 1)
18                 {
19                     recipe += (getArray(obj.XialianSystemGeneratedSets[0].XialianCandidates));
20                 }
21                 if (obj.XialianWellKnownSets != null)
22                 {
23                     recipe += (getArray(obj.XialianWellKnownSets[0].XialianCandidates));
24                 }
25                 request.Data.SetText(recipe);
26             }
27         }

最后别忘了把事件給注销掉,小心报错

DataTransferManager.GetForCurrentView().DataRequested -= DuiLianS_DataRequested;

search 契约

1.在app.cs里激活搜索契约

代码:

View Code
 ////<summary>
         ////This is the handler for Search activation.
         ////</summary>
         ////<param name="args">This is the list of arguments for search activation, including QueryText and Language</param>
        protected override void OnSearchActivated(SearchActivatedEventArgs args)
        {
            SeacherReslut.Activate(args.QueryText);
           // SearchResultsPage1.Activate(args.QueryText);
        }

2.在搜索页面启动搜索

代码示例:

View Code
 1  /// <summary>跳转方法
 2         /// 
 3         /// </summary>
 4         /// <param name="queryText"></param>
 5         public static void Activate(String queryText)
 6         {
 7             // If the window isn't already using Frame navigation, insert our own frame
 8             var previousContent = Window.Current.Content;
 9             var frame = previousContent as Frame;
10             if (frame == null)
11             {
12                 frame = new Frame();
13                 Window.Current.Content = frame;
14             }
15             //Use navigation to display the results, packing both the query text and the previous
16             //  Window content into a single parameter object
17             frame.Navigate(typeof(SeacherReslut),
18                 new Tuple<String, UIElement>(queryText, previousContent));
19             // The window must be activated in 15 seconds
20             Window.Current.Activate();
21         }

然后接收到queryText可以对queryText进行处理然后,绑定到搜索页面

3.添加搜索建议让你的程序更加人性化

您可以使用localContentSuggestionSettings,建议的基础上从Windows的本地文件,只使用几行代码添加。或者,你也可以注册的suggestionsrequested事件,建立自己的列表的建议。这些都是建议你从其他来源获取,像一个本地定义的列表或Web服务。

参考代码:可以去微软的sample示例去下载下来参考一下

 

设置契约:

基本上和搜索差不多,下载个sample看一下吧

 

磁贴title:

http://www.cnblogs.com/fengyun1989/archive/2012/07/26/2610200.html

可以到这里看看,这里介绍的比较详细!

就不罗嗦了,如果没闲心看就给大家一个偷懒的方法

在app.cs里直接把这段代码放入到lanch启动的方法里,让程序一运行就可以更新新的磁贴!

示例代码:

View Code
 1  //动态磁贴
 2             // Note: This sample contains an additional project, NotificationsExtensions.
 3             // NotificationsExtensions exposes an object model for creating notifications, but you can also 
 4             // modify the strings directly. See UpdateTileWithImageWithStringManipulation_Click for an example
 5             // Create notification content based on a visual template.
 6             ITileWideImageAndText01 tileContent = TileContentFactory.CreateTileWideImageAndText01();
 7 
 8             tileContent.TextCaptionWrap.Text = "落笔生花";
 9             tileContent.Image.Src = "ms-appx:///Images/title/redWide.png";
10             tileContent.Image.Alt = "落笔生花";
11 
12             // Users can resize tiles to square or wide.
13             // Apps can choose to include only square assets (meaning the app's tile can never be wide), or
14             // include both wide and square assets (the user can resize the tile to square or wide).
15             // Apps should not include only wide assets.
16 
17             // Apps that support being wide should include square tile notifications since users
18             // determine the size of the tile.
19 
20             // create the square template and attach it to the wide template
21             ITileSquareImage squareContent = TileContentFactory.CreateTileSquareImage();
22             tileContent.TextCaptionWrap.Text = "落笔生花";
23             squareContent.Image.Src = "ms-appx:///images/title/graySquare.png";
24             squareContent.Image.Alt = "落笔生花";
25             tileContent.SquareContent = squareContent;
26 
27             // Send the notification to the app's application tile.
28             TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());

不过用之前要引用微软的一个辅助类库

这个类库挺通用挺强大的,通知也可以用得到的!所以想偷懒就收藏一下吧

c#的示例下载地址给大家贴出来,类库包含在里面呵呵大家也可以参考着这个做!

http://code.msdn.microsoft.com/windowsapps/App-tiles-and-badges-sample-5fc49148/file/51004/22/App%20tiles%20and%20badges%20sample.zip

记得还一个挺全的title示例大家自己搜索一下吧!好像有辅助title还有各种类型的title的示例

 

snap视图:

贴靠视图,为了充分利用屏幕应用场景而设计应用程序,来展示你的程序内容win8提供了很多中视图模式,呵呵其实这也是比较头痛的一点,头痛的就是各种视图更改了,你要更改你的样式,来配合不同模式下程序的完美展示

接下来就来看看怎么在模式更改来修改你响应的样式

首先可以再构造函数里注册监视页面大小改变的事件,以让程序知道视图切换做响应的处理

View Code
1 Window.Current.SizeChanged += Current_SizeChanged;

然后再事件里可以处理响应的大小及其样式

  void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            //var currentViewState = Windows.UI.ViewManagement.ApplicationView.Value;
            //if (currentViewState == ApplicationViewState.Snapped)
            //{
            //   
            //}
            //else
            //{
            //    GridLayout.ColumnDefinitions[0].Width = new GridLength(e.Size.Width / 2);
            
            //}
        }

  ApplicationViewState其中这是个枚举大家可以看看都有哪些视图

然后进入每种视图做相应的处理

如需在这些不同的状态间进行切换,使用 VisualStateManager会更方面,微软的示例,模板都是那么做的!!

呵呵,我没怎么研究

大家可以研究下然后一起交流下

参考文章:http://blogs.msdn.com/b/windowsappdev_cn/archive/2012/04/25/getting-your-pixels.aspx

 

 

上面几篇文章大概就是这一个两天的小东西用到的,那两天做学习win8的开发遇到的一些问题,可能还有小问题没有说,只是挑了主线主要知识点提了一下,说的也比较浅!还请大家见谅!!

源码稍后有空整理好会共享给大家!!

最后,特别感谢前一段时间王飞老师的细心指导,才会那么快速的入门!!!

这个项目告一段落!有空碰到好玩的东西再和大家一起分享,望大家可以一同学习一起进步!

                                                              星

                                                            2012.11.10

posted @ 2012-11-10 19:12  ~星~  阅读(562)  评论(0编辑  收藏  举报