Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用从MongoDB检索的数组中的数据。使用数据在地图上创建图钉_C#_Arrays_Wpf_Mongodb_Bing Maps - Fatal编程技术网

C# 使用从MongoDB检索的数组中的数据。使用数据在地图上创建图钉

C# 使用从MongoDB检索的数组中的数据。使用数据在地图上创建图钉,c#,arrays,wpf,mongodb,bing-maps,C#,Arrays,Wpf,Mongodb,Bing Maps,我正在尝试用c#,WPF创建一个应用程序,它将从MongoDB获取数据。我对MongoDB进行了基本的查询,我也从DB中获取了数据,但是我尝试使用的地理数据是这样存储的:“geo”:{“type”:“Point”,“coordinates”:[59.28973919,11.1446193]} 所以我像这样检索数据: public static MongoDatabase GetDatabase(string searchText) { MongoServerSettings setti

我正在尝试用c#,WPF创建一个应用程序,它将从MongoDB获取数据。我对MongoDB进行了基本的查询,我也从DB中获取了数据,但是我尝试使用的地理数据是这样存储的:“geo”:{“type”:“Point”,“coordinates”:[59.28973919,11.1446193]}

所以我像这样检索数据:

public static MongoDatabase GetDatabase(string searchText)
{
     MongoServerSettings settings = new MongoServerSettings();
     settings.Server = new MongoServerAddress("database.example.com", 27017);
     MongoServer server = new MongoServer(settings);
     MongoDatabase database = server.GetDatabase("tweet_database");
     var collection = database.GetCollection<Tweets>("docs");
     System.Console.WriteLine("5");
     var query = Query.And(Query.Matches("text", searchText),
     Query.NE("geo_enabled", false));

     System.Console.WriteLine("6");
            
     var cursor = collection.Find(query);
     cursor.SetLimit(20);
     System.Console.WriteLine("7");

     //Puts the result from the last query into a list.
     var resultList = cursor.ToList();

     //Iterates over the previous mentioned list and inserts the content into the ObservableCollcetion created earlier.
     foreach (var item in resultList)
     TweetOC.Add(item);
     System.Console.WriteLine(TweetOC.Count);

     return database;
}
<Window.Resources>
   

    <DataTemplate x:Key="Maptemplate">
        <m:Pushpin m:MapLayer.Position="{Binding geo}" 
           Tag="{Binding}"  
                   
           Content="{Binding text}">
            
            <m:Pushpin.DataContext>
                <local:Tweets/>
            </m:Pushpin.DataContext>

        </m:Pushpin>
    </DataTemplate>

</Window.Resources>
<m:Map x:Name="myMap" CredentialsProvider="API-Key" Mode="Road"  Margin="1,0,0,0"
           Center="58.1453,7.9571,0.0000" ZoomLevel="9.000" Grid.Column="1" Grid.Row="2" >            
            <m:Map.Children>
            <m:MapItemsControl
               ItemsSource="{Binding TweetOC}"  
                ItemTemplate="{StaticResource Maptemplate}" >
                    <m:MapItemsControl.DataContext>
                        <local:Docs/>
                    </m:MapItemsControl.DataContext>
                </m:MapItemsControl>
                <m:MapLayer x:Name="ContentPopupLayer">
                        <Grid x:Name="ContentPopup"
                          Visibility="Collapsed"
                          Background="White"
                          Opacity="0.85">
                            <StackPanel Margin="15">
                                <TextBlock x:Name="ContentPopupText"
                                     FontSize="12"
                                     FontWeight="Bold"/>
                                <TextBlock x:Name="ContentPopupDescription"
                                         FontSize="12"/>
                            </StackPanel>
                        </Grid>
                    </m:MapLayer>           
        </m:Map.Children>
    </m:Map>
}

XAML是这样的:

public static MongoDatabase GetDatabase(string searchText)
{
     MongoServerSettings settings = new MongoServerSettings();
     settings.Server = new MongoServerAddress("database.example.com", 27017);
     MongoServer server = new MongoServer(settings);
     MongoDatabase database = server.GetDatabase("tweet_database");
     var collection = database.GetCollection<Tweets>("docs");
     System.Console.WriteLine("5");
     var query = Query.And(Query.Matches("text", searchText),
     Query.NE("geo_enabled", false));

     System.Console.WriteLine("6");
            
     var cursor = collection.Find(query);
     cursor.SetLimit(20);
     System.Console.WriteLine("7");

     //Puts the result from the last query into a list.
     var resultList = cursor.ToList();

     //Iterates over the previous mentioned list and inserts the content into the ObservableCollcetion created earlier.
     foreach (var item in resultList)
     TweetOC.Add(item);
     System.Console.WriteLine(TweetOC.Count);

     return database;
}
<Window.Resources>
   

    <DataTemplate x:Key="Maptemplate">
        <m:Pushpin m:MapLayer.Position="{Binding geo}" 
           Tag="{Binding}"  
                   
           Content="{Binding text}">
            
            <m:Pushpin.DataContext>
                <local:Tweets/>
            </m:Pushpin.DataContext>

        </m:Pushpin>
    </DataTemplate>

</Window.Resources>
<m:Map x:Name="myMap" CredentialsProvider="API-Key" Mode="Road"  Margin="1,0,0,0"
           Center="58.1453,7.9571,0.0000" ZoomLevel="9.000" Grid.Column="1" Grid.Row="2" >            
            <m:Map.Children>
            <m:MapItemsControl
               ItemsSource="{Binding TweetOC}"  
                ItemTemplate="{StaticResource Maptemplate}" >
                    <m:MapItemsControl.DataContext>
                        <local:Docs/>
                    </m:MapItemsControl.DataContext>
                </m:MapItemsControl>
                <m:MapLayer x:Name="ContentPopupLayer">
                        <Grid x:Name="ContentPopup"
                          Visibility="Collapsed"
                          Background="White"
                          Opacity="0.85">
                            <StackPanel Margin="15">
                                <TextBlock x:Name="ContentPopupText"
                                     FontSize="12"
                                     FontWeight="Bold"/>
                                <TextBlock x:Name="ContentPopupDescription"
                                         FontSize="12"/>
                            </StackPanel>
                        </Grid>
                    </m:MapLayer>           
        </m:Map.Children>
    </m:Map>

当我尝试使用此命令时,会出现以下错误:无法将“WpfApplication1.geo”类型的对象强制转换为“System.IConvertible”。

错误非常明显。处理所需数据转换的转换器类在哪里?以
IValueConverter
为例,我尝试编写一个自定义转换器(将用它更新问题),但它不起作用,主要是因为我有点不确定应该转换成什么以及如何转换,因为数据存储在数组中。
<Window.Resources>
   

    <DataTemplate x:Key="Maptemplate">
        <m:Pushpin m:MapLayer.Position="{Binding geo}" 
           Tag="{Binding}"  
                   
           Content="{Binding text}">
            
            <m:Pushpin.DataContext>
                <local:Tweets/>
            </m:Pushpin.DataContext>

        </m:Pushpin>
    </DataTemplate>

</Window.Resources>
<m:Map x:Name="myMap" CredentialsProvider="API-Key" Mode="Road"  Margin="1,0,0,0"
           Center="58.1453,7.9571,0.0000" ZoomLevel="9.000" Grid.Column="1" Grid.Row="2" >            
            <m:Map.Children>
            <m:MapItemsControl
               ItemsSource="{Binding TweetOC}"  
                ItemTemplate="{StaticResource Maptemplate}" >
                    <m:MapItemsControl.DataContext>
                        <local:Docs/>
                    </m:MapItemsControl.DataContext>
                </m:MapItemsControl>
                <m:MapLayer x:Name="ContentPopupLayer">
                        <Grid x:Name="ContentPopup"
                          Visibility="Collapsed"
                          Background="White"
                          Opacity="0.85">
                            <StackPanel Margin="15">
                                <TextBlock x:Name="ContentPopupText"
                                     FontSize="12"
                                     FontWeight="Bold"/>
                                <TextBlock x:Name="ContentPopupDescription"
                                         FontSize="12"/>
                            </StackPanel>
                        </Grid>
                    </m:MapLayer>           
        </m:Map.Children>
    </m:Map>
public class GeoConverter : IValueConverter
{
   
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        object geo = value;

        double asDouble = System.Convert.ToDouble(value);
        return asDouble;
    }
    
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {

        object geo = value;

        double asDouble = System.Convert.ToDouble(value);
        return asDouble;
    }
}