即使使用Task,也无法等待'Void'

问题描述 投票:1回答:1

我有个问题。我试图修改我前一段时间制作的应用程序,它仍在WP Phone上工作,但现在我尝试运行它得到这些错误:

在等待我得到:

不能等待'无效'。

当我将void更改为任务时,错误仍然存​​在

无法等待'无效'

我甚至没有void了。

有人能帮助我吗?

namespace StreamUploadDownload
{
    using System.Threading;
    public partial class Page1 : PhoneApplicationPage
    {
        private PhotoCamera _cam;
        private double _canvasWidth;
        private double _canvasHeight;
        private MediaLibrary _library = new MediaLibrary();
        public int count = 100;
        private static readonly string[] scopes = new string[] { "wl.signin", "wl.basic", "wl.offline_access", "wl.skydrive_update", "wl.skydrive" };
        string comboValue;
        private LiveConnectClient liveClient;
        public int x = 0;
        public int y = 0;  

        public string FileText { get; set; }
        public int ComboNumber { get; set; }
        public int ConnectionOK { get; set; }

        public Page1()
        {
            InitializeComponent();    
        }

        private void OnSessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
        {
            this.liveClient = (e.Status == LiveConnectSessionStatus.Connected) ? new LiveConnectClient(e.Session) : null;
            if (e.Status == LiveConnectSessionStatus.Connected)
            {
                btnSignin.Visibility = Visibility.Collapsed;
                Pildista.Visibility = Visibility.Visible;
                //Pildista2K.Visibility = Visibility.Visible;
                Pildista.Content = "Pildista";    
            }
            else
            {
                Pildista.Visibility = Visibility.Collapsed;
                //Pildista2K.Visibility = Visibility.Collapsed;
                btnSignin.Visibility = Visibility.Visible;
            }
        }    

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true) ||
                 (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) == true))
            {
                if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
                {
                    _cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
                    _cam.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(cam_Initialized);
                    _cam.CaptureImageAvailable += new EventHandler<ContentReadyEventArgs>(cam_CaptureImageAvailable);
                    viewfinderBrush.SetSource(_cam);

                    //CameraButtons.ShutterKeyPressed += OnButtonFullPress;

                    base.OnNavigatedTo(e);
                    if (PhoneApplicationService.Current.State.ContainsKey("Text"))
                    {
                        txtvalue.Text = (string)PhoneApplicationService.Current.State["Text"];
                        FileText = txtvalue.Text;
                    }
                    if (PhoneApplicationService.Current.State.ContainsKey("index"))
                    {
                        ComboNumber = (int)PhoneApplicationService.Current.State["index"];
                    }
                    else
                    {
                        // The camera is not supported on the device.
                        this.Dispatcher.BeginInvoke(delegate()
                        {
                            // Write message.    
                        });

                        // Disable UI.    
                        AFButton.IsEnabled = false;    
                    }    
                }
            }
        }

        private double GetCameraAspectRatio()
        {
            IEnumerable<Size> resList = _cam.AvailableResolutions;

            if (resList.Count<Size>() > 0)
            {
                Size res = resList.ElementAt<Size>(0);
                return res.Width / res.Height;
            }

            return 1;
        }

        void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e)
        {
            if (e.Succeeded)
            {
                this.Dispatcher.BeginInvoke(delegate()
                {
                    _canvasHeight = Application.Current.Host.Content.ActualWidth;
                    _canvasWidth = _canvasHeight * GetCameraAspectRatio();

                    viewfinderCanvas.Width = _canvasWidth;
                    viewfinderCanvas.Height = _canvasHeight;
                });
            }
        }   

        //Failinime andmine ning salvestamine.
        private async void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
        {
            if (ComboNumber == 1)
            {
                comboValue = "O";
            }
            if (ComboNumber == 2)
            {
                comboValue = "T";
            }
            if (ComboNumber == 3)
            {
                comboValue = "S";
            }
            if (ComboNumber == 4)
            {
                comboValue = "P";
            }
            if (ComboNumber == 5)
            {
                comboValue = "A";
            }
            if (ComboNumber == 6)
            {
                comboValue = "M";
            }

            string fileName = String.Format(FileText + "_" + comboValue + "_" + DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss") + ".jpg");    

            try
            {    
                LiveOperationResult operationResult = await this.liveClient.UploadAsync("/me/skydrive", fileName, e.ImageStream, OverwriteOption.Overwrite); //Cannot await 'void'    
            }

            catch (LiveConnectException ex)
            {
                // e.ImageStream.Close();
                // this.infoTextBlock.Text = "Error getting contact info: ";
                // this.infoTextBlock.Visibility = Visibility.Visible;    
            }
            finally
            {    
                e.ImageStream.Close();
                y++;
                Dispatcher.BeginInvoke(delegate()
                {
                    string b = Convert.ToString(y);
                    loobvalue2.Text = b;
                });    
            }    
        }    

        //kaameranupu vajutus.

        private void takephoto_Click(object sender, RoutedEventArgs e)
        {    
            if (_cam != null)
            {    
                _cam.CaptureImage();
                x++;
                string s = x.ToString();
                loobvalue.Text = s;    
            }    
        }   

        // Ühenduse Loomine. Session load.
        private async void connectButton_Click(object sender, RoutedEventArgs e)
        {
            bool connected = false;
            try
            {
                var authClient = new LiveAuthClient("RemovedforWeb");
                LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive" }); // cannot await 'void'

                if (result.Status == LiveConnectSessionStatus.Connected)
                {
                    connected = true;
                    var connectClient = new LiveConnectClient(result.Session);
                    var meResult = await connectClient.GetAsync("me");
                    dynamic meData = meResult.Result; //cannot await 'void'
                    }
                else
                {
                    //btnSignin.Visibility = Visibility.Visible;
                }    
            }
            catch (LiveAuthException ex)
            {

            }

编辑:我添加了更多的代码,并评论有问题的地方

c# windows-phone-8 void
1个回答
5
投票
public async Task Method1 ()
{

}

public async Task<int> Method2 ()
{

}

对于上面的代码,“Method1”不返回任何值,而“Method2”返回“int”值。

int i = await Method2(); //this is correct
await Method2(); //this is correct
int i = await Method1(); //this is NOT correct
await Method1(); //this is also correct

对于以下代码行

LiveOperationResult operationResult = await this.liveClient.UploadAsync("/me/skydrive", fileName, e.ImageStream, OverwriteOption.Overwrite); //Cannot await 'void'

“UploadAsync”方法不会返回任何值,如果您说“无法等待'无效',那就是它的样子”

尝试从代码行中删除“LiveOperationResult operationResult =”并写入 -

await this.liveClient.UploadAsync("/me/skydrive", fileName, e.ImageStream, OverwriteOption.Overwrite);

第二行代码相同 -

LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive" }); // cannot await 'void'

重写为 -

await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive" }); // cannot await 'void'
© www.soinside.com 2019 - 2024. All rights reserved.