0

I have a method that populates a List with 5 random images. The method returns correctly. When I call the method to populate the List before this.InitializeComponent(); in the codebehind, the images appear on screen. However, when I call the method afterwards, it has no effect on what is shown on screen. What can I do to fix this? It seems like I need to call RaisePropertyChanged() or something along these lines, but I can't find a way to do this. Can anyone please help ?

In my code behind I have the code :

 public List<BitmapImage> listOfImages { get; set; }

  private async void Get_Images(object sender, RoutedEventArgs e)
        {
            //code to get 5 random images
            IReadOnlyList<StorageFile> fileList = await query.GetFilesAsync();
            listOfImages = new List<BitmapImage>();
            foreach (StorageFile file in fileList)
            {
                BitmapImage src = new BitmapImage();
                src.SetSource(await file.OpenAsync(FileAccessMode.Read));
                listOfRelatedImages.Add(src);
            }
        }

And in my XAML :

 <ItemsControl ItemsSource="{Binding Path=listOfImages}" HorizontalContentAlignment="Stretch">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel VerticalAlignment="Center">
                <Image x:Name="images" Source="{Binding}" Visibility="Visible" Stretch="Fill">
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
 </ItemsControl>

The OverlayView approach is the way to do it. If it is not working then it could be because the map hasn't finished loading before you try to get the overlay projection.

Once you have created the map and the overlay, wait for the map idle event trigger and then attempt to use the overlay.

google.maps.event.addListenerOnce(map, 'idle', function() {
    aPoint=overlay.getProjection().fromLatLngToDivPixel(labelA.getPoint()); 
    bPoint=overlay.getProjection().fromLatLngToDivPixel(labelB.getPoint()); 
});

EDIT - Updated answer to reflect question changes

It's not a good idea to create the overlay and wait for the idle event every time you call the function. You should create the map and overlay once and continue the application once the map has loaded.

For Example

var map, overlay;

var labelLatLongSort = function(a, b) {
    labelA = eval("label_"+a.deviceId);
    labelB = eval("label_"+b.deviceId);
    longOffset = eval("offsetOverlapWith_"+a.customId);
    aPoint=overlay.getProjection().fromLatLngToDivPixel(labelA.getPoint()); 
    bPoint=overlay.getProjection().fromLatLngToDivPixel(labelB.getPoint());
    diffLat = bPoint.y-aPoint.y;
    diffLong = aPoint.x-bPoint.x; 
    if (Math.abs(diffLong)>longOffset) return diffLong;
    if (diffLat==0) {
        return b.deviceId-a.deviceId;
    }
    return diffLat;
}

var init = function(callback) {
    map = new google.maps.Map(div, options);
    overlay = new google.maps.OverlayView();
    overlay.draw = function() {};
    overlay.setMap(map);
    google.maps.event.addListenerOnce(map, 'idle', function() {
        callback();
    });
}

var startApp = function() {
    var diffLat = labelLatLongSort(a, b);
}


// start application on DOM load
init(startApp);

What I did here was put my application code inside a function called startApp and pass this into the init function as a callback. Once the init function has loaded the map by waiting for the idle event, it calls the callback function.

Depending on your application you may need to change the setup slightly.

4

2 に答える 2

3

List の変更 (add() や remove() など) を Datagrid に知らせるには、ObservableCollectionを使用する必要があります。

リスト内の変更 (プロパティの変更など) について DataGrid に通知する場合、DataGrid に表示されるクラスはINotifyPropertyChangedインターフェイスを実装する必要があります。

于 2013-01-11T11:29:09.763 に答える
2

List の代わりにObservableCollectionを使用する

ItemsControl はコレクションの変更 (追加/削除/リセット/移動) を INotifyCollectionChanged インターフェイスで追跡するため、ItemsControl を自動的に更新する場合は、このインターフェイスをリストに実装する必要があります。WPF には、このインターフェイスを実装するジェネリック コレクションが既に含まれています。

交換

List<BitmapImage> listOfImages

ObservableCollection<BitmapImage> listOfImages

そして、それはうまくいくはずです

于 2013-01-11T11:29:35.253 に答える