Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UWP#未显示ListView.ItemTemplate,因为我从List更改为ObservableCollection和async_C#_Xaml_Asynchronous_Uwp_Observablecollection - Fatal编程技术网

C# UWP#未显示ListView.ItemTemplate,因为我从List更改为ObservableCollection和async

C# UWP#未显示ListView.ItemTemplate,因为我从List更改为ObservableCollection和async,c#,xaml,asynchronous,uwp,observablecollection,C#,Xaml,Asynchronous,Uwp,Observablecollection,我有一个手动创建列表的工作解决方案。 但由于我想从文件中读取数据,因此我不得不改为异步,并且因为数据量将来将更改为ObservableCollection,所以XAML不再显示9行 调试时,我看到{x:Bind Accounts}仍然包含9行和两个值。但未列出AccountName和sumcountname数据。只有页眉和页脚。 我现在花了好几个小时比较这两种解决方案,但不知道为什么这一个不显示数据 Overview.xaml: x:Class="Finance_Manager.Overview

我有一个手动创建列表的工作解决方案。 但由于我想从文件中读取数据,因此我不得不改为异步,并且因为数据量将来将更改为ObservableCollection,所以XAML不再显示9行

调试时,我看到{x:Bind Accounts}仍然包含9行和两个值。但未列出AccountNamesumcountname数据。只有页眉和页脚。 我现在花了好几个小时比较这两种解决方案,但不知道为什么这一个不显示数据

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>
x:Class=“财务经理概述”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local=“使用:财务经理”
xmlns:d=”http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data=“使用:Finance\u Manager.Models”
mc:Ignorable=“d”>
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;
        }
    }
}
namespace Finance\u Manager.Models
{
公共类帐户
{
公共字符串AccountName{get;set;}
公共双SumAccountName{get;set;}
}
公共类帐户概述
{
公共静态异步任务GetAccounts()
{
var账户=新的ObservableCollection();
//
//加载文件
var folder=ApplicationData.Current.LocalFolder;
var GetOverviewFile=await folder.GetFileAsync(“overview.json”);
string jsonString=await FileIO.ReadTextAsync(GetOverviewFile);
//
JsonArray root=JsonValue.Parse(jsonString.GetArray();
对于(uint i=0;i
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();
        }
    }
}
namespace财务经理
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类概述:第页
{
公共收款账户;
公众概览()
{
this.InitializeComponent();
}
已加载公共异步无效网格(对象发送方,路由目标)
{
Accounts=await AccountOverview.GetAccounts();
}
}
}

首先,绑定只处理属性而不是字段(您的ObservableCollection帐户只是字段)

另一个问题可能是加载XAML时,
Accounts
属性将为
null
,因此您可能需要添加
Accounts=newobserveCollection()在此之前
this.InitializeComponents()并且不从
AccountOverview.GetAccounts()返回ObservableCollection的整个新实例方法,但只填充已存在的对象。

1)将
帐户
字段更改为属性

2) 如果使用绑定,则应实现
INotifyPropertyChanged
,并在每次更改
Accounts
属性时调用propertychanged()。这将更新视图

3) 默认情况下,
x:Bind
使用
mode=OneTime
,您应该将其更改为
mode=OneWay


如果您想要最快、最简单的解决方案,请删除绑定,只需执行
ListView.ItemSource=MyClass.GetAccounts()
。这对MVVM模式不好,但我看到您正在使用代码隐藏。

我已经做了所有解释,但仍然不起作用


我将
Name=“AccountList”
添加到Overview.xaml的列表视图中。和
AccountList.ItemSource=Accounts到Overview.xaml.cs。现在它可以工作了。

谢谢你的回答!我现在有点挣扎。我使用了Channel9“针对绝对初学者的Windows 10开发”中的示例。如何将帐户从字段更改为属性?没有找到与我不同的示例。在您的情况下,只需更改
公共Obs即可