【问题标题】:How to update a ListView with a Button click in different pages?如何使用不同页面中的按钮单击更新 ListView?
【发布时间】:2017-08-23 22:04:58
【问题描述】:

我有两个不同的 XAML 页面,第一个页面有一个 Button,另一个页面有一个 ListView。我已经完成了一些绑定工作并创建了一个“模型”类。
这是我的列表视图:

<ListView x:Name="ListaEventos" ItemsSource="{x:Bind Eventos}" FontFamily="Segoe UI Emoji">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="data:Evento">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{x:Bind Minuto}" Style="{ThemeResource CaptionTextBlockStyle}" VerticalAlignment="Center"/>
                            <TextBlock Text="{x:Bind Segundo}" Style="{ThemeResource CaptionTextBlockStyle}" VerticalAlignment="Center"/>
                            <TextBlock Text="{x:Bind Icono}" Margin="10,0,0,0" VerticalAlignment="Center"/>
                            <TextBlock Text="{x:Bind Accion}" Margin="10,0,0,0" VerticalAlignment="Center"/>
                            <TextBlock Text="{x:Bind Equipo}" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

这是我的模型类:

public class Evento
{
    public string Minuto { get; set; }

    public string Segundo { get; set; }

    public string Icono { get; set; }

    public string Accion { get; set; }

    public string Equipo { get; set; }

}

最后,我在其他页面有一个按钮,我想添加一个项目,如下所示:

Eventos.Add(new Evento { Minuto = "10", Segundo = "00", Icono = "????", Accion="Triple de", Equipo=" Visitante" });

我做了很多研究,但没有找到任何有用的信息或类似的简单信息。请点赞,我需要你的帮助,谢谢!

【问题讨论】:

    标签: c# visual-studio xaml binding uwp


    【解决方案1】:

    我们可以定义一个静态ObservableCollection 来在其他页面中添加新项目。我们无法将ListView 绑定到静态ObservableCollection,我们可以覆盖OnNavigatedTo 事件并将静态ObservableCollection 设置为Eventos

    例如:

    public static  ObservableCollection<Evento> StaticEvento;
    public  ObservableCollection<Evento> Eventos { get; set; }
    public MainPage()
    {
        this.InitializeComponent();
        if (StaticEvento == null)
        {
            StaticEvento = new ObservableCollection<Evento>();
            StaticEvento.Add(new Evento { Minuto = "20", Segundo = "00", Icono = "?", Accion = "Triple de", Equipo = " Visitante" });
            Eventos = StaticEvento;
        }
    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Eventos = StaticEvento;
    }
    

    另一页:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MainPage.StaticEvento.Add(new Evento { Minuto = "10", Segundo = "00", Icono = "?", Accion = "Triple de", Equipo = " Visitante" });
        Frame rootFrame = Window.Current.Content as Frame;
        rootFrame.GoBack();
    }
    

    【讨论】:

    • 这对我不起作用,因为按钮的页面出现在 ListView 页面之前,所以它抛出了这个异常“System.NullReferenceException:'对象引用未设置为对象的实例。' "
    • @PabloJiménezPascual 如果您一开始没有加载 ListView 页面,您应该能够在添加新项目之前实例化静态 ObservableCollection
    • 谢谢,我终于设法在 Button 的页面中声明了“StaticEvento”并更改了一些代码以适合我的情况。
    猜你喜欢
    • 1970-01-01
    • 2021-11-15
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2014-05-02
    相关资源
    最近更新 更多