C# 活动指示器在所有代码执行后才显示
基本上,我有一个活动指示器,它在方法中的所有其他代码执行之前不会显示。我已经看过了,代码端(登录页面)和xaml绑定(登录xaml)上的IsBusy属性都设置为true,所以我知道至少传递了正确的值 我非常确定我已经正确地设置了所有内容,因为async/await没有锁定任何内容 我有一个所有视图模型都使用的BaseViewModel类。下面是IsBusy的代码:C# 活动指示器在所有代码执行后才显示,c#,xamarin.forms,visual-studio-2019,activity-indicator,C#,Xamarin.forms,Visual Studio 2019,Activity Indicator,基本上,我有一个活动指示器,它在方法中的所有其他代码执行之前不会显示。我已经看过了,代码端(登录页面)和xaml绑定(登录xaml)上的IsBusy属性都设置为true,所以我知道至少传递了正确的值 我非常确定我已经正确地设置了所有内容,因为async/await没有锁定任何内容 我有一个所有视图模型都使用的BaseViewModel类。下面是IsBusy的代码: bool isBusy = false; public bool IsBusy { get { re
bool isBusy = false;
public bool IsBusy
{
get { return isBusy; }
set { SetProperty(ref isBusy, value); }
}
protected bool SetProperty<T>(ref T backingStore, T value,
[CallerMemberName]string propertyName = "",
Action onChanged = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
我肯定这个问题很小,但我就是看不到
编辑:添加经过身份验证的方法
已验证
public static async Task<bool> IsAuthenticated(string email, string password)
{
var isAuthenticated = false;
try
{
var payload = $"{{\n\t\"Email\":\"{email}\",\n\t\"Password\":\"{password}\"\n}}";
var response = await LoginApi.Authenticate(payload);
isAuthenticated = bool.Parse(response["isAuthenticated"].ToString());
}
catch (Exception e)
{
// log stuff here
}
return isAuthenticated;
}
已验证公共静态异步任务(字符串电子邮件、字符串密码)
{
var isAuthenticated=假;
尝试
{
var payload=$“{\n\t\“Email\”:\“{Email}\”,\n\t\“Password\”:\“{Password}\”\n}}”;
var响应=等待LoginApi.Authenticate(有效负载);
isAuthenticated=bool.Parse(响应[“isAuthenticated”].ToString());
}
捕获(例外e)
{
//这里有日志
}
返回未经验证;
}
验证API调用:
public static async Task<JObject> Authenticate(string payloadBody)
{
IRestResponse response = null;
try
{
var client = new RestClient($"{url}/authenticate");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("undefined", payloadBody, ParameterType.RequestBody);
response = client.Execute(request);
} catch (Exception e)
{
// log some bad stuff here
}
return JObject.Parse(response.Content);
}
公共静态异步任务身份验证(字符串payloadBody)
{
iRestreponse响应=null;
尝试
{
var client=newrestclient($“{url}/authenticate”);
var请求=新的重新请求(Method.POST);
AddHeader(“内容类型”、“应用程序/json”);
AddParameter(“未定义”,payloadBody,ParameterType.RequestBody);
响应=客户端。执行(请求);
}捕获(例外e)
{
//在这里记录一些不好的东西
}
返回JObject.Parse(response.Content);
}
确保viewmodel实现INotifyPropertyChanged,然后:
public bool IsBusy
{
set { isBusy = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsBusy))); }
get { return isBusy; }
}
通过更新的
Authenticate
方法代码,您可以更改
client.Execute;
到
在这种情况下,您将显示您的ActivityIndicator,因为您的expect是
isBusy
字段受保护还是私有?未指定,因此它是内部的,我想我认为它是私有的,但是,我不确定这是否真的是原因-。我只是将其公开以进行双重检查,是的,将isBusy设置为后,它仍然没有出现true@user1818298如果(!isAuthenticated)这一行,您可以进行调试,查看它是否在IsBusy=true之后立即进入该行
调用OnLogin
方法时,如果是,请尝试使用client.ExecuteAsync
而不是client.Execute
public bool IsBusy
{
set { isBusy = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsBusy))); }
get { return isBusy; }
}
client.Execute;
client.ExecuteAsync