Blog Park client UAP development essay-connect your App to the world (2): built-in sharing in WinRT, uapwinrt
Is there a kind of feeling that other friends can see when I see a bright blog? Is there an impulse to "not turn into a programmer? On the PC browser, we can see that it is okay to copy the URL directly, and the other side of the IM will be sent over, but if it is the content in the App, it is not so convenient, there is always no way to shout on the other side of the IM: "Next door, Old Wang, there is a blog post on the blog garden, titled 'blog garden client (Universal App) Development essay-adding the wings of sharing for the applications'. Is it really nice? ". It is too troublesome for Old Wang to search again next door. You may say, hi, it's not enough to share it directly. Well, that's right. It's the sharing function. So how can we introduce the sharing feature into our App? Look down.
Similarities and differences between Windows and Windows Phone sharing in the Universal App
Since it is a Universal App, it is natural to share a set of shared code. However, there are still some differences between the two. The difference is that for Windows, because Charm Bar is a system-level menu, and this menu comes with a sharing portal, Windows only needs to prepare the content to be shared in advance, you do not need to provide another sharing portal. Windows Phone does not have this entry, so in addition to preparing content, you also need to explicitly provide a sharing entry through menus or buttons.
Using Windows. ApplicationModel. DataTransfer;
Create a method named RegisterForShare and register the content to be shared in the sharing area:
private void RegisterForShare() { DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView(); dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareTextHandler); }
In this method, we created a DataTransferManager and asked him to set out the ShareTextHandler method during the DataRequested event:
private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs e) { DataRequest request = e.Request; request.Data.Properties.Title = loader.GetString("ShareTitlePrefixText") + this.post.Title; request.Data.Properties.Description = this.post.Summary; string summaryText = (this.post.Author != null ? loader.GetString("ShareContentAuthor") + this.post.Author.Name : "") + "\n" + loader.GetString("ShareContentSummary") + (this.post.Summary.Length > 50 ? this.post.Summary.Substring(0, 50) : this.post.Summary) + "\n" + this.post.Link.Href; request.Data.SetText(summaryText); }
Next, implement this method, create a request, and complete the content in the request. data. properties. title is the content that will be displayed in the share Title. For example, if it is email sharing, the mail Title is request. data. properties. title. If Onenote is shared, the Title of the note will also be request. data. properties. title. Request. Data. Properties. Description is not a required parameter. It is only a reference for remarks and will not be displayed during sharing. The final request. Data. SetText (summaryText); sets the remaining content to be shared. In this way, the content we want to share is filled in as we thought before.
The next step is to call the RegisterForShare method, which can be completed in the constructor.
Sharing portal
If it is Windows, you can directly share it after the constructor calls the RegisterForShare method. The entry is the previously mentioned Charm Bar or menu:
<AppBarButton x: Name = "btn_Share" x: Uid = "ShareBlogButton" Label = "share blog" Click = "btn_assist_click" Icon = "ReShare"/>
Then, in the method you click, the sharing portal is explicitly called:
private void btn_Share_Click(object sender, RoutedEventArgs e) { Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI(); }
In this way, the system's sharing portal will be called up:
Because it is a simulator test, so there is no MS Account, so there is no entry to mail sharing.
We select SMS
You can send the shared content.
Other sharing formats
In addition to SetText to share text content, we can also create HTML, WebLink, files, and other content. The implementation is similar to that of Text. For details, refer:
Text: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh871372.aspx
WebLink: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh973056.aspx
HTML: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh973055.aspx
Files: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh871371.aspx
Summary
Through the above introduction, we can use a small amount of code to quickly share our favorite content with others. We no longer have to worry that the Old Wang next door will not see the rao we have given them.
Share code and change the world!
Windows Phone Store App link:
Bytes
Windows Store App link:
Http://apps.microsoft.com/windows/zh-cn/app/c76b99a0-9abd-4a4e-86f0-b29bfcc51059
GitHub open source link:
Https://github.com/MS-UAP/cnblogs-UAP
MSDN Sample Code:
Https://code.msdn.microsoft.com/CNBlogs-Client-Universal-477943ab