我有一个手动创建列表的工作解决方案。 但是因为我想从一个文件读取,因此我必须更改为异步,并且因为未来数据量将改变为ObservableCollection,XAML不再显示9行。
调试我看到 {x:Bind Accounts} 仍然包含9行和两个值。但未列出 AccountName 和 SumAccountName 数据。只有页眉和页脚。 我现在花了几个小时来比较两种解决方案,但不知道为什么这个解决方案没有显示数据。
Overview.xaml:
x:Class="Finance_Manager.Overview"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Finance_Manager"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data="using:Finance_Manager.Models"
mc:Ignorable="d">
<StackPanel>
<Grid Margin="20,20,20,20" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Loaded="Grid_Loaded">
<ListView ItemsSource="{x:Bind Accounts}" IsItemClickEnabled="True" ItemClick="ListView_ItemClick">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.Header>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<TextBlock FontWeight="Bold" Text="Konto" />
<TextBlock Grid.Column="1" FontWeight="Bold" Text="Total" TextAlignment="Right"/>
</Grid>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Account">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{x:Bind AccountName}" />
<TextBlock Grid.Column="1" Text="{x:Bind SumAccountName}" TextAlignment="Right"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Footer>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<TextBlock FontWeight="Bold" Text="Gesamtvermögen" />
<!-- <TextBlock Grid.Column="1" FontWeight="Bold" x:Name="SumTextBlock" TextAlignment="Right"/> -->
<TextBlock Grid.Column="1" FontWeight="Bold" Text="CHF 326'979.74" TextAlignment="Right"/>
</Grid>
</ListView.Footer>
</ListView>
</Grid>
<Grid>
<TextBlock Margin="20,80,0,0" x:Name="TextBlockClicked" TextWrapping="Wrap" VerticalAlignment="Bottom" />
</Grid>
</StackPanel>
Accounts.cs:
namespace Finance_Manager.Models
{
public class Account
{
public string AccountName { get; set; }
public double SumAccountName { get; set; }
}
public class AccountOverview
{
public static async Task<ObservableCollection<Account>> GetAccounts()
{
var accounts = new ObservableCollection<Account>();
//
// Load file
var folder = ApplicationData.Current.LocalFolder;
var GetOverviewFile = await folder.GetFileAsync("overview.json");
string jsonString = await FileIO.ReadTextAsync(GetOverviewFile);
//
JsonArray root = JsonValue.Parse(jsonString).GetArray();
for (uint i = 0; i < root.Count; i++)
{
string account1 = root.GetObjectAt(i).GetNamedString("account");
double sumaccount1 = root.GetObjectAt(i).GetNamedNumber("sumaccount");
accounts.Add(new Account { AccountName = account1, SumAccountName = sumaccount1 });
};
//accounts.Add(new Account { AccountName = "Bank1", SumAccountName = 100.00 });
//accounts.Add(new Account { AccountName = "Bank2", SumAccountName = 100.00 });
//accounts.Add(new Account { AccountName = "Bank3", SumAccountName = 100.00 });
//accounts.Add(new Account { AccountName = "Bank4", SumAccountName = 100.00 });
//accounts.Add(new Account { AccountName = "Bank5", SumAccountName = 100.00 });
//accounts.Add(new Account { AccountName = "Bank6", SumAccountName = 100.00 });
//accounts.Add(new Account { AccountName = "Bank7", SumAccountName = 100.00 });
//accounts.Add(new Account { AccountName = "Bank8", SumAccountName = 100.00 });
//accounts.Add(new Account { AccountName = "Bank9", SumAccountName = 100.00 });
return accounts;
}
}
}
Overview.xaml.cs:
namespace Finance_Manager
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class Overview : Page
{
public ObservableCollection<Account> Accounts;
public Overview()
{
this.InitializeComponent();
}
public async void Grid_Loaded(object sender, RoutedEventArgs e)
{
Accounts = await AccountOverview.GetAccounts();
}
}
}
答案 0 :(得分:1)
首先,绑定仅适用于属性而非字段(您的ObservableCollection帐户只是字段)。
另一个问题可能是加载XAML时Accounts
属性为null
,因此您可能需要在Accounts = new ObservableCollection<Accounts>();
之前添加this.InitializeComponents();
,而不是返回来自AccountOverview.GetAccounts();
方法的全新ObservableCollection实例,但只填充已存在的对象。
答案 1 :(得分:1)
1)将您的Accounts
字段更改为属性
2)如果您正在使用绑定,则应在每次更改INotifyPropertyChanged
属性时实现Accounts
并调用OnPropertyChanged()。这将更新View。
3)默认x:Bind
使用mode=OneTime
,您应将其更改为Mode=OneWay
。
如果您想要最快速,最简单的解决方案,请删除绑定,然后执行ListView.ItemSource = MyClass.GetAccounts()
。它对MVVM模式不利,但我发现你正在使用代码。
答案 2 :(得分:0)
我做了所有解释,但它仍然无法正常工作。
我将Name="AccountList"
添加到Overview.xaml中的ListView。和AccountList.ItemSource = Accounts;
到Overview.xaml.cs。现在它有效。