UWP 网格数据不显示在自动刷新中

UWP Grid data not display in auto-refresh

我是开发 UWP 桌面应用程序的新手。网格在自动刷新阶段无法显示数据。

我正在尝试创建一个带有网格的页面,该网格将从数据库中检索数据。加载页面时,我启动一个计时器并每 10 秒刷新一次网格。 当页面只包含网格时,结果很好。 但是如果我在顶部添加一个命令栏,网格将无法在第一次刷新时显示数据。数据可以在页面加载时第一时间显示。 谁能告诉我哪里做错了?

隐藏代码:

public sealed partial class DataGridPage : Page
{
    public string Titletext="";
    public DataGridViewModel ViewModel { get; } = new DataGridViewModel();

    DispatcherTimer dispatcherTimer;
    DateTimeOffset startTime;
    DateTimeOffset lastTime;
    DateTimeOffset stopTime;
    int timesTicked = 1;
    int timesToTick = 10;

    // TODO WTS: Change the grid as appropriate to your app, adjust the column definitions on DataGridPage.xaml.
    // For more details see the documentation at https://docs.microsoft.com/windows/communitytoolkit/controls/datagrid
    public DataGridPage()
    {
        InitializeComponent();
    }

    private void Page_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        DispatcherTimerSetup();
    }

    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        await ViewModel.LoadDataAsync();
        //if (Titletext != "")
        //{
        //    GridTitle.Text = Titletext;
        //}
    }

    public void DispatcherTimerSetup()
    {
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += dispatcherTimer_Tick;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
        //IsEnabled defaults to false
        //FormattingCommandBar.Content += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
        startTime = DateTimeOffset.Now;
        lastTime = startTime;
        //FormattingCommandBar.Content = "Calling dispatcherTimer.Start()";
        dispatcherTimer.Start();
        //IsEnabled should now be true after calling start
        //GridTitle.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
    }

    async void dispatcherTimer_Tick(object sender, object e)
    {
        DateTimeOffset time = DateTimeOffset.Now;
        TimeSpan span = time - lastTime;
        lastTime = time;
        await ViewModel.LoadDataAsync();
        //Time since last tick should be very very close to Interval
        //FormattingCommandBar.Content = "Running dispatcherTimer: " + ViewModel.Source.Count;
        timesTicked++;
        if (timesTicked > timesToTick)
        {
            stopTime = time;
            //FormattingCommandBar.Content = "Calling dispatcherTimer.Stop()\n";
            dispatcherTimer.Stop();
            //IsEnabled should now be false after calling stop
            //GridTitle.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
            span = stopTime - startTime;
            //GridTitle.Text += "Total Time Start-Stop: " + span.ToString() + "\n";
        }
    }
}

xaml(数据可以自动刷新显示)

<Page.Content>
        <Grid x:Name="ContentArea">
            <Grid>
                    <controls:DataGrid
                    AutoGenerateColumns="False"
                    GridLinesVisibility="Horizontal"
                    ItemsSource="{x:Bind Mode=OneWay, Path=ViewModel.Source}" >
                        <controls:DataGrid.Columns>
                            <controls:DataGridTextColumn Binding="{Binding PaymentNo}" Header="Payment No." />
                            <controls:DataGridTextColumn Binding="{Binding PaymentDate}" Header="Payment Date" />
                            <controls:DataGridTextColumn Binding="{Binding Dept}" Header="Department" />
                            <controls:DataGridTextColumn Binding="{Binding PAYCD}" Header="Payee Code" />
                            <controls:DataGridTextColumn Binding="{Binding Vendor.companyName}" Header="Payee Name" />
                            <controls:DataGridTextColumn Binding="{Binding FC}" Header="F.C." />
                            <controls:DataGridTextColumn Binding="{Binding Amount}" Header="Amount"  />
                            <controls:DataGridTextColumn Binding="{Binding CountryID}" Header="Country ID" />
                        </controls:DataGrid.Columns>
                    </controls:DataGrid>

            </Grid>
        </Grid>
</Page.Content>

xaml(如果添加了stackpanel,自动刷新时无法显示数据)

<Page.Content>
    <StackPanel>
        <Button x:Name="ABC" Content="Options" Click="ABC_Click" ></Button>
        <Grid x:Name="ContentArea">
            <Grid>
                    <controls:DataGrid
                    AutoGenerateColumns="False"
                    GridLinesVisibility="Horizontal"
                    ItemsSource="{x:Bind Mode=OneWay, Path=ViewModel.Source}" >
                        <controls:DataGrid.Columns>
                            <controls:DataGridTextColumn Binding="{Binding PaymentNo}" Header="Payment No." />
                            <controls:DataGridTextColumn Binding="{Binding PaymentDate}" Header="Payment Date" />
                            <controls:DataGridTextColumn Binding="{Binding Dept}" Header="Department" />
                            <controls:DataGridTextColumn Binding="{Binding PAYCD}" Header="Payee Code" />
                            <controls:DataGridTextColumn Binding="{Binding Vendor.companyName}" Header="Payee Name" />
                            <controls:DataGridTextColumn Binding="{Binding FC}" Header="F.C." />
                            <controls:DataGridTextColumn Binding="{Binding Amount}" Header="Amount"  />
                            <controls:DataGridTextColumn Binding="{Binding CountryID}" Header="Country ID" />
                        </controls:DataGrid.Columns>
                    </controls:DataGrid>

            </Grid>
        </Grid>
    </StackPanel>
</Page.Content>

StackPanel 不支持 Data Virtualization。这意味着处理大量数据是一团糟。 Grid 默认支持数据虚拟化,这就是它刷新的原因。

你可以选择 Grid。如果你还想 StackPanel 试试 VirtualizingStackPanel

if i want to have a commandbar on top of the grid, what is the best way to do it

你可以这样做

<Page.Content>
<Grid>
  <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="*"></RowDefinition>
  </Grid.RowDefinitions>
    <Button x:Name="ABC" Content="Options" Click="ABC_Click" ></Button>
    <Grid x:Name="ContentArea" Grid.Row="1">
    ...
    </Grid>
</Grid>
</Page.Content>