从本地文件夹获取图像文件以在运行时在UI中显示失败

时间:2014-03-18 07:09:54

标签: mvvm winrt-xaml

我使用以下代码从LocalFolder

中检索图像文件

LocalFolder中的总图像文件数为20

问题:

  1. 仅显示8张图像,其余图像为空白 为什么其余部分无法显示?
  2. 可以将BitmapImage文件绑定到基于MVVM的图像控件上吗?

    示例:imageURL =“ms-appdata:///local/imgfile.jpg”

    ---- In XAML : PhotoView.xaml
    
    <Image x:Name="Img" Source="{Binding ImageUrl}"  Stretch="UniformToFill"/>
    <TextBlock FontSize="23" Text="{Binding Unit_Price}"  Height="23" Margin="3,1,3,0"/>  
    <TextBlock FontSize="23" Text="{Binding Description}" Height="23" Width="300" Margin="1,1,1,0/>
    
    --- In code behind: PhotoView
    
     ItemsViewModel itemsViewModel = null;
     ObservableCollection items = null;
    
    
    itemsViewModel = new ItemsViewModel();
    items = itemsViewModel.GetItems();
    
    //-- GridViewControl
    ItemsViewSource.Source = items;
    ItemsGridView.SelectedItem = null; 
    
    
    -------------MVVM 
    --------- Model : 
    
    class ItemViewModel : ViewModelBase
    {
      private string imageurl = string.Empty;
            public string ImageUrl
            {
                get
                { return imageurl; }
    
                set
                {
                    if (imageurl == value)
                    { return; }
    
                    imageurl = value;
    
                    isDirty = true;
                    RaisePropertyChanged("ImageUrl");
                }
            }
    
         private decimal unit_price = 0;
            public decimal Unit_Price
            {
                get
                { return unit_price; }
    
                set
                {
                    if (unit_price == value)
                    { return; }
    
                    unit_price = value;
                    isDirty = true;
                    RaisePropertyChanged("Unit_Price");
                }
            }
    
    }
    
    ---------- View Model 
    
     class ItemsViewModel : ViewModelBase
        {
            private ObservableCollection items;
    
            public ObservableCollection Items
            {
                get
                {
                    return items;
                }
    
                set
                {
                    items = value;
                    RaisePropertyChanged("Items");
                }
            }
    
    
      public ObservableCollection GetItems()
      {
         items = new ObservableCollection();
    
         using (var db = new SQLite.SQLiteConnection(App.DBPath))
         {
    
           var query = db.Table().Where(c=> c.CompanyName == Company).OrderBy(c => c.No);
    
           foreach (var _item in query)
           {
             var item = new ItemViewModel()
             {
                 No = _item.No,                   
                 ImageUrl = "ms-appdata:///local/" + _item.PictureFilename,
                 Unit_Price = _item.Unit_Price,
                  Description = _item.Description
    
               };
    
                  items.Add(item);
             }
          }
        return items;
      }
    
    

2 个答案:

答案 0 :(得分:0)

尝试绑定ImageSource(不是字符串)

答案 1 :(得分:0)

尝试复制文件夹中的Image文件夹,无论您在何处使用,或者在Register.resx中注册Every images,并按照Register中的名称调用图像