[英]ListView on Xamarin Forms only shows data when you resave the xml page… Any idea?
Well, I'm working on a Project on Xamarin forms where I drag data from SQL through an API, which is connected to my application.好吧,我正在做一个关于 Xamarin forms 的项目,我从 SQL 中拖动数据通过一个 ZDB974238714CA8DE4FZA 连接到我的应用程序。 The API works fine, it shows the information from SQL, and then on the
ViewModel.cs
and I can see the data con my ListView ItemSource={binding path}
, but when the process is completed it doesn't show any data or info. API 工作正常,它显示来自 SQL 的信息,然后在
ViewModel.cs
上,我可以看到我的ListView ItemSource={binding path}
的数据,但是当该过程完成时,它不显示任何数据或信息. It only displays once I hit Ctrl+s on my xaml page.它仅在我在 xaml 页面上按 Ctrl+s时显示。
ListView列表显示
<ListView ItemsSource="{Binding CompraCoba}" SeparatorVisibility="Default" SelectionMode="None" MinimumHeightRequest="100" HeightRequest="325" HasUnevenRows="False">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid RowSpacing="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<Label Margin="10,10,10,0" HorizontalOptions="Start" x:Name="SucDlr" Text="{Binding monedaC}" FontSize="10" VerticalOptions="Start" TextColor="White" Grid.Column="0" HorizontalTextAlignment="Start" />
<Label Margin="10,10,10,0" HorizontalOptions="Center" x:Name="MontoDlr" Text="{Binding valorC}" TextColor="White" FontSize="10" VerticalOptions="Start" Grid.Column="1" />
<Label Margin="10,10,10,0" HorizontalOptions="End" x:Name="CambioDlr" Text="{Binding diaC}" TextColor="White" FontSize="10" VerticalOptions="Start" Grid.Column="2" />
<!--<Label Grid.ColumnSpan="4" BackgroundColor="#bababa" HeightRequest="1" HorizontalOptions="FillAndExpand" VerticalOptions="End" />-->
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code-behind where vm = to my ViewModel. vm = 我的 ViewModel 的代码隐藏。 The GetCoba is an http request, it works fine
GetCoba 是一个 http 请求,它工作正常
public ComprasTaxa()
{
InitializeComponent();
BindingContext = vm;
vm.GetCoba(1);
}
Any suggestion would be an excellent help.任何建议将是一个很好的帮助。 Thanks in regards
致谢
ViewModel视图模型
public async void GetCoba(int dias)
{
using (var client = new HttpClient())
{
var accessToken = Xamarin.Forms.Application.Current.Properties["token"];
//Get Request
var url = "https://masterxserver.azurewebsites.net/api/CompraTaxa/GetCoba?dia=" + dias + "";
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
var result = await client.GetStringAsync(url);
var CompraTaxaList = JsonConvert.DeserializeObject<List<CompraTaxaM>>(result);
CompraCoba = new ObservableCollection<CompraTaxaM>(CompraTaxaList);
}
}
ObservableCollection<CompraTaxaM> _compraCoba;
public ObservableCollection<CompraTaxaM> CompraCoba
{
get
{
return _compraCoba;
}
set
{
_compraCoba = value;
OnPropertyChanged();
}
}```
From your code and the description of the issue, it seems that the CompraCoba
set is not notifying the View about the changed property.从您的代码和问题的描述来看,似乎
CompraCoba
集没有通知视图有关更改的属性。 You mentioned that the VM implemented INotifyPropertyChanged
, could you please show that code?您提到虚拟机实现了
INotifyPropertyChanged
,您能否显示该代码?
Also, it's possible that after you set value for the CompraCoba
property, something can override, I would add a Debug.WriteLine
and printing the value to be assigned and (important) thread it's assigned from ( Thread.CurrentThread.ManagedThreadId
) and make sure you get this print only once and only from the main thread (#1).此外,在您为
CompraCoba
属性设置值之后,有可能会覆盖某些东西,我会添加一个Debug.WriteLine
并打印要分配的值和(重要的)线程,它是从( Thread.CurrentThread.ManagedThreadId
)分配的,并确保您只能从主线程(#1)获得一次打印。 The fact that the data is loaded on Ctr+S means that property content is most likely correct but the thread it's been assigned from (or PropertyChanged event) is not.数据加载到 Ctr+S 上的事实意味着属性内容很可能是正确的,但分配给它的线程(或 PropertyChanged 事件)却不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.