After knowing about ObservableCollection in UWP i try it out immediately, working in 1 page is simple enough. But when i try to show the gridview bind to ObservableCollection in one page, and call Add data to that ObservableCollection from another page i ran into error. I dont know if my searching skill is still too low or what but after 30m searching i found no solution. Hoping for some guides here. Here my code:
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();
}
Test class:
public class Test
{
public string id { get; set; }
public string name { get; set; }
public Test(){}
}
Error:
An exception of type 'System.InvalidCastException' occurred in System.ObjectModel.dll but was not handled in user code Additional information: Unable to cast COM object of type 'System.Collections.Specialized.NotifyCollectionChangedEventHandler' to class type
Thanks in advance. Sorry if duplicated.