【发布时间】:2017-06-08 07:54:20
【问题描述】:
我创建了一个连接到 API 的类,以使用 httpclient 检索所需的数据。该文件在视图的代码隐藏文件中被调用并且运行良好。然后我决定实施 MVVM 方法。结果,我将初始化其余服务类的代码移到了视图模型中。 之后,我停止获取数据。为了进行调查,我在调试会话中声明了带有断点的调试会话,该断点位于我初始化其余服务类的行。比我执行那条线。通过这样做,我发现抛出了一个巨大的 android 单声道异常,并且如果停止了调试会话。应用退出调试会话。 自从我声明在 Xamarin Forms 中开发我的应用程序以来,这是第一次发生。我不知道它为什么会这样坏。对你的帮助表示感谢。
这是正常工作的代码。 在文件后面的视图代码中
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SubtaskPage : ContentPage
{
protected override void OnAppearing()
{
base.OnAppearing();
PopulateSubtaskData();
}
private async void PopulateSubtaskData()
{
lstSubtasks.IsRefreshing = true;
try
{
RestService rs = new RestService();
SResponse = await rs.GetSubtasksAsync(Convert.ToInt32(Application.Current.Properties["UserId"]));
if (SResponse.Status == 1)
{
lstSubtasks.ItemsSource = SResponse.Subtasks;
}
else
{
await DisplayAlert("Error", SResponse.Message, "Ok");
}
}
catch (Exception E)
{
Debug.WriteLine(@"GetSubtasksAsync -> ERROR {0}", E.Message);
}
lstSubtasks.IsRefreshing = false;
}
}
其余服务类如下 此类位于名为“服务”的单独文件夹中。出于安全原因,已更改 ip 和 url。
class RestService
{
HttpClient client;
public List<Ticket> Tickets { get; private set; }
string Server1 = "server ip";
string Server2 = "server ip";
public RestService()
{
client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;
}
public async Task<SubtasksResponse> GetSubtasksAsync(int UserId)
{
SubtasksResponse SubtaskResponse = new SubtasksResponse();
string ApiUrl = "URL";
string Url = "";
HttpResponseMessage response;
if (CrossConnectivity.Current.IsConnected)
{
Url = await GetActiveServerAsync();
if (Url != "")
{
var uri = string.Format(Url + ApiUrl, UserId);
try
{
response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
SubtaskResponse.Subtasks = JsonConvert.DeserializeObject<List<Ticket>>(content);
SubtaskResponse.Status = 1;
}
else
{
SubtaskResponse.Subtasks = null;
SubtaskResponse.Status = 0;
SubtaskResponse.Message = "Attempt to fetch data from server was unsuccessful. Please try again";
}
}
catch (Exception E)
{
SubtaskResponse.Subtasks = null;
SubtaskResponse.Status = 0;
SubtaskResponse.Message = "Error occured while fetching data from the server. Please try again";
}
}
else
{
SubtaskResponse.Subtasks = null;
SubtaskResponse.Status = 0;
SubtaskResponse.Message = "Remote Server Not Responding! Please try again later";
}
}
else
{
SubtaskResponse.Subtasks = null;
SubtaskResponse.Status = 0;
SubtaskResponse.Message = "No Network Connection Found! Please connect to a network and try again";
}
return SubtaskResponse;
}
}
}
在我将视图模型添加到组合中之前,这一切正常。 这就是我在视图模型中调用函数的方式。
async Task<SubtasksResponse> PopulateSubtaskList()
{
RestService rs = new RestService();
IsBusy = true;
_subtaskList = await rs.GetSubtasksAsync(Convert.ToInt32(Application.Current.Properties["UserId"]));
IsBusy = false;
return _subtaskList;
}
"RestService rs = new RestService();"这是代码中断的那一行。
希望您能清楚地了解情况。如果需要其他信息,请告诉我。 谢谢
【问题讨论】:
-
如果您可以共享代码,无论您到目前为止尝试过什么,都很棒。以便该社区可以更好地帮助您。
-
需要将产生异常的代码和异常本身添加到您的问题中
-
为 API 调用创建单独的文件夹,如“服务”并在此处调用 api。
-
ViewModel 是否在单独的 pcl 项目中?
标签: xamarin mvvm xamarin.forms httpclient viewmodel