gpt4 book ai didi

mvvm - 可观察的集合更改后,UI不变

转载 作者:行者123 更新时间:2023-12-03 10:36:14 24 4
gpt4 key购买 nike

嗨,我有observablecollection,我从服务器获取数据,完成后我想刷新page.Data即将到来,但我在屏幕上看不到它,我找不到错误。我实现了INotifypropertychanged,但我认为它无法正常运行。当我放置断点时,我每次看到的都是PropertyChanged事件为null。

我的课

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;
}

Ulke储存库类
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代码
<ListBox x:Name="liste"
ItemsSource="{Binding Mode=TwoWay}" SelectionChanged="liste_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Adi}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

最佳答案

在查看源代码之后更新了解决方案:

一切正常,您只需要注意编译器警告即可。

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>

关于mvvm - 可观察的集合更改后,UI不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27261708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com