none
后台任务的问题!! RRS feed

  • 问题

  • 我声明了一个打开并以序列化方式读取本地xml文件的函数,声明定义如下

     public async Task<List<CheckHistoryItems>> ReadData(string fileName)
            {
                //获取文件
                List<CheckHistoryItems> data = new List<CheckHistoryItems>();
                StorageFile sFile = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
                if (sFile == null)
                {
                    return data;
                }

                using (IInputStream inputStream = await sFile.OpenSequentialReadAsync())
                {
                    //反序列化
                    DataContractSerializer serializer = new DataContractSerializer(typeof(List<CheckHistoryItems>));
                    data = (List<CheckHistoryItems>)serializer.ReadObject(inputStream.AsStreamForRead());
                }
                return data;
            }

    但是出现了如下错误,十分的不解。。。。

    这样的声明也不通过

     public async Task<bool> DoesFileExistAsync(string fileName)
            {//。。。。。        }

    错误 1 Method 'BackgroundTasks.DataSerialization.DoesFileExistAsync(System.String)' has a parameter of type 'System.Threading.Tasks.Task<System.Boolean>' in its signature. Although this generic type is not a valid Windows Runtime type, the type or its generic parameters implement interfaces that are valid Windows Runtime types.  Consider changing the type 'Task' in the method signature to one of the following types instead: Windows.Foundation.IAsyncAction, Windows.Foundation.IAsyncOperation, or one of the other Windows Runtime async interfaces. The standard .NET awaiter pattern also applies when consuming Windows Runtime async interfaces. Please see System.Runtime.InteropServices.WindowsRuntime.AsyncInfo for more information about converting managed task objects to Windows Runtime async interfaces. 

    2012年11月29日 8:11

答案

全部回复

  • 类型不对,'BackgroundTasks.DataSerialization.DoesFileExistAsync(System.String)' 是Task<Boolean>,你的返回是Task<List<CheckHistoryItems>> 了吧……。

    关注WP&Win8产品及体验,并致力于Windows Phone & Windows 8 技术的研究和推广!如果有任何WP&win8问题,欢迎到:http://ask.metrofeng.com 来找我!

    2012年11月30日 2:43
  • Hi,

    在Background task中需要设置成WinRT Component,而WinRT Component的公开类型只接受WinRT类型,因此你可以改成private或者其他的,或者是转换成WinRT类型。


    Aaron Xue [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年11月30日 10:12
    版主