上一節(jié)簡(jiǎn)單介紹了通過粘貼板來共享數(shù)據(jù),這一節(jié)將會(huì)添加更為強(qiáng)大的功能哦。
以下就是大概的樣式了,隨便看看就好了,這都不是重點(diǎn)。
<Grid Background="AliceBlue">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition />
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" FontSize="25" Foreground="Red" Text="共享源示例" Margin="12"/>
<ScrollViewer Grid.Row="1" Margin="14" VerticalScrollMode="Auto" HorizontalScrollMode="Disabled">
<StackPanel>
<StackPanel Orientation="Horizontal">
<RadioButton x:Name="radioBtnText" Foreground="Aqua" FontWeight="Bold" FontSize="22"
Content="共享文本" Checked="OnChecked" GroupName="g" Tag="text"/>
<RadioButton x:Name="radioBtnImg" Foreground="Aqua" FontWeight="Bold" FontSize="22"
Content="共享圖像" Margin="12,0,0,0" Checked="OnChecked" GroupName="g" Tag="image"/>
<RadioButton x:Name="radioBtnFile" Foreground="Aqua" FontWeight="Bold" FontSize="22"
Content="共享文件" Margin="12,0,0,0" Checked="OnChecked" GroupName="g" Tag="file"/>
</StackPanel>
<StackPanel Name="stackPText" Visibility="Collapsed" Margin="8">
<TextBlock Text="共享文本" FontSize="25"/>
<TextBlock Foreground="Gold" FontSize="25" Text="輸入要共享的內(nèi)容" />
<TextBox x:Name="tBlockText" Foreground="Gold" />
</StackPanel>
<StackPanel Name="stackPImg" Visibility="Collapsed" Margin="8">
<TextBlock Text="共享圖像" FontSize="25"/>
<TextBlock Foreground="Gold" FontSize="25" Text="共享以下圖片"/>
<Image Width="600" Height="400" Stretch="UniformToFill" HorizontalAlignment="Left"
Margin="1,5,0,5" Source="Assets/SpaceNeedle1.jpg"/>
</StackPanel>
<StackPanel Name="stackPFile" Visibility="Collapsed" Margin="8">
<TextBlock Text="共享文件" FontSize="28"/>
<TextBlock Foreground="Gold" FontSize="25" Text="選擇要共享的文件"/>
<StackPanel>
<Button Content="選擇文件" Click="OnPickFile"/>
<TextBlock x:Name="tBlockFile" Foreground="Gold" FontSize="24"/>
</StackPanel>
</StackPanel>
</StackPanel>
</ScrollViewer>
<Button Grid.Row="2" Name="btnShare" Margin="12" Content="確定共享" FontSize="35"
FontFamily="隸書" Foreground="Azure" Background="Black" Click="btnShare_Click"/>
</Grid>
這里通過 3 個(gè) StackPanel 的“顯示“與”隱藏“來達(dá)到在一個(gè)位置顯示 3 個(gè)界面的功能,然后在后臺(tái)通過以下方法更改 Visibility 屬性。
private void OnChecked(object sender, RoutedEventArgs e)
{
RadioButton rbtn = sender as RadioButton;
if (rbtn != null)
{
string tag = rbtn.Tag.ToString();
switch (tag)
{
case "text":
this.stackPText.Visibility = Windows.UI.Xaml.Visibility.Visible;
this.stackPImg.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
this.stackPFile.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
break;
case "image":
this.stackPText.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
this.stackPImg.Visibility = Windows.UI.Xaml.Visibility.Visible;
this.stackPFile.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
break;
case "file":
this.stackPText.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
this.stackPImg.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
this.stackPFile.Visibility = Windows.UI.Xaml.Visibility.Visible;
break;
default:
this.stackPText.Visibility = Windows.UI.Xaml.Visibility.Visible;
this.stackPImg.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
this.stackPFile.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
break;
}
}
}
以下是核心代碼,通過 RadioButton 的選擇來共享不同的內(nèi)容。這里沒有進(jìn)行 try、catch 異常檢測(cè),但在實(shí)際工程中則是必要的,因?yàn)槿绻悴还蚕砣魏蝺?nèi)容而點(diǎn)擊共享按鈕你就知道了……
void MainPage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
var deferral = args.Request.GetDeferral();
if (radioBtnText.IsChecked == true)
{
args.Request.Data.Properties.Title = "共享文本";
args.Request.Data.Properties.Description = "共享你輸入的文本數(shù)據(jù)。";
args.Request.Data.SetText(this.tBlockText.Text);
}
else if (radioBtnImg.IsChecked == true)
{
args.Request.Data.Properties.Title = "共享圖像";
args.Request.Data.Properties.Description = "共享以下圖片。";
args.Request.Data.SetBitmap(Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri
(new Uri("ms-appx:///Assets/SpaceNeedle1.jpg")));
}
else if (radioBtnFile.IsChecked == true)
{
args.Request.Data.Properties.Title = "共享文件";
args.Request.Data.Properties.Description = "共享你選擇的文件。";
var file = this.tBlockFile.Tag as Windows.Storage.StorageFile;
List<IStorageItem> files = new List<IStorageItem>();
files.Add(file);
args.Request.Data.SetStorageItems(files);
}
deferral.Complete();
}
選擇文件的方法我們?cè)谇懊嬉捕冀榻B過了,直接貼代碼……
private async void OnPickFile(object sender, RoutedEventArgs e)
{
Windows.Storage.Pickers.FileOpenPicker picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".mp3");
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".docx");
picker.FileTypeFilter.Add(".pptx");
picker.FileTypeFilter.Add(".txt");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
this.tBlockFile.Text = file.Path;
this.tBlockFile.Tag = file;
}
}
當(dāng)然了,記得下面這些操作……
protected override void OnNavigatedTo(NavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested += MainPage_DataRequested;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested -= MainPage_DataRequested;
}
最后就是共享確認(rèn)按鈕了,一行代碼搞定。
private void btnShare_Click(object sender, RoutedEventArgs e)
{
DataTransferManager.ShowShareUI();
}
以上這個(gè) App,你將需要共享的數(shù)據(jù)從這里發(fā)出,也叫共享源,但共享到哪里了呢?
看到”共享圖像“和”共享以下圖片“想起來剛才的兩行代碼了么?這兩個(gè)屬性就用在了這里。
args.Request.Data.Properties.Title = "共享文本";
args.Request.Data.Properties.Description = "共享你輸入的文本數(shù)據(jù)。";
我們當(dāng)然可以將數(shù)據(jù)共享到郵件、OneNote 里,但如果你是要寫一個(gè)自己的接收共享數(shù)據(jù)的應(yīng)用呢,如何來寫?
接下來就來寫另一個(gè) App 咯,也就是上圖中的 App49 了。首先在清單文件中做如下操作,當(dāng)然了,具體要添加哪些東西大家自己看著辦就好了。
然后添加一個(gè) XAML 頁(yè)面來接收數(shù)據(jù),因?yàn)槟悴豢赡苤蛔屇愕?APP 專門用來接收數(shù)據(jù)咯,所以就不建議在 MainPage 中寫了。
在新頁(yè)面中大概做一下頁(yè)面布局,我的布局通常來說都不是很美觀的……
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Grid x:Name="gridText" Margin="24" Visibility="Collapsed" Grid.Row="0">
<StackPanel>
<TextBlock FontSize="25" Foreground="Red" Text="接收到的文本:"/>
<TextBlock FontSize="30" Foreground="Pink" FontWeight="Bold" x:Name="tbText" Margin="8"/>
</StackPanel>
</Grid>
<Grid x:Name="gridImg" Margin="25" Visibility="Collapsed" Grid.Row="0">
<StackPanel>
<TextBlock FontSize="25" Foreground="Red" Text="接收到的圖像:"/>
<Image x:Name="img" Margin="12" Width="500" Height="400" HorizontalAlignment="Left" Stretch="Uniform"/>
</StackPanel>
</Grid>
<Grid x:Name="gridStorageItems" Margin="25" Visibility="Collapsed" Grid.Row="0">
<StackPanel>
<TextBlock FontSize="25" Foreground="Red" Text="接收到的文件:"/>
<TextBlock FontSize="30" Margin="12" x:Name="tbStorageItem"/>
</StackPanel>
</Grid>
<Button Grid.Row="1" HorizontalAlignment="Center" Margin="0,15,0,20"
Content="完成共享" FontSize="28" Width="200" Click="btnCompleteShare_Click"/>
</Grid>
后臺(tái)代碼中寫以下代碼,核心在于 if 中的 3 個(gè)判斷,就是 3 中共享的文件了咯。
public sealed partial class ShareTargetPage : Page
{
ShareOperation shareOperation = null;
public ShareTargetPage()
{
this.InitializeComponent();
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
ShareOperation sp = e.Parameter as ShareOperation;
if (sp != null)
{
this.shareOperation = sp;
DataPackageView pack = sp.Data;
if (pack.Contains(StandardDataFormats.Text))
{
string s = await pack.GetTextAsync();
this.tbText.Text = s;
this.gridText.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
else if (pack.Contains(StandardDataFormats.Bitmap))
{
var stream = await pack.GetBitmapAsync();
BitmapImage bmp = new BitmapImage();
bmp.SetSource(await stream.OpenReadAsync());
this.img.Source = bmp;
this.gridImg.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
else if (pack.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await pack.GetStorageItemsAsync();
StorageFile file = storageItems[0] as StorageFile;
this.tbStorageItem.Text = file.Name;
this.gridStorageItems.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
}
}
private void btnCompleteShare_Click(object sender, RoutedEventArgs e)
{
this.shareOperation.ReportCompleted();
}
}
接著我們就要來調(diào)試這兩個(gè)程序啦。只需要將接受共享數(shù)據(jù)的 App 按 F5 運(yùn)行后關(guān)掉就好了,因?yàn)樗鼤?huì)部署到本地的,或者也可以在 Build 選項(xiàng)卡中直接部署也是一樣的。然后按 F5 運(yùn)行共享數(shù)據(jù)的數(shù)據(jù)源 App 就好啦。
截圖如下:
這張圖片我壓縮過了,不如太大上傳不了,所以可能看不清楚吧。下面是共享文本數(shù)據(jù)的過程截圖啦。
這個(gè)是共享圖像的截圖,忘了說了,在前面的 SpaceNeedle1.jpg 就是下面這張圖片我已經(jīng)事先添加到工程里了的。
緊接著我們共享這個(gè) docx 文件,卻發(fā)現(xiàn)在共享欄里沒有了 App49,發(fā)生了什么?
下面這首剛才添加的受支持的文件類型,明顯沒有添加 .docx,所以這也是一個(gè)需要注意的地方。
而想知道如何算出 10000 的階層可以看“100的階層真的算不出來嗎?”
更多建議: