从UWP中的另一个页面/窗口更新ObservableCollection

时间:2017-01-07 10:39:11

标签: c# sqlite uwp

在了解UWP中的ObservableCollection后我立即尝试了,在1页中工作很简单。但是,当我尝试在一个页面中显示gridview绑定到ObservableCollection,并从另一个页面调用Add Data to ObservableCollection时,我遇到了错误。我不知道我的搜索技能是否仍然太低或者在搜索30米后我找不到任何解决方案。希望这里有一些指南。这是我的代码:

MainPage.xaml中

<Grid.RowDefinitions>
        <RowDefinition Height="50*"></RowDefinition>
        <RowDefinition Height="400*"></RowDefinition>
    </Grid.RowDefinitions>
    <Button x:Name="btnNewWindow" Content="Show add window" FontSize="20" Grid.Row="0" Click="btnNewWindow_Click"></Button>
    <ListView x:Name="listView" Grid.Row="1">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:Test" x:Name="templateGrid">
                <StackPanel x:Name="stackPanel"  Orientation="Vertical" HorizontalAlignment="Center">
                    <TextBlock FontSize="18" Text="{x:Bind id}" HorizontalAlignment="Center"></TextBlock>
                    <TextBlock FontSize="10" Text="{x:Bind name}" HorizontalAlignment="Center"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

MainPage.xaml.cs中

SQLite.Net.SQLiteConnection conn;
    private List<Test> tempList;
    public static ObservableCollection<Test> testList;
    public static string _bookID = "";
    private string bookTitle = "";

    public MainPage()
    {
        this.InitializeComponent();
        string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "testDB.db");
        conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);
        conn.CreateTable<Test>();
        tempList = conn.Query<Test>(@"select * from Test");
        testList = new ObservableCollection<Test>(tempList);
        listView.ItemsSource = testList;
    }

    async private void btnNewWindow_Click(object sender, RoutedEventArgs e)
    {
        CoreApplicationView newView = CoreApplication.CreateNewView();
        int view_id = 0;

        await newView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            Frame frame = new Frame();
            frame.Navigate(typeof(AddPage), null);
            Window.Current.Content = frame;
            Window.Current.Activate();

            view_id = ApplicationView.GetForCurrentView().Id;

            var app_view = ApplicationView.GetForCurrentView();
            app_view.SetPreferredMinSize(new Size(500, 400));

            ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.PreferredLaunchViewSize;
            ApplicationView.PreferredLaunchViewSize = new Size(500, 400);
            app_view.Title = $"Add page";
        });

        await ApplicationViewSwitcher.TryShowAsStandaloneAsync(view_id);
    }

AddPage.xaml

<Grid.RowDefinitions>
        <RowDefinition Height="70*"></RowDefinition>
        <RowDefinition Height="70*"></RowDefinition>
        <RowDefinition Height="70*"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBox x:Name="txtID" Header="ID" Margin="20" Grid.Row="0"></TextBox>
    <TextBox x:Name="txtName" Header="Name" Margin="20" Grid.Row="1"></TextBox>
    <Button x:Name="btnAdd" Margin="20" Grid.Row="2" Content="Add" Click="btnAdd_Click"></Button>

AddPage.xaml.cs

SQLite.Net.SQLiteConnection conn;
    public AddPage()
    {
        this.InitializeComponent();
        string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "testDB.db");
        conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        string id = txtID.Text;
        string name = txtName.Text;
        MainPage.testList.Add(new App2.Test{ id = id, name = name });//Error here
        conn.Execute(@"insert into Test values (?,?)", id, name);
        Window.Current.Close();
    }

测试类:

public class Test
{
        public string id { get; set; }
        public string name { get; set; }

        public Test(){}
}

错误:

  

发生了'System.InvalidCastException'类型的异常   System.ObjectModel.dll但未在用户代码中处理       附加信息:无法转换'System.Collections.Specialized.NotifyCollectionChangedEventHandler'类型的COM对象   到班级类型

提前致谢。抱歉,如果重复。

2 个答案:

答案 0 :(得分:2)

我可以重现您的问题。我将使用我们的内部渠道报告。

作为临时解决方法,我们应该能够使用Popup来替换新视图。我们也可以毫无例外地将项目插入ObservableCollection

例如:

XAML代码:

<Popup Name="MyPopup">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="70*"></RowDefinition>
            <RowDefinition Height="70*"></RowDefinition>
            <RowDefinition Height="70*"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox x:Name="txtID" Header="ID" Margin="20" Grid.Row="0"></TextBox>
        <TextBox x:Name="txtName" Header="Name" Margin="20" Grid.Row="1"></TextBox>
        <Button x:Name="btnAdd" Margin="20" Grid.Row="2" Content="Add" Click="btnAdd_Click"></Button>
    </Grid>
</Popup>

背后的代码:

private void btnNewWindow_Click(object sender, RoutedEventArgs e)
{
    MyPopup.IsOpen = true;
}

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
    string id = txtID.Text;
    string name = txtName.Text;
    testList.Add(new App2.Test { id = id, name = name });
    conn.Execute(@"insert into Test values (?,?)", id, name);
    MyPopup.IsOpen = false;
}

答案 1 :(得分:0)

我可以为您注意几件事:

如果您的问题不是交叉线程,您能否澄清此异常发生的位置和时间?例如在启动添加窗口或关闭时,以及哪行代码。

道歉,因为无法对此进行评论,缺乏声誉:/