gpt4 book ai didi

c# - MvvmCross 命令中的异步任务未返回

转载 作者:行者123 更新时间:2023-11-30 12:21:47 26 4
gpt4 key购买 nike

我正在 Xamarin PCL 项目中使用 MvvMCross 构建一个小项目,我在绑定(bind)到按钮的命令中调用的异步任务有问题。

我有一个假的网络服务,我只是调用 Task.Delay(3000)。当进程到达这一点时,它只是坐着什么都不做。

我最初使用 .wait() 调用进行命令调用,但在某处读到这是一个阻塞调用,不能与“异步/等待”混用

有人可以提供帮助并提示我在命令绑定(bind)上出错的地方吗?

https://bitbucket.org/johncogan/exevaxamarinapp是public git repo,具体命令是

public ICommand SaveProfile

在 ProfileViewModel.cs 文件中。

具体代码为:

public ICommand SaveProfile
{
get
{
return new MvxCommand(() =>
{
if (_profile.IsValidData())
{
// Wait for task to compelte, do UI updates here
// TODO Throbber / Spinner
EnumWebServiceResult taskResult;
Mvx.Resolve<IProfileWebService>().SendProfileToServer(_profile).Wait();

if(_profileWebService.getLastResponseResult() == true){
taskResult = EnumWebServiceResult.SUCCESS;
}else{
taskResult = EnumWebServiceResult.FAILED_UNKNOWN;
}
//_profileWebService.SendProfileToServer(_profile).Wait();
// Close(this);
}
});
}
}

Web服务类()是:

using System;
using System.Threading.Tasks;
using ExevaXamarinApp.Models;

namespace ExevaXamarinApp.Services
{
public class FakeProfileWebService : IProfileWebService
{
public int _delayPeriod { get; private set; }
public bool? lastResult;

/// <summary>
/// Initializes a new instance of the <see cref="T:ExevaXamarinApp.Enumerations.FakeProfileWebService"/> class.
/// </summary>
/// 3 second delay to simulate a remote request
public FakeProfileWebService()
{
_delayPeriod = 3000;
lastResult = null;
}

private Task Sleep()
{
return Task.Delay(3000);
}

public bool? getLastResponseResult(){
return lastResult;
}

/// <summary>
/// Sends the profile to server asynchronously
/// </summary>
/// <returns>EnumWebServiceResultFlag value</returns>
/// <param name="profileObject">Profile model object</param>
public async Task SendProfileToServer(Profile profileObject)
{
// Validate arguments before attempting to use web serivce
if (profileObject.IsValidData())
{
// TODO: Return ENUM FLAG that represents the state of the result
await Sleep();
lastResult = true;
}else{
lastResult = false;
}
}
}
}

最佳答案

请试试这个:

    public ICommand SaveProfile
{
get
{
return new MvxCommand(async () => // async added
{
if (_profile.IsValidData())
{
// Wait for task to compelte, do UI updates here
// TODO Throbber / Spinner
EnumWebServiceResult taskResult;
await Mvx.Resolve<IProfileWebService>().SendProfileToServer(_profile).ConfigureAwait(false); // await, confi.. added

if(_profileWebService.getLastResponseResult() == true){
taskResult = EnumWebServiceResult.SUCCESS;
}else{
taskResult = EnumWebServiceResult.FAILED_UNKNOWN;
}
//_profileWebService.SendProfileToServer(_profile).Wait();
// Close(this);
}
});
}
}

    private async Task Sleep()                                 // async added
{
return await Task.Delay(3000).ConfigureAwait(false); // await, confi... added
}

public async Task SendProfileToServer(Profile profileObject)
{
// Validate arguments before attempting to use web serivce
if (profileObject.IsValidData())
{
// TODO: Return ENUM FLAG that represents the state of the result
await Sleep().ConfigureAwait(false); // await, confi... added
lastResult = true;
}else{
lastResult = false;
}
}

问题是,来自 UI 的上下文和异步会导致死锁。

关于c# - MvvmCross 命令中的异步任务未返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43958380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com