Skip to main content
 首页 » 编程设计

c#之在当前上下文错误中不存在

2024年02月13日12小虾米

当我尝试将 XAML 中的图像绑定(bind)到背后代码中的 bitmapImage 对象时,它给了我 “在当前上下文中不存在”错误。

代码

BitmapImage bitmapImage = new BitmapImage(); 
PhotoSource.Source = bitmapImage; 
ObservableCollection<BitmapImage> Photos = new ObservableCollection<BitmapImage>(); 
PhotoList.ItemsSource = Photos; 

XAML

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,5,12,-10"> 
        <ProgressBar x:Name="progressBar" HorizontalAlignment="Left" Height="40" Margin="0,0,0,0" VerticalAlignment="Top" Width="436" Visibility="Collapsed" IsIndeterminate="True"/> 
        <ListBox x:Name="PhotoList"  
                 toolkit:TiltEffect.IsTiltEnabled="True" 
                 SelectionChanged="PhotoList_SelectionChange" 
                 HorizontalAlignment="Left" Height="500" Margin="0,40,0,0" VerticalAlignment="Top" Width="450"> 
            <ListBox.ItemsPanel> 
                <ItemsPanelTemplate> 
                    <toolkit:WrapPanel  
                        HorizontalAlignment="Left"  
                        Margin="0,0,0,0"  
                        VerticalAlignment="Top"  
                    /> 
                </ItemsPanelTemplate> 
            </ListBox.ItemsPanel> 
            <ListBox.ItemTemplate> 
                <DataTemplate> 
                    <StackPanel Margin="5"> 
                        <StackPanel Orientation="Vertical"> 
                            **<Image delay:LowProfileImageLoader.UriSource="{Binding PhotoSource}" Width="99" Height="80"/>** 
                        </StackPanel> 
                    </StackPanel> 
 
                </DataTemplate> 
            </ListBox.ItemTemplate> 
        </ListBox> 
    </Grid> 

请您参考如下方法:

你需要:

  • 创建一些你想绑定(bind)到你的类。例如:

    private class Photo { 
        public string PhotoSource {get; set;} 
    } 
    
  • 创建一个你想绑定(bind)的集合。例如,List<Photo> Photos = new List<Photo>();

  • 将一些数据添加到您的列表中。例如,Photos.Add(new Photo { PhotoSource = yourBitmap });
  • 将其绑定(bind)到您的列表。 PhotoList.ItemsSource = Photos;