C# 从UWP中的另一页/窗口更新ObservableCollection
在了解UWP中的ObservableCollection之后,我立即尝试了它,在1页中工作就足够简单了。但是当我试图在一个页面中显示gridview绑定到ObservableCollection,并从另一个页面调用AddData到该ObservableCollection时,我遇到了错误。我不知道我的搜索技能是否仍然太低,但是在30米的搜索之后,我没有找到解决办法。希望这里有一些导游。这是我的代码: MainPage.xamlC# 从UWP中的另一页/窗口更新ObservableCollection,c#,sqlite,uwp,C#,Sqlite,Uwp,在了解UWP中的ObservableCollection之后,我立即尝试了它,在1页中工作就足够简单了。但是当我试图在一个页面中显示gridview绑定到ObservableCollection,并从另一个页面调用AddData到该ObservableCollection时,我遇到了错误。我不知道我的搜索技能是否仍然太低,但是在30米的搜索之后,我没有找到解决办法。希望这里有一些导游。这是我的代码: MainPage.xaml <Grid.RowDefinitions>
<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>
测试等级:
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对象
到类类型
提前谢谢。如果复制了,很抱歉。有几件事我可以提醒您:
- 不会在窗口的主线程上调用按钮单击事件。对于 此问题的解决方案请参见和
- 代码中的实际异常是无效的强制转换,我怀疑这可能是由于XAML绑定期望的类型不是您提供的类型
抱歉不能对此发表评论,因为缺乏声誉://我可以重复您的问题。我将使用我们的内部渠道进行报告 作为临时解决方法,我们应该能够使用
弹出窗口
替换新视图。此外,我们可以毫无例外地将项目插入到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>
我相信问题可能出在btnAdd_Click事件方法中。如果字符串id与int view_id相关,则可能只需要对其进行解析。i、 e.代替字符串id=0;int.TryParse(txtID.Text,out-id)。如果这还不能解决问题,您可以发布您的测试类字段和与id和名称相关的属性吗?@David Cardinale:在这里,我在问题中添加了测试类代码。对不起,我忘了。我相信问题出在这行:templast=conn.Query(@“select*fromtest”);圣殿骑士需要一个类型。我将创建一个单独的方法来执行此查询并返回一个列表。不幸的是,我必须运行,但我希望这有点帮助。当我点击第二页的“添加”按钮时出现异常。在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(){}
}
<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;
}