- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 .Net Mvvm/MVC 学习过程中,我偶然发现了另一个问题。
我正在尝试填充 ObservableObject 属性,以便可以将其绑定(bind)到我的窗口或用户控件。该属性应填写在 private async void
中执行 GET 请求。
因为你不能await
属性内的方法/函数,我试图填充方法内的属性。它总是给出 NullReferenceException
.
在设置换行符并按 F11(跳过)很多次后,我发现除了所需的属性 Events
之外,所有内容都已正确填充。 .
流程是这样的:
ObservableObject
在这些与 API 通信的方法中设置属性“国家、省、市”。此时一切仍然正常。
City
属性被设置,它调用一个方法
GetOrgsByCity()
填充另一个 ObservableObject 属性
Orgs
.在
Orgs
的 setter 中,
GetFacebookData(FB_IDS)
叫做。此方法需要一个类型为
List<string>
的参数并且是一个由 LINQ 返回过滤列表的属性。一切仍然正常。
GetFacebookData(FB_IDS)
方法我正在尝试填写
ObservableObject
名为
Events
的属性,该方法向 Facebook 执行 GET 请求。
IsSuccessStatusCode
等于
true
除了
ObservableObject
之外,所有内容都已正确填写房产
Events
.
List<FB> data
)“转换”为
foreach
获取
Events
填充。当我运行它时,它会中断并抛出
NullReferenceException
.
Events
为 null 或其他东西,因为其他变量已正确填充。
Events
属性(property)拍个NullReferenceException
当试图填补它?
<Grid Grid.Row="0" DataContext="{Binding Source={StaticResource PartOneVM}}">
<Grid.RowDefinitions>
<RowDefinition Height="30px" />
<RowDefinition Height="30px" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="txbCountry" Style="{StaticResource InfoLabelCountry}" />
<ComboBox x:Name="cboCountry" Style="{StaticResource CountryBox}" ItemsSource="{Binding Countries}" DisplayMemberPath="En_name" SelectedItem="{Binding SelectedCountry}" />
<TextBlock x:Name="txbGewest" Style="{StaticResource InfoLabelGewest}" />
<ComboBox x:Name="cboGewest" Style="{StaticResource GewestBox}" ItemsSource="{Binding Provinces}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedProvince}" />
<TextBlock x:Name="txbCity" Style="{StaticResource InfoLabelCity}" />
<ComboBox x:Name="cboCity" Style="{StaticResource CityBox}" ItemsSource="{Binding Cities}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedCity}" />
</Grid>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Height="300px" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Events}">
</ListBox>
</Grid>
public class PartOneVM : ObservableObject, IPage
{
#region props
public string Name { get { return "Page One"; } }
private ObservableCollection<Country> _countries;
public ObservableCollection<Country> Countries
{
get { return _countries; }
set { _countries = value; OnPropertyChanged("Countries"); }
}
private ObservableCollection<Province> _provinces;
public ObservableCollection<Province> Provinces
{
get { return _provinces; }
set { _provinces = value; OnPropertyChanged("Provinces"); }
}
private ObservableCollection<City> _cities;
public ObservableCollection<City> Cities
{
get { return _cities; }
set { _cities = value; OnPropertyChanged("Cities"); }
}
private Country _selectedCountry;
public Country SelectedCountry
{
get { return _selectedCountry; }
set { _selectedCountry = value; GetProvincesByCountry(); OnPropertyChanged("SelectedCountry"); }
}
private Province _selectedProvince;
public Province SelectedProvince
{
get { return _selectedProvince; }
set { _selectedProvince = value; GetCitiesByProvince(); OnPropertyChanged("SelectedProvince"); }
}
private City _selectedCity;
public City SelectedCity
{
get { return _selectedCity; }
set { _selectedCity = value; GetOrgsByCity(); OnPropertyChanged("SelectedCity"); }
}
private ObservableCollection<Org> _orgs;
public ObservableCollection<Org> Orgs
{
get { return _orgs; }
set { _orgs = value; GetFacebookData(FB_IDS); OnPropertyChanged("Orgs"); }
}
public List<string> FB_IDS
{
get { if (Orgs == null) return null; return (from s in Orgs where s.CityID.Equals(SelectedCity.ID) select s.FB_ID).Distinct().ToList<string>(); }
}
private ObservableCollection<FB> _events;
public ObservableCollection<FB> Events
{
get { return _events; }
set { _events = value; OnPropertyChanged("Events"); }
}
#endregion
#region ctor
public PartOneVM()
{
GetCountries();
}
#endregion
#region methodes
private async void GetCountries()
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("http://localhost:58564/api/country");
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
Countries = JsonConvert.DeserializeObject<ObservableCollection<Country>>(json);
}
}
}
private async void GetProvincesByCountry()
{
if (SelectedCountry != null)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("http://localhost:58564/api/province/GetProvincesByCountry/" + SelectedCountry.ID);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
Provinces = JsonConvert.DeserializeObject<ObservableCollection<Province>>(json);
}
}
}
}
private async void GetCitiesByProvince()
{
if (SelectedCountry != null && SelectedProvince != null)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("http://localhost:58564/api/city/GetCitiesByProvince/" + SelectedCountry.ID + "/" + SelectedProvince.ID);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
Cities = JsonConvert.DeserializeObject<ObservableCollection<City>>(json);
}
}
}
}
private async void GetOrgsByCity()
{
if (SelectedCountry != null && SelectedProvince != null && SelectedCity != null)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("http://localhost:58564/api/org/GetOrgsByCity/" + SelectedCountry.ID + "/" + SelectedProvince.ID + "/" + SelectedCity.ID);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
Orgs = JsonConvert.DeserializeObject<ObservableCollection<Org>>(json);
}
}
}
}
private async void GetFacebookData(List<string> fb_ids)
{
var l = fb_ids
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / 50)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
for (var i = 0; i < l.Count; i++)
{
string ids = string.Join(",", l[i]);
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(@"https://graph.facebook.com/v2.8/");
HttpResponseMessage response = await client.GetAsync("?ids=" + ids + "&fields=id,name,events.limit(60)&access_token=<acces_token>");
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<IDictionary<string, FB>>(json);
List<FB> data = result.Select(item => item.Value).ToList();
if (data != null)
{
foreach (FB item in data)
{
if (item != null)
{
Events.Add(item); // THROWS NullReferenceException !!!
}
}
}
}
else
{
// error handling
}
}
}
}
#endregion
}
最佳答案
首先-永远不要使用'async void'-这有时可能只适用于事件处理程序,但在这里不适用。使用“异步任务”,然后您将能够在 GetFacebook 方法(或其他异步方法之一)真正完成后使用 ContinueWith 构造调用 OnPropertyChanged。
第二 - 事件抛出异常,因为它没有被初始化。你应该改变:
private ObservableCollection<FB> _events = new ObservableCollection<FB>();
关于c# Mvvm 用来自 GET 请求的数据填充 ObservableObject 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41655596/
我知道使用 GET 和 SET 函数的公共(public)变量的缺点/私有(private)变量的优点,但目前我正在使用 Ogre3D 开发自己的第一个“真实”游戏(C++)..同时,我有时需要 6-
我正在开发一个 GSM/GPRS 应用程序,它将每 10 秒报告一些值。我必须使用的 SIM 卡每月只有 15MB 可用数据。我使用的是 SIM900 GSM 芯片供您引用。 我到达服务器的方式是通过
这三者有什么区别:gets - 它获取带有 '\n' 的行gets.chomp - 它得到一行,但删除 '\n' 这样对吗? gets.chomp! 怎么样? 最佳答案 gets - 它得到一个末尾带
问题和我现在遇到的问题 脚本 顺便说一句,评论是挪威语的,如果它们看起来很奇怪哈哈 Connect-AzureAD #variabel $Users = Get-AzureADUser -All:$t
我现在面临的问题是获取一个 URL,如下所示: www.example.com/example.php?url=www.google.com 现在的问题是,如果我的网址中有一个 get,如下所示: w
我有一个 queryString 传递给 servlet 的 doGet() 方法,如下所示: count=9&preId0=-99&objId0=-99&preId1=-99&objId1=-99&
这是我在 Django 模板中的代码: {% for tag in tags %} {{ tag }} {% endfor %} 在view.py中: def tag_find(
我正在尝试在express.js中为我的网络应用程序创建一个路由系统,我需要知道是否需要使用app.get/post/put/delete.apply以编程方式设置多个功能对于一条路线。 也是如此 a
我正在通过示例查看 A.Mele Django,第 1 章 def post_list(request, category=None): object_list = Post.publishe
如果我想找到与IIS站点或应用程序关联的目录,我该怎么做? 我似乎无法从Get-Website和Get-WebApplication的对象的任何属性中找到任何允许我这样做的东西。 最佳答案 只需查看一
不知道发生了什么。当我执行以下代码时......它运行良好......但它产生了错误。如果我将以下内容粘贴到我的浏览器地址栏中并点击它,我会得到一个 URL。如果我通过 KRL http:get 输入
Curl 提供了一系列不同的带有 X 前缀的 http 方法调用,但也提供了不带 X 的相同方法。我两种都试过了,但我似乎无法弄清楚其中的区别。有人可以快速向我解释这两种操作有何不同吗? 最佳答案 默
request.GET.get 是什么意思?我在 Django 中看到类似的东西 page = request.GET.get('page', 1) 我认为它与类似的东西有关 « 它们是如
我正在从我的 Angular2 站点查询一些 Elasticsearch 服务器。为了帮助提高安全性,我们希望锁定对 GET 请求的访问权限。 Elasticsearch 支持带主体的 GET,但我在
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
调用 HTable.get(List) 返回的 Result 数组的顺序是什么? ? 我的意思是,假设与输入列表的顺序相同是否正确? 最佳答案 结果数组中的顺序将与输入列表的顺序相同。与批处理方法一样
所以我有一个看起来像这样的 JSON 数组: var myData = { foo : { biz : 'baz', fig : 'tree' } }
我正在学习 Ajax、javascript 和 html,并且有一个应用程序可以触发“get”请求,然后再触发另一个“get”请求。这些请求是用户按下按钮的结果。在我的 servlet 中,我使用 T
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
运行以下 cmdlet 适用于组成员(Amer 域中的组)中的所有用户,无论列出的用户位于哪个域: Get-ADGroupMember -Server amer 但是,当尝试通过管道传输到 Get-
我是一名优秀的程序员,十分优秀!