我有一个手动创建列表的工作解决方案。 但是由于我想从文件中读取,因此我不得不更改为异步,并且由于数据量将来将更改为可观察集合,XAML 不再显示 9 行。
调试我看到{x:绑定帐户}仍然包含 9 行和两个值。但帐户名和总帐户名数据未列出。只有页眉和页脚。 我现在花了几个小时比较这两种解决方案,但不知道为什么这个解决方案不显示数据。
概述.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>
账户.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();
}
}
}
1) 将Accounts
字段更改为属性
2)如果你使用绑定,你应该实现INotifyPropertyChanged
并在每次更改属性时调用OnPropertyChanged(Accounts
)。这将更新视图。
3)默认情况下x:Bind
使用mode=OneTime
,应将其更改为Mode=OneWay
。
如果您想要最快和简单的解决方案,请删除绑定并执行ListView.ItemSource = MyClass.GetAccounts()
.它不适合 MVVM 模式,但我看到您正在使用隐藏的代码。
首先,绑定仅适用于属性而不是字段(您的 ObservableCollection 帐户只是字段)。
另一个问题可能是加载 XAML 时Accounts
属性将被null
,因此您可能需要在this.InitializeComponents();
之前添加Accounts = new ObservableCollection<Accounts>();
,并且也不要在AccountOverview.GetAccounts();
方法返回 ObservableCollection 的全新实例,而只是填充已经存在的对象。
我做了所有解释,但它仍然不起作用。
我Name="AccountList"
添加到 Overview.xaml 中的列表视图中。AccountList.ItemSource = Accounts;
到概述.xaml.cs。现在它起作用了。