Remember the smart phone just came out that would compare to a concept of fire "can copy and paste the phone is a smartphone." Now, this is just an old-fashioned feature, but the real use is very powerful, so now let's try how to do this function.
The English name of the pasteboard is called Clipboard, which is also its class name.
New project This is not said, in the XAML code is as follows:
<Grid Background="{StaticResource Applicationpagebackgroundthemebrush}"> <Gridmargin=" A"Horizontalalignment="left"Verticalalignment="Top"Width="a"height="a"> <Grid. Rowdefinitions> <rowdefinition height="Auto"/> <rowdefinition height="*"/> </Grid. Rowdefinitions> <Button Grid.Row="0"Name="Btnclip"margin="0,3,0,16"Content="Paste" FontSize=" the"click="Btnclip_click"Isenabled="False"/> <scrollviewer name="ScrollView" Grid.Row="1"visibility="collapsed"> <textblock margin=" A"Name="Tblockclipboard" FontSize="very"Foreground="Gainsboro"textwrapping="Wrap"/> </ScrollViewer> </Grid> </Grid>
Write a method in the background code:
void Clipboard_ContentChanged(objectobject e) { DataPackageView pv = Clipboard.GetContent(); if (pv.Contains(StandardDataFormats.Text)) { true; } }
Standarddataformats is the standard data format, which determines if it is text and, if so, makes the previous button buttons available (previously set to unavailable and grayed out).
Standard data formats are Bitmap,html,rtf,storageitems,text,uri and so on.
Then write the following code in the Click event of the button:
privateasyncvoidbtnClip_Click(object sender, RoutedEventArgs e) { varawait Clipboard.GetContent().GetTextAsync(); tBlockClipboard.Text = txt; }
Here we use the GetContent () method of the Clipboard class to remove the Datapackageview object data from the Clipboard and, similarly, setcontent () for storing the data in the Clipboard. There is also a clear event to empty the Clipboard, and the Flush event writes the data from the source to the Clipboard, and remains in the Clipboard after the application exits. There are also contentchanged events that automatically activate when the content of the data stored in the Clipboard changes to achieve the effect of monitoring the contents of the Clipboard.
protected Override void Onnavigatedto(NavigationEventArgs e) {clipboard.contentchanged + = clipboard_contentchanged; }protected Override void Onnavigatedfrom(NavigationEventArgs e) {clipboard.contentchanged-= clipboard_contentchanged; }voidClipboard_contentchanged (ObjectSenderObjectE) {Datapackageview PV = clipboard.getcontent ();if(PV. Contains (standarddataformats.text) | | Pv. Contains (Standarddataformats.bitmap)) {btnclip.isenabled =true; } }
You can try it, it's done, but we can do more, right?
The complete code is as follows:
<Grid Background="{StaticResource Applicationpagebackgroundthemebrush}"> <Gridmargin=" A"Horizontalalignment="left"Verticalalignment="Top"Width="a"height="a"> <Grid. Rowdefinitions> <rowdefinition height="Auto"/> <rowdefinition height="*"/> </Grid. Rowdefinitions> <Button Grid.Row="0"Name="Btnclip"margin="0,3,0,16"Content="Paste" FontSize=" the"click="Btnclip_click"Isenabled="False"/> <scrollviewer name="ScrollView" Grid.Row="1"visibility="collapsed"> <textblock margin=" A"Name="Tblockclipboard" FontSize="very"Foreground="Gainsboro"textwrapping="Wrap"/> </ScrollViewer> <ImageX:name="Imgclicpboard" Grid.Row="1"margin="5"Stretch="Uniform"visibility="collapsed"/> </Grid> </Grid>
Public Sealed Partial classMainpage:page { Public MainPage() { This. InitializeComponent (); }protected Override void Onnavigatedto(NavigationEventArgs e) {clipboard.contentchanged + = clipboard_contentchanged; }protected Override void Onnavigatedfrom(NavigationEventArgs e) {clipboard.contentchanged-= clipboard_contentchanged; }voidClipboard_contentchanged (ObjectSenderObjectE) {Datapackageview PV = clipboard.getcontent ();if(PV. Contains (standarddataformats.text) | | Pv. Contains (Standarddataformats.bitmap)) {btnclip.isenabled =true; } }Private Async void Btnclip_click(Objectsender, RoutedEventArgs e) {scrollview.visibility = visibility.collapsed; imgclicpboard.visibility = visibility.collapsed; Tblockclipboard.text =" "; Imgclicpboard.source =NULL; Datapackageview PV = clipboard.getcontent ();if(PV. Contains (Standarddataformats.text)) {scrollview.visibility = Visibility;varTXT =awaitClipboard.getcontent (). Gettextasync (); Tblockclipboard.text = txt; }Else if(PV. Contains (Standarddataformats.bitmap)) {imgclicpboard.visibility = Visibility;varBMP =awaitClipboard.getcontent (). Getbitmapasync (); Windows.UI.Xaml.Media.Imaging.BitmapImage BitMap =NewWindows.UI.Xaml.Media.Imaging.BitmapImage (); Bitmap.setsource (awaitBmp. OpenReadAsync ()); This. Imgclicpboard.source = BitMap; } } }
Now it can also copy pictures Oh ~
Thank you for your visit and hope to help you. We welcome your attention, collection and comment.
To make this article get treatise and ask questions, reprint please indicate the source:
Http://blog.csdn.net/nomasp
"Miles Journey--windows App development" How to use Pasteboard