【问题标题】:Image NOT displaying in Windows Phone 8图像未在 Windows Phone 8 中显示
【发布时间】:2014-02-04 06:13:43
【问题描述】:

我在显示来自 ApplicationData.Current.LocalFolder 的图像时遇到问题。 我能够保存图像并将其存储在LocalFolder 中,但将其显示在Image 控件或LongListSelector 中时,图像不会显示。

代码如下:

private async void StoreToFile(string imageFileName)
{
   StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(imageFileName, CreationCollisionOption.GenerateUniqueName);
   using (Stream current = await file.OpenStreamForWriteAsync())
   {
      await photoStream.CopyToAsync(current);
   }
}

这里是绑定的代码:

var folder = ApplicationData.Current.LocalFolder;
var images = await folder.GetFilesAsync();
Recent.ItemsSource = images.ToList();

XAML 代码:

<phone:PivotItem Name="pivot1" Header="item2" Background="White">
   <phone:LongListSelector x:Name="Recent" Margin="0,0,0,72" ItemsSource="{Binding lst}" >
      <phone:LongListSelector.ItemTemplate>
         <DataTemplate>
            <StackPanel>
               <Image  Source="{Binding Path}" Width="60"/>
            </StackPanel>
         </DataTemplate>
      </phone:LongListSelector.ItemTemplate>
   </phone:LongListSelector>
</phone:PivotItem>

我能够将路径绑定到 TextBlock 并能够看到确切的路径,例如:

C:\Data\Users\DefApps\AppData{GUID}\Local\bucket.png

但如果我绑定到图像源,图像不会显示。

谁能看出我做错了什么?

【问题讨论】:

  • 您无法绑定到此类路径,您必须自己从流中加载图像并创建位图以绑定到Image.Source
  • 感谢您提供一个小的工作示例

标签: c# xaml windows-phone-8


【解决方案1】:

您需要使用 msappx uri 方案:

new Uri("ms-appx:///myimage.jpg");

【讨论】:

    【解决方案2】:

    正如 ToniPetrina 在评论中所说 - 你不能绑定到 Path,你的属性应该返回 BitmapImage。我想到了两种方法:

    • 为您的属性提供一个特殊的 getter,它将返回 BitmapImage(下面的一些代码)
    • 在绑定到独立存储映像时提供转换器 - here is very nice example

    第一个解决方案的示例代码如下所示:
    在 Xaml 中:

    <ListBox Name="myList" Grid.Row="2">
        <ListBox.ItemTemplate>
           <DataTemplate>
                 <Image Source="{Binding GetImgSource}" Width="60"/>
           </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    在后面的代码中:

    public class Images : INotifyPropertyChanged
    {
       public event PropertyChangedEventHandler PropertyChanged;
       private string imgSource = "";
       public string SetImgSource // setter - string
       {
          set
          {
            imgSource = value;
            RaiseProperty("GetImgSource");
          }
       }
       public BitmapImage GetImgSource // getter - BitmapImage
       {
         get
         {
            if (String.IsNullOrEmpty(imgSource)) return null;
            BitmapImage temp = new BitmapImage();
    
            using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
              using (IsolatedStorageFileStream file = ISF.OpenFile(imgSource, FileMode.Open, FileAccess.Read))
                  temp.SetSource(file);
    
            return temp;
         }
      }
    
      public void RaiseProperty(string property = null)
      {
          if (this.PropertyChanged != null)
              this.PropertyChanged(this, new PropertyChangedEventArgs(property));
      }
    }
    
    ObservableCollection<Images> myImg = new ObservableCollection<Images>();
    
    myList.ItemsSource = myImg; // somewhere
    

    【讨论】:

    • 谢谢罗马兹。我将 myImg 的计数设为 0。 ObservableCollection myImg = new ObservableCollection(); myList.ItemsSource = myImg; // 某处
    • 添加了 myImg.Add(new Images() { SetImgSource = ImagePath });仍然没有运气。没有图像显示
    • @user3266804 myList.ItemsSource 例如在页面的构造函数中。您是否尝试过添加 myImg.Add(new Images() {"bucket.png"}); ?你检查过你的图片是否存在?
    • 是的...我全局声明了 ObservableCollection myImg = new ObservableCollection();并在构造函数 myList.ItemsSource = myImg; myImg.Add(new Images() { SetImgSource = Path.Combine(localFolderPath, filename) });我使用 ISETool 检查了图像是否存在。
    • @user3266804 你能用这个本地路径检查它吗?等等。只需 myImg.Add(new Images() { SetImgSource = "bucket.png"}); - 它指的是您的独立存储,所以它是相对的。正如我检查过的那样,它应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多