代码之家  ›  专栏  ›  技术社区  ›  BumpaRoy

错误CS0029无法隐式转换类型“System”。线程。任务。任务<Type>'到'Type'

  •  0
  • BumpaRoy  · 技术社区  · 2 年前

    获取错误CS0029无法隐式转换类型“System”。线程。任务。任务<peMove。毛伊岛。模型。ErpPart>'到'peMove。毛伊岛。模型。ErpPart’。

    它不能将ErpPart类型转换为ErpPart??这可能是相当直接的,但对于C#(VB编码器)和异步操作来说是新的。我一直在挣扎,只是没有看到(或筋疲力尽)。

    主页.xmal.cs

    async void OnButtonClicked(object sender, EventArgs args)
    {
    
        DocId key = new DocId();
        key.Id1 = "Default";
        key.Id2 = "WF201B";
        key.Id3 = "000";
        ErpPart erpDocument = ErpPart.LoadAsync(key);
        }
    

    类ErpPart代码(部分)

        public static async Task<ErpPart> LoadAsync(DocId key)
        // Static can be used with out declaring an instance.  Non-Static will require and instance.
        {
            string qlQuery = "{ products(filter:{and: [{fac: {eq: \"" + key.Id1 + "\" }}, {fpartno: {eq: \"" + key.Id2 + "\"}}, {frev: {eq: \"" + key.Id3 + "\" }} ]}) " +
                            @"{
                                    items {
                                        id1 : fpartno
                                        id2 : frev
                                        id3 : fac
                                        id4 : fac
                                        name : fdescript
                                        OnHand {
                                            locations: items {
                                                location: flocation
                                                bin : fbinno
                                                lotsn : flot
                                                onhand : fonhand
                                            }
                                        }
                                        uofm : fmeasure
                                        source : fsource
                                        purchased : fcpurchase
                                        status : fcstscode
                                    }
                                }
                            }";
    
        ErpPart retPart = new ErpPart();
        Item doc = new ();
        Item[] docs = null;
    
        try
        {
            var rootObj = new RootObject();
            //rootObj = GetDocumentAsync(qlQuery).GetAwaiter().GetResult();
            rootObj = await GetDocumentAsync(qlQuery);
            docs = rootObj.products.items;
        }
        catch (Exception ex)
        {
            Debug.Print(ex.Message);
        }
    
        if (docs != null)
        {
            foreach (var row in docs)
            {
                doc.Id1 = row.Id1;
                doc.Id2 = row.Id2;
                doc.Id3 = row.Id3;
                doc.Id4 = row.Id4;
                doc.Name = row.Name;
                doc.Type = "Part";
                doc.Onhand = row.Onhand;
                doc.UofM = row.UofM;
                doc.Source = row.Source;
                doc.Purchased = row.Purchased;
                doc.Status = row.Status;
                //doc.Color = MauiProgram.colorPart;
    
                Location[] inventory = row.Onhand.Locations;
                Debug.Print("");
                decimal onhand = 0;
                if (inventory != null && inventory.Length > 0)
                {
                    // Code goes here
                }
                else
                {
                    //Debug.Print("No OnHand Exists.");
                }
            }
        }
        else
        {
            Debug.Print("No matching Parts found in query.");
        }
    
        retPart = (ErpPart)doc;
        return (ErpPart)doc;
    }
    
    public static explicit operator ErpPart(Item v)
    {
        ErpPart p = new ErpPart();
        p.Part.Id1= v.Id1;
        p.Part.Id2= v.Id2;  
        p.Part.Id3= v.Id3;
        p.Part.Id4= v.Id4;
        p.Part.Name= v.Name;
        p.Part.Type= v.Type;
        p.Part.Onhand=v.Onhand;
        p.Part.UofM=v.UofM;
        p.Part.Source=v.Source;
        p.Part.Purchased=v.Purchased;
        p.Part.Status = v.Status;
        return p;
    }
    
    private static async Task<RootObject> GetDocumentAsync(string iQuery)
    {
    
        var graphQLOptions = new GraphQLHttpClientOptions
        {
            EndPoint = new Uri($"{Url}/graphql", UriKind.Absolute)
        };
    
        var graphQLHttpClient = new GraphQLHttpClient(graphQLOptions, new NewtonsoftJsonSerializer());
    
        var documentRequest = new GraphQLRequest { Query = iQuery };
    
        try
        {
            var graphQLResponse = await graphQLHttpClient.SendQueryAsync<RootObject>(documentRequest);
            Debug.Print("");
            return null;
        }
        catch (Exception ex)
        {
            Debug.Print(ex.Message);
            return null;
        }
    
    }
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   David    2 年前

    它不能将ErpPart类型转换为ErpPart??

    你俯瞰着 任务 部分错误:

    无法隐式转换类型“” 系统线程。任务。任务 <peMove。毛伊岛。模型。ErpPart>'到'peMove。毛伊岛。模型。ErpPart'

    此方法返回的是:

    public static async Task<ErpPart> LoadAsync(DocId key)
    

    但请注意您是如何尝试使用结果的:

    ErpPart erpDocument = ErpPart.LoadAsync(key);
    

    这个 LoadAsync 方法返回该对象,但包装在 Task 因为这是一个 async 方法你需要 await 就像你一样 等候 另外 异步 方法:

    ErpPart erpDocument = await ErpPart.LoadAsync(key);