我目前正在开发UWP应用程序,因为我使用的是MVVM模式。并且还使用web服务来获取数据。当我试图导航到一个页面到另一个页面或点击任何控件时应用程序被击中,我不知道确切的原因。
例如,在我的应用程序中我有Userprofile页面,因为我绑定了数据以显示我调用webservice获取数据的用户详细信息,我有服务中的webservice方法viewmodel中的类和服务调用方法,我在.cs文件中调用viewmodel方法。在这种情况下,我的应用程序也被攻击了,
我的代码:
public async Task<ObservableCollection<CoverageModel>> GetCoverageDetailsForUserAsync()
{
HttpClient httpClient = new HttpClient();
var CoverageDetailsList = new ObservableCollection<CoverageModel>();
try
{
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, App.Base_Url + "api/User/GetCoverageDetailsOfUser");
requestMessage.Headers.Add("authorization", App.TokenType + " " + App.AccessToken);
HttpResponseMessage response1 = await httpClient.SendAsync(requestMessage);
var responsedata = await response1.Content.ReadAsStringAsync();
JObject data = JObject.Parse(responsedata);
var page = data.SelectToken("<Result>k__BackingField");
var result = page.ToObject<CoverageModel>();
App.CoverageId = result.CoverageID;
CoverageDetailsList.Add(result);
}
catch (Exception e)
{
}
return CoverageDetailsList;
}
和我的viewmodel代码:
public async void GetCoverageDetailsList()
{
IsProcessing = true;
var userdetails = await UserManagementService.GetCoverageDetailsForUserAsync();
var collection = new ObservableCollection<AFUI>();
if (userdetails[0].AF!= null)
{
foreach (var item in userdetails[0].AF)
{
AFUI Af = new AFUI ();
Af.Number = userdetails[0].AF.IndexOf(item) + 1;
//Af = item.AF[0];
Af.Fee = item.Fee;
//"$" + "" +
Af.JobTitle = item.JobTitle;
Af.JobType = item.JobType;
Af.AFID= item.AFID;
Af.EditImage = "\xE28F";
collection.Add(Af);
}
CoverageCollection = collection;
}
IsProcessing = false;
}
private RelayCommand _getCoverageListCommand;
public RelayCommand GetCoverageListCommand
{
get
{
return _getCoverageListCommand
?? (_getCoverageListCommand = new RelayCommand(GetCoverageDetailsList));
}
}
我在.cs文件中调用该命令OnNavigatedTo方法
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (VM.GetCoverageListCommand.CanExecute(null))
VM.GetCoverageListCommand.Execute(null);
}
在我的xaml中,我使用了Sync融合控制SFDataGrid。只需我绑定ItemSource = CoverageCollection;到该datagrid控件以显示coverage列表
这就是我用来调用web服务和绑定的。