[Wanli journey-Windows App development] How to Use clipboard and journey app

Source: Internet
Author: User

[Wanli journey-Windows App development] How to Use clipboard and journey app

I remember the concept that a smartphone was so popular when it first came out: "A mobile phone that can be copied and pasted is a smartphone ". Now it seems that this is just an old feature, but it is very useful. Now we will try to implement this function.

The English name of the Clipboard is Clipboard, which is also its class name.

The code in XAML is as follows:

<Grid Background = "{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid Margin = "12" HorizontalAlignment = "Left" verticalignment = "Top" Width = "500" Height = "500"> <grid. rowDefinitions> <RowDefinition Height = "Auto"/> <RowDefinition Height = "*"/> </Grid. rowDefinitions> <Button Grid. row = "0" Name = "btnClip" Margin = "0, 3, 0, 16 "Content =" Paste "FontSize =" 32 "Click =" btnClip_Click "IsEnabled =" False "/> <ScrollViewer Name =" scrollView "Grid. row = "1" Visibility = "Collapsed"> <TextBlock Margin = "12" Name = "tBlockClipboard" FontSize = "35" Foreground = "Gainsboro" TextWrapping = "Wrap"/> </ScrollViewer> </Grid>

Write the following method in the background code:

        void Clipboard_ContentChanged(object sender, object e)        {            DataPackageView pv = Clipboard.GetContent();            if (pv.Contains(StandardDataFormats.Text))            {                btnClip.IsEnabled = true;            }                       }

StandardDataFormats is a standard data format. check whether it is Text. If so, make the previous Button available (previously set to unavailable and displayed in gray ).

Standard data formats include Bitmap, HTML, RTF, StorageItems, Text, and Uri.

Then write the following code in the button Click event:

        private async void btnClip_Click(object sender, RoutedEventArgs e)        {                            var txt = await Clipboard.GetContent().GetTextAsync();                tBlockClipboard.Text = txt;                   }

Here we use the GetContent () method of the Clipboard class to retrieve the data of the DataPackageView object in the Clipboard. Similarly, we also use SetContent () to store the data in the Clipboard. There is also a Clear event to Clear the clipboard. The Flush event writes data from the source to the clipboard and remains in the clipboard after the application exits. The ContentChanged event is automatically activated when the data content stored in the clipboard changes to monitor the effect of the clipboard content changes.

        protected override void OnNavigatedTo(NavigationEventArgs e)        {            Clipboard.ContentChanged += Clipboard_ContentChanged;        }        protected override void OnNavigatedFrom(NavigationEventArgs e)        {            Clipboard.ContentChanged -= Clipboard_ContentChanged;        }        void Clipboard_ContentChanged(object sender, object e)        {            DataPackageView pv = Clipboard.GetContent();            if (pv.Contains(StandardDataFormats.Text)||pv.Contains(StandardDataFormats.Bitmap))            {                btnClip.IsEnabled = true;            }                       }

You can try it, but we can do more, right?
The complete code is as follows:

<Grid Background = "{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid Margin = "12" HorizontalAlignment = "Left" verticalignment = "Top" Width = "500" Height = "500"> <grid. rowDefinitions> <RowDefinition Height = "Auto"/> <RowDefinition Height = "*"/> </Grid. rowDefinitions> <Button Grid. row = "0" Name = "btnClip" Margin = "0, 3, 0, 16 "Content =" Paste "FontSize =" 32 "Click =" btnClip_Click "IsEnabled =" False "/> <ScrollViewer Name =" scrollView "Grid. row = "1" Visibility = "Collapsed"> <TextBlock Margin = "12" Name = "tBlockClipboard" FontSize = "35" Foreground = "Gainsboro" TextWrapping = "Wrap"/> </ScrollViewer> <Image x: name = "imgClicpboard" Grid. row = "1" Margin = "5" Stretch = "Uniform" Visibility = "Collapsed"/> </Grid>
    public sealed partial class MainPage : 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;        }        void Clipboard_ContentChanged(object sender, object e)        {            DataPackageView pv = Clipboard.GetContent();            if (pv.Contains(StandardDataFormats.Text)||pv.Contains(StandardDataFormats.Bitmap))            {                btnClip.IsEnabled = true;            }                       }                     private async void btnClip_Click(object sender, 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;                var txt = await Clipboard.GetContent().GetTextAsync();                tBlockClipboard.Text = txt;            }            else if(pv.Contains(StandardDataFormats.Bitmap))            {                imgClicpboard.Visibility = Visibility;                var bmp = await Clipboard.GetContent().GetBitmapAsync();                Windows.UI.Xaml.Media.Imaging.BitmapImage bitMap = new Windows.UI.Xaml.Media.Imaging.BitmapImage();                bitMap.SetSource(await bmp.OpenReadAsync());                this.imgClicpboard.Source = bitMap;            }        }    }

Now it can copy images ~

Thank you for your visit and hope to help you. Welcome to your attention, favorites, and comments.

For this article to get an axe and a question, please indicate the source:
Http://blog.csdn.net/nomasp

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.