嗨我有observablecollection,我从服务器获取数据,当它完成后,我想在我的页面上刷新列表。数据即将到来,但我无法在屏幕上看到它,我无法找到我的错误。我实现了INotifypropertychanged,我觉得它运行不好。当我放置断点时,我每次都看到PropertyChanged事件为空。
我的班级
public class Ulke:INotifyPropertyChanged
{
private int _ulkeID;
public int UlkeID
{
get { return _ulkeID; }
set { _ulkeID = value; NotifyPropertyChanged(); }
}
private string _adi;
public string Adi
{
get { return _adi; }
set { _adi = value; NotifyPropertyChanged(); }
}
public string DiyanetID { get; set; }
public bool EyaletVarmi { get; set; }
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
UlkeRepository Class
public class UlkeRepository : INotifyPropertyChanged
{
public UlkeRepository()
{
_ulkeler = new ObservableCollection<Ulke>();
ulkeler.Add(new Ulke { UlkeID = 3, EyaletVarmi = true, DiyanetID = "3", Adi = "Moliba" });
ulkeler.Add(new Ulke { UlkeID = 5, EyaletVarmi = true, DiyanetID = "3", Adi = "As" });
}
private ObservableCollection<Ulke> _ulkeler;
public ObservableCollection<Ulke> ulkeler
{
get { return _ulkeler; }
set { _ulkeler = value; NotifyPropertyChanged(); }
}
public async Task UlkeGetir()
{
JsonDownloader js = new JsonDownloader();
var result = await js.GetDataFromUrl<Ulke>("http://www.example.com/A/UlkeGetir");
ulkeler = result;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
MyPageLoaded代码
this.DataContext = ulke.ulkeler;
liste.DataContext = ulke.ulkeler;
ulke.UlkeGetir();
Xaml Code
<ListBox x:Name="liste"
ItemsSource="{Binding Mode=TwoWay}" SelectionChanged="liste_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Adi}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:1)
在查看来源后更新了解决方案:
一切正常,您只需要注意编译器警告。
<强> HubPage.xaml.cs 强>
功能UlkeYukle更新(你忘了等待)
UlkeRepository ulke;
private async void UlkeYukle()
{
ulke = new UlkeRepository();
await ulke.UlkeGetir(); // you forgot this
}
// this is a function to test your OnPropertyChange
private void Button_Click(object sender, RoutedEventArgs e)
{
ulke.ulkeler.Insert(0, new Ulke { UlkeID = 5, EyaletVarmi = true, DiyanetID = "3", Adi = "A_TEST" }); // inssert at top
ulke.ulkeler[0].Adi = "Changing Title Again";
}
出于调试目的,我在模板中添加了一个Button(因此我们可以测试它是否在事后更新)
<强> HubPage.xaml 强>
<HubSection x:Uid="HubSection1" x:Name="hub1" Header="SECTION 1" DataContext="{Binding Mode=TwoWay}" HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80*"/>
<RowDefinition Height="20*"/>
</Grid.RowDefinitions>
<ListView
ItemsSource="{Binding Mode=TwoWay}"
IsItemClickEnabled="True"
ItemClick="GroupSection_ItemClick"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,27.5">
<TextBlock Text="{Binding Adi}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Click="Button_Click" Grid.Row="1"></Button>
</Grid>
</DataTemplate>
</HubSection>