更新List<比;用于xaml和重载框架

本文关键字:xaml 重载 框架 用于 List 更新 | 更新日期: 2023-09-27 18:12:41

所以我有一个XML文件,我加载数据到List<>。这意味着我有一个包含我需要的所有数据的主列表。

从该列表中创建变量"sub"列表,然后根据需要使用它来显示数据。

我的主窗口(目前)包含一个框架,我用它来加载页面。

我的问题是,我不知道你如何改变数据显示在一个页面上,重新加载它,并保持它从显示标准数据。

所以我认为有两种方法来解决这个问题,但我需要建议哪一种是正确的(工作)方法。

所以在我的page1(加载到框架中)我有以下代码:
public sealed partial class Page1 : Page
{
    public List<Book> Books;
    public List<Book> ts1;
    public Page1()
    {
        this.InitializeComponent();
        Books = BookManager.GetBooks();
        ts1 = Books.Where(p => p.bogNa == "Robert").ToList();
    }
    private void Update_Click(object sender, RoutedEventArgs e)
    {
        ts1 = Books.Where(p => p.bogNa != "Robert").ToList();
    }
}

所以我可以用我的按钮更新列表,但显然,当我然后重新加载帧,我们只是回到原点。我能和书上的不一样吗?

在我的page1 xaml我有这个代码:

<GridView ItemsSource="{x:Bind ts1}" IsItemClickEnabled="True" SelectionMode="Multiple">
        <GridView.ItemTemplate>
            <DataTemplate x:DataType="data:Book">
                <StackPanel>
                    <Grid Height="200">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="3*" />
                            <RowDefinition Height="1*" />
                            <RowDefinition Height="1*" />
                            <RowDefinition Height="1*" />
                        </Grid.RowDefinitions>
                        <Image Grid.Row="0" Name="pic" Width="150" Source="{x:Bind bogCo }"/>
                        <TextBlock Grid.Row="1" FontSize ="20" Text="{x:Bind bogNa}" />
                        <TextBlock Grid.Row="2" FontSize ="16" Text="{x:Bind bogRe}"/>
                        <Button Grid.Row="3" Content="More Info" FontSize="16">
                            <Button.Flyout>
                                <Flyout x:Name="FlyoutTest">
                                    <TextBlock Text="{x:Bind bogAr}">
                                    </TextBlock>
                                </Flyout>
                            </Button.Flyout>
                        </Button>
                    </Grid>
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

有没有办法改变

<GridView ItemSource="{x:bind ts1}" ...

到与page1不同的页面,如MainPage?

我已经研究了制作数据模板,但我不知道这是要走的路吗?因为我现在不想把时间花在这上面,如果它会导致同样的问题。

更新List<比;用于xaml和重载框架

解决这个问题的一个简单方法是为Page1启用NavigationCacheMode

这将缓存page1的状态,并在重新打开页面时显示旧状态。请记住,一旦页面初始化,就不能清空缓存。此外,NavigationCacheMode必须在页的构造函数中设置。

public Page1()
{
    this.InitializeComponent();
    NavigationCacheMode = NavigationCacheMode.Enabled; // or NavigationCacheMode.Required if you want to ignore caching limits.
    Books = BookManager.GetBooks();
    ts1 = Books.Where(p => p.bogNa == "Robert").ToList();
}

但是就像Inbar Barkai说的,一个更简洁的解决方案是保留page1的ViewModel

所以我找到了一个很好的解决方案:ObservableCollection

 public sealed partial class MainPage : Page
    {
        public List<dataRaw> t1;
        public List<dataRaw> t3;
        public ObservableCollection<dataRaw> t2;
        public MainPage()
        {
            this.InitializeComponent();
            //So you call the information from a class file (normally in Models)
            t1 = collectionGenerator.getList();
            //Then you use the t1 (List<T>) and convert it to an ObservableCollection
            t2 = new ObservableCollection<dataRaw>(t1);
        }
        private void Filter_Click(object sender, RoutedEventArgs e)
        {
            //Gather user input from a textbox (TB1)
            // clear the data in ObservableCollection t2
            t1 = collectionGenerator.getList();
            t2.Clear();
            string data = TB1.Text;
            //Check what input the user have entered
            if(data != "")
            {
            var Vtest = from VT in t1
                        where VT.firstName == data || VT.lastName == data
                        select VT;
                foreach (var VT in Vtest)
                {
                    //Add the given data back to the t2
                    t2.Add(new dataRaw { data = "1", firstName = VT.firstName, lastName = VT.lastName });
                }
            }
            else
            {
                var Vtest = from VT in t1
                            where VT.data == "1"
                            select VT;
                foreach (var VT in Vtest)
                {
                    t2.Add(new dataRaw { data = "1", firstName = VT.firstName, lastName = VT.lastName });
                }
            }
          }
        }