Xamarin.forms 不显示Cosmos数据库中的项目
我的显示方法有问题。我一直在学习xamarin的MSToDo项目教程。 我能够插入到数据库中,所以我知道它可以工作,但无法从数据库中获取任何信息。你能帮我吗 这是我的获取方法Xamarin.forms 不显示Cosmos数据库中的项目,xamarin.forms,Xamarin.forms,我的显示方法有问题。我一直在学习xamarin的MSToDo项目教程。 我能够插入到数据库中,所以我知道它可以工作,但无法从数据库中获取任何信息。你能帮我吗 这是我的获取方法 static DocumentClient docClient = null; static readonly string databaseName = "xxxxx"; static readonly string collectionName = "xxxxx"; s
static DocumentClient docClient = null;
static readonly string databaseName = "xxxxx";
static readonly string collectionName = "xxxxx";
static bool Initialize() //connection
{
if (docClient != null)
return true;
try
{
docClient = new DocumentClient(new Uri(AppConstants.CosmosEndpointUrl), AppConstants.CosmosAuthKey);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
docClient = null;
return false;
}
return true;
}
public async static Task<List<Post>> GetPosts()
{
var posts = new List<Post>();
if (! Initialize())
return posts;
var postQuery = docClient.CreateDocumentQuery<Post>(
UriFactory.CreateDocumentCollectionUri(databaseName, collectionName),
new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true })
.OrderBy(i => i.Date)
.AsDocumentQuery();
while (postQuery.HasMoreResults)
{
var queryResults = await postQuery.ExecuteNextAsync<Post>();
posts.AddRange(queryResults);
}
return posts;
}
public class PostViewModel : BaseViewModel
{
List<Post> posts;
public PostViewModel()
{
Posts = new List<Post>();
RefreshCommand = new Command(async () => await ExecuteRefreshCommand());
}
private PostViewModel _selectedAd;
//private ObservableCollection<Post> _posts;
public List<Post> Posts { get => posts; set { posts = value; OnPropertyChanged(); } }
public ICommand RefreshCommand { get; }
async Task ExecuteRefreshCommand()
{
if (IsBusy) return;
IsBusy = true;
try
{
Posts = await AdService.GetPosts();
}
finally
{
IsBusy = false;
}
}
还有我的xaml
<ListView
ItemsSource="{Binding Posts}" x:Name="AdLogListView"
ItemTemplate="{StaticResource HomePageTemplate}"
SelectionMode="Single" Margin="12,0">
绑定源路径应该是Post而不是Post。您修改的xaml将是:-
<ListView
ItemsSource="{Binding Posts}" x:Name="AdLogListView"
ItemTemplate="{StaticResource HomePageTemplate}"
SelectionMode="Single" Margin="12,0">
是的,刚刚添加了,我得到System.InvalidCastException:'指定的强制转换无效。'PostViewModel PostViewModel//专用SQLite连接_conn;公共主页(){InitializeComponent();postViewModel=new postViewModel();BindingContext=postViewModel;}受保护的覆盖void OnAppearing(){base.OnAppearing();postViewModel.RefreshCommand.Execute(null);}没有发生什么事,但现在它起作用了。。这我要归功于你好的,谢谢你!让我知道进一步的疑问。
<ListView
ItemsSource="{Binding Posts}" x:Name="AdLogListView"
ItemTemplate="{StaticResource HomePageTemplate}"
SelectionMode="Single" Margin="12,0">