如何在 XAML UWP 网格视图中创建分页

本文关键字:视图 创建 分页 网格 UWP XAML | 更新日期: 2023-09-27 18:16:30

high 我很想创建一个简单的分页,其中网格视图项目左右翻转。 我目前有网格视图显示项目......

我希望能够单击或滑动下一步并查看其他项目。

请参阅下面的 XAML 代码:

<Page
x:Class="IMG.InformationKiosk.Views.ProductsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:IMG.InformationKiosk.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:y="using:IMG.InformationKiosk.Models"
mc:Ignorable="d">

<Page.Resources>
    <DataTemplate x:DataType="y:Book" x:Key="BookDataTemplate">
        <StackPanel HorizontalAlignment="Center" DragEnter="StackPanel_DragEnter" DragStarting="StackPanel_DragStarting"  Name="BookStackPanel" >
            <Image  Width="150" Source="{x:Bind CoverImage}"  />
            <TextBlock   FontSize="16" Text="{x:Bind Title}" HorizontalAlignment="Center"   />
            <TextBlock   FontSize="10" Text="{x:Bind Author}" HorizontalAlignment="Center"  />
        </StackPanel>
    </DataTemplate>
</Page.Resources>
<Page.Background>
    <ImageBrush Stretch="Fill" ImageSource="ms-appx:///Assets/background small logo.png"/>
</Page.Background>
<Grid Margin="20">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="5*" />
        <ColumnDefinition Width="1*" />
    </Grid.ColumnDefinitions>

    <GridView   ItemsSource="{x:Bind Books}" 
              CanDragItems="True"  DragItemsCompleted="GridView_DragItemsCompleted"                                    
              DragItemsStarting="GridView_DragItemsStarting"                  
              ItemTemplate="{StaticResource BookDataTemplate}" Margin="10,143,34.5,63" Grid.Column="1" Grid.RowSpan="2"/>
    <StackPanel Grid.Column="2" HorizontalAlignment="Left" Height="365" Margin="10.5,102,0,0" VerticalAlignment="Top" Width="143" Grid.RowSpan="3" Opacity="1" CornerRadius="30">
        <TextBlock Opacity="1" Name="SelectedBooksTextBlock" Margin="12,35,-13,0" Grid.Column="1"/>
        <TextBlock  Opacity="1" Foreground="White"  Grid.Column="1">
            <Run Text="TOTAL" Foreground="White" FontWeight="Bold"/>
        </TextBlock>
    </StackPanel>
    <StackPanel Grid.Column="2" HorizontalAlignment="Left" Height="365" Margin="1.5,113,0,0" VerticalAlignment="Top" Width="152" Background="#FF4D4D4D" Grid.RowSpan="3" Opacity="0.3" CornerRadius="30">
    </StackPanel>

    <Image  Source="ms-appx:///Assets/shoppingBasket.png" AllowDrop="True" Name="ImageBasket" Drop="ImageBasket_Drop" DragOver="ImageBasket_DragOver" Margin="0,13,133,-19" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" />
    <TextBlock x:Name="textBlock" Grid.Column="2" HorizontalAlignment="Left" Margin="60.5,128,-36,0" TextWrapping="Wrap" Text="Cart" VerticalAlignment="Top" Width="107" Height="30" FontWeight="Bold" Foreground="White"/>
</Grid>

C#

public sealed partial class ProductsPage : Page
{
    public List<Book> Books;
    public List<string> BooksSelectedList;
    public Dictionary<string, int> booksNumberSelectedDictionary;
    public ProductsPage()
    {
        this.InitializeComponent();
        Books = BookManager.GetBooks();
        BooksSelectedList = new List<string>();
        booksNumberSelectedDictionary = new Dictionary<string, int>();
    }
    private void GridView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var book = (Book)e.ClickedItem;
        //    ResultTextBlock.Text = "You selected " + book.Title;
        book.NumberOfBookTypeSelected++;
        string selectedBookInfoToDisplay = $"{book.NumberOfBookTypeSelected} {book.Title}";
        if (booksNumberSelectedDictionary.ContainsKey(book.Title))
        {
            booksNumberSelectedDictionary[book.Title] = book.NumberOfBookTypeSelected;
        }
        else
        {
            booksNumberSelectedDictionary.Add(book.Title, book.NumberOfBookTypeSelected);
        }
        SelectedBooksTextBlock.Text = string.Empty;
        foreach (KeyValuePair<string, int> dictionaryItem in booksNumberSelectedDictionary)
        {
            SelectedBooksTextBlock.Text += (Environment.NewLine + $"{dictionaryItem.Value} {dictionaryItem.Key}");
        }
        //      BooksSelectedList.Add(Environment.NewLine + selectedBookInfoToDisplay);

        // SelectedBooksTextBlock.Text += Environment.NewLine + selectedBookInfoToDisplay;
        // code to trigger the item
        /*
            if (book.BookId == 1)
            {
                Frame.Navigate(typeof(MainPage));
            }*/

    }
    private void StackPanel_DropCompleted(UIElement sender, DropCompletedEventArgs args)
    {
    }
    private void StackPanel_Drop(object sender, DragEventArgs e)
    {
        //  var book = (Book)e.ClickedItem;
        var book = (Book)sender;
        // var book = (Book)e.;
        book.NumberOfBookTypeSelected++;
        string selectedBookInfoToDisplay = $"{book.NumberOfBookTypeSelected} {book.Title}";
        if (booksNumberSelectedDictionary.ContainsKey(book.Title))
        {
            booksNumberSelectedDictionary[book.Title] = book.NumberOfBookTypeSelected;
        }
        else
        {
            booksNumberSelectedDictionary.Add(book.Title, book.NumberOfBookTypeSelected);
        }
        SelectedBooksTextBlock.Text = string.Empty;
        foreach (KeyValuePair<string, int> dictionaryItem in booksNumberSelectedDictionary)
        {
            SelectedBooksTextBlock.Text += (Environment.NewLine + $"{dictionaryItem.Value} {dictionaryItem.Key}");
        }
    }
    private void StackPanel_DragEnter(object sender, DragEventArgs e)
    {
    }
    private void StackPanel_DragStarting(UIElement sender, DragStartingEventArgs args)
    {
        args.Data.RequestedOperation = DataPackageOperation.Copy;
    }
    private async void ImageBasket_DragOver(object sender, DragEventArgs e)
    {
         e.AcceptedOperation = DataPackageOperation.Copy;
        try
        {
            if (e.DataView == null) { return; }
            string draggedBookTitle = await e.DataView.GetTextAsync();
            e.DragUIOverride.Caption = $"Add {draggedBookTitle} to cart";
        }
        catch(COMException comex)
        {
        }
    }
    private async void ImageBasket_Drop(object sender, DragEventArgs e)
    {
        if(e.Data == null) { return; }
        var data = e.DataView;
        string dataText = data.ToString();
     //   IAsyncOperation<string> textAsyncTask = e.DataView.GetTextAsync();
        string draggedBookTitle = await e.DataView.GetTextAsync();
        Book draggedBook = Books.Where(b => b.Title == draggedBookTitle).FirstOrDefault();
        draggedBook.NumberOfBookTypeSelected++;
        if (booksNumberSelectedDictionary.ContainsKey(draggedBook.Title))
        {
            booksNumberSelectedDictionary[draggedBook.Title] = draggedBook.NumberOfBookTypeSelected;
        }
        else
        {
            booksNumberSelectedDictionary.Add(draggedBook.Title, draggedBook.NumberOfBookTypeSelected);
        }
        SelectedBooksTextBlock.Text = string.Empty;
        foreach (KeyValuePair<string, int> dictionaryItem in booksNumberSelectedDictionary)
        {
            SelectedBooksTextBlock.Text += (Environment.NewLine + $"{dictionaryItem.Value} {dictionaryItem.Key}");
        }         
    }
    private void GridView_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
    {
        Book draggedBook = (Book)e.Items[0];
        e.Data.SetText(draggedBook.Title);
        e.Data.RequestedOperation = DataPackageOperation.Copy;
    }
    private void MyLV_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
    {
      Book draggedBook = (Book) e.Items[0];
        e.Data.SetText(draggedBook.Title);
        e.Data.RequestedOperation = DataPackageOperation.Copy ;
    }

如何在 XAML UWP 网格视图中创建分页

您可以通过BookManager.GetBooks()方法处理数据的分页。将其更改为传入标识您所在页面的int并返回 X 数量的书籍。

GetBooks(int page) {
    // Return x number of books
}

然后,在滑动网格或按下按钮时,您可以调用BookManager.GetBooks(int page)方法并传递新页码以获取下一个书籍列表。