我正在處理這個表面項目,我們有一個bing地圖控件,並且我們想通過使用數據綁定在地圖上繪製多段線。WPF bing地圖控制首先添加到集合的多義線/多邊形
發生的奇怪行爲是當我點擊添加按鈕時,地圖上沒有任何反應。如果我稍微移動地圖,多段線就會繪製在地圖上。這種類型的另一種場景是單擊添加按鈕一次,沒有任何反應,再次單擊它就會繪製兩條折線。 (在我的手動集合中,我有4個LocationCollections),所以第三次單擊和第四次單擊都會發生同樣的情況,即再次繪製兩條線。
我完全不知道在哪裏再來解決這個問題。我嘗試訂閱Layoutupdated事件,這兩種情況都會發生。還將一個collectionchanged事件添加到observablecollection,以查看添加是否被觸發,並且是否觸發。我嘗試的另一件事是將折線更改爲圖釘,並從pipelineviewmodel中的位置集合中獲取第一個位置,而不是預期的工作。
我已經上傳sample project如果你想看看自己在發生什麼事情。
真的希望有人能指引我走向正確的方向,因爲我再也沒有線索了。
下面你可以看到我寫的代碼:
我有以下的ViewModels:
MainViewModel
public class MainViewModel
{
private ObservableCollection<PipelineViewModel> _pipelines;
public ObservableCollection<PipelineViewModel> Pipes
{
get { return _pipelines; }
}
public MainViewModel()
{
_pipelines = new ObservableCollection<PipelineViewModel>();
}
}
而且PipelineViewModel其中有位置的集合,其實現INotifyPropertyChanged :
Pipelin eViewModel
public class PipelineViewModel : ViewModelBase
{
private LocationCollection _locations;
public string Geometry { get; set; }
public string Label { get; set; }
public LocationCollection Locations
{
get { return _locations; }
set
{
_locations = value;
RaisePropertyChanged("Locations");
}
}
}
我的XAML看起來像如下:
<s:SurfaceWindow x:Class="SurfaceApplication3.SurfaceWindow1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
Title="SurfaceApplication3">
<s:SurfaceWindow.Resources>
<DataTemplate x:Key="Poly">
<m:MapPolyline Locations="{Binding Locations}" Stroke="Black" StrokeThickness="5" />
</DataTemplate>
</s:SurfaceWindow.Resources>
<Grid>
<m:Map ZoomLevel="8" Center="52.332074,5.542302" Name="Map">
<m:MapItemsControl Name="x" ItemsSource="{Binding Pipes}" ItemTemplate="{StaticResource Poly}" />
</m:Map>
<Button Name="add" Width="100" Height="50" Content="Add" Click="add_Click"></Button>
</Grid>
</s:SurfaceWindow>
而在我們的代碼隱藏我們正在建立綁定像這樣的單擊事件:
private int _counter = 0;
private string[] geoLines;
private MainViewModel _mainViewModel = new MainViewModel();
/// <summary>
/// Default constructor.
/// </summary>
public SurfaceWindow1()
{
InitializeComponent();
// Add handlers for window availability events
AddWindowAvailabilityHandlers();
this.DataContext = _mainViewModel;
geoLines = new string[4]{ "52.588032,5.979309; 52.491143,6.020508; 52.397391,5.929871; 52.269838,5.957336; 52.224435,5.696411; 52.071065,5.740356",
"52.539614,4.902649; 52.429222,4.801025; 52.308479,4.86145; 52.246301,4.669189; 52.217704,4.836731; 52.313516,5.048218",
"51.840869,4.394531; 51.8731,4.866943; 51.99841,5.122375; 52.178985,5.438232; 51.8731,5.701904; 52.071065,6.421509",
"51.633362,4.111633; 51.923943,6.193542; 52.561325,5.28717; 52.561325,6.25946; 51.524125,5.427246; 51.937492,5.28717" };
}
private void add_Click(object sender, RoutedEventArgs e)
{
PipelineViewModel plv = new PipelineViewModel();
plv.Locations = AddLinestring(geoLines[_counter]);
plv.Geometry = geoLines[_counter];
_mainViewModel.Pipes.Add(plv);
_counter++;
}
private LocationCollection AddLinestring(string shapegeo)
{
LocationCollection shapeCollection = new LocationCollection();
string[] lines = Regex.Split(shapegeo, ";");
foreach (string line in lines)
{
string[] pts = Regex.Split(line, ",");
double lon = double.Parse(pts[1], new CultureInfo("en-GB"));
double lat = double.Parse(pts[0], new CultureInfo("en-GB"));
shapeCollection.Add(new Location(lat, lon));
}
return shapeCollection;
}
我不能幫你這個但測試你的樣本項目;做了一些試驗和錯誤的東西(無效,強制移動地圖),但也沒有線索爲什麼它不工作。你實施的一切看起來都很好。 但是,這裏有一些發現:從CodeBehind添加MapPolyline工作正常。如果您使用另一個像Pushpin的元素,它也可以正常工作。所以這個問題與所有從MapShapeBase繼承的東西有關。那就是MapPolyline和MapPolygon。我通過Reflector查看了它,並嘗試將Pushpin實現與MapPolyline實現進行比較。 – SvenG
我不能投入更多時間,但如果我能夠調試反射代碼,並看看爲什麼Pushpin刷新正確,但MapPolyLine/MapPolygon不是。 – SvenG
嗨SvenG,感謝您的時間看着它。是的,我已經看到,圖釘工作正常。我也在MapItemsControl上調用了方法UpdateLayout(),並向圖層添加了一個空的UIElement,它將顯示折線。仍然hav eno線索爲什麼它不工作:( – ChristiaanV