欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wp wp8:后台传输服务

程序员文章站 2022-04-30 10:39:47
...
1.TransferPreferences属性设置:

后台传输服务上传最大文件大小为5M

通过手机网络连接最大下载大小为20M,若超过此大小,属性将自动更改为AllowBattery

通过wifi没有外部电源最大下载大小为100M,若超过此大小,属性设置为None

2.每个应用程序队列中未完成的最大请求个数为25个,最大并发数为2个,对大排队传输数位500

3.网络连接数组3G网络低于50kbps,wifi/usb网络低于100kbps会暂停传输并重试





上代码:

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using wp8BackgroundTransfer.Resources;
using Microsoft.Phone.BackgroundTransfer;

namespace wp8BackgroundTransfer
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();

          
        }

        BackgroundTransferRequest transferRequest;

        private void download_click(object sender, System.Windows.RoutedEventArgs e)
        {

            if (BackgroundTransferService.Requests.Count() > 25)
            {
                return;
            }

            String transferFileName = "http://m.news.cn/version_wp8.txt";
            Uri transferUri;
            try
            {
                transferUri = new Uri(Uri.EscapeUriString(transferFileName), UriKind.RelativeOrAbsolute);
            }
            catch
            {
                return;
            }

            transferRequest = new BackgroundTransferRequest(transferUri);

            transferRequest.TransferPreferences = TransferPreferences.None;

            transferRequest.TransferProgressChanged += new EventHandler<BackgroundTransferEventArgs>(transfer_TransferProgressChanged);

            transferRequest.TransferStatusChanged += new EventHandler<BackgroundTransferEventArgs>(transfer_TransferStatusChanged);

            transferRequest.Method = "GET";

            string downloadFileName = transferFileName.Substring(transferFileName.LastIndexOf("/")+1);

            Uri downloadUri = new Uri("shared/transfers/"+ downloadFileName, UriKind.RelativeOrAbsolute);

            transferRequest.DownloadLocation = downloadUri;

            transferRequest.Tag = downloadFileName;

            try
            {
                BackgroundTransferService.Add(transferRequest);
            }
            catch
            {
                return;
            }

        }

        private void cancel_click(object sender, System.Windows.RoutedEventArgs e)
        {

            if(transferRequest != null)
                removeRequest(transferRequest.RequestId);

        }

        void transfer_TransferProgressChanged(object sender, BackgroundTransferEventArgs e)
        {
           
        }

        void transfer_TransferStatusChanged(object sender, BackgroundTransferEventArgs e)
        {
            BackgroundTransferRequest transfer = e.Request;

            switch (transfer.TransferStatus)
            {
                case TransferStatus.Completed:
                    {
                        if (transfer.StatusCode == 200 || transfer.StatusCode == 206)
                        {
                            removeRequest(transfer.RequestId);
                        }
                        else
                        {
                            removeRequest(transfer.RequestId);
                            if (transfer.TransferError != null)
                            {
                                System.Diagnostics.Debug.WriteLine("失败:"+transfer.TransferError);
                            }
                        }
                    }
                    break;
                case TransferStatus.WaitingForExternalPower:
                    {
                        System.Diagnostics.Debug.WriteLine("电力不足,不能启动");
                    }
                    break;
                case TransferStatus.WaitingForExternalPowerDueToBatterySaverMode:
                    {
                        System.Diagnostics.Debug.WriteLine("省电模式,不能启动");
                    }
                    break;
                case TransferStatus.WaitingForNonVoiceBlockingNetwork:
                    {
                        System.Diagnostics.Debug.WriteLine("等待网络");
                    }
                    break;
                case TransferStatus.WaitingForWiFi:
                    {
                        System.Diagnostics.Debug.WriteLine("等待wifi网络");
                    }
                    break;
            }
        }

        void removeRequest(string id)
        {
            BackgroundTransferRequest request = BackgroundTransferService.Find(id);
            if (request != null)
            {
                BackgroundTransferService.Remove(request);
            }
        }
       
    }
}

MainPage.xaml

<phone:PhoneApplicationPage
    x:Class="wp8BackgroundTransfer.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>


        <StackPanel Orientation="Vertical">


            <Button Content="download" Width="300" Height="100" Click="download_click"></Button>
            <Button Content="cancel" Width="300" Height="100" Click="cancel_click"></Button>
           
        </StackPanel>


    </Grid>

</phone:PhoneApplicationPage>

相关标签: wp