2011-01-16 74 views
30

Tôi gặp sự cố với hộp danh sách cập nhật từ lớp mô hình xem. Tôi sử dụng khung Caliburn Micro. Kịch bản của tôi là ở đây:XAML liên kết thuộc tính BitmapImage ViewModel

tôi ràng buộc tài sản của loại bindableCollection trên listbox:

Mã khỏi tầm nhìn mô hình:

private BindableCollection<UserInfo> _friends; 

public BindableCollection<UserInfo> Friends 
{ 
    get { return _friends; } 
    set 
    { 
     _friends= value; 
     NotifyOfPropertyChange(()=>Friends); 
    } 
} 

Theo quan điểm của mô hình tôi tạo phương pháp dịch vụ giả Mà trả lại dữ liệu tươi mới như Danh sách và với dữ liệu này tôi cập nhật tài sản Bạn bè được ràng buộc trên hộp danh sách.

Tôi gọi phương thức dịch vụ giả trong sự kiện đánh dấu hẹn giờ mỗi 3 giây.

private static UserInfo FakeUser() 
     { 
      var user = new UserInfo 
      { 
       Age = "16", 
       Emphasis = true, 
       IdUser = "11542", 
       IsBlocked = false, 
       IsFriend = true, 
       LocationInfo = new Location 
       { 
        CityName = "TN", 
        IdCity = 123456, 
        IdRegion = 1246, 
        RegionName = "TN", 
       }, 
       StatusInfo = new Status 
       { 
        IdChat = 12, 
        IsLogged = true, 
        LastLogin = "153151", 
        IsChating = true, 
        RoomName = "Car", 
       }, 
       ProjectStatusInfo = new ProjectStatus(), 
       IsIamFriend = true, 
       PlusInfo = new Plus(), 
       ProfilePhoto = new BitmapImage(new Uri("http://pokec.azet.sk/vanes90?i9=1f104a294997", UriKind.RelativeOrAbsolute)) 

      }; 

      return user; 
     } 

     private static IEnumerable<UserInfo> GetFakeFriends() 
     { 
      var list = new List<UserInfo>(); 

      for (int i = 0; i < 20; i++) 
      { 
       list.Add(FakeUser()); 
      } 

      return list; 
     } 

     private void DispatcherTimer_Tick(object sender, EventArgs eventArgs) 
     { 
      if (_isExecuting) 
       return; 
      _isExecuting = true; 
      new System.Threading.Tasks.Task(() => 
      { 
       var freshFriends = GetFakeFriends(); 

       Execute.OnUIThread((System.Action)(() => 
       { 
        Friends.Clear(); 
        foreach (var freshFriend in freshFriends) 
        { 
         Friends.Add(freshFriend); 

        } 
       })); 
      }).Start(); 

      _isExecuting = false; 
     } 

    } 

Nếu tôi không áp dụng bất kỳ kiểu nào trên hộp danh sách, nó hoạt động tốt.

Xem:

<Grid> 
    <ListBox Name="Friends" 
      Grid.Row="2" 
      Margin="4,4,4,4"> 
    </ListBox> 
</Grid> 

Nếu tôi áp dụng một số phong cách mà tôi gắn ProfilePhoto tài sản (typeof BitmapeImage) từ UserInfo trên listbox.

Style là đây:

 <Style x:Key="friendsListStyle" TargetType="{x:Type ListBox}"> 
      <Setter Property="ItemTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
         <Grid Name="RootLayout"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="0.3*"></ColumnDefinition> 
           <ColumnDefinition Width="*"></ColumnDefinition> 
          </Grid.ColumnDefinitions> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="60"></RowDefinition> 
          </Grid.RowDefinitions> 
          <Image Margin="4,4,4,2" Source="{Binding Path=ProfilePhoto}" Grid.Column="0"/> 
         </Grid> 
        </DataTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

tôi nhận được lỗi này:

Must create DependencySource on same Thread as the DependencyObject. 

    at System.Windows.Markup.XamlReader.RewrapException(Exception e, Uri baseUri) 
    at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter) 
    at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter) 
    at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField) 
    at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren) 
    at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate) 
    at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container) 
    at System.Windows.FrameworkElement.ApplyTemplate() 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at System.Windows.Controls.Border.MeasureOverride(Size constraint) 

Nếu tôi thực hiện một phong cách trên mục listbox/hộp danh sách, trong Mà tôi chỉ bắt buộc chuỗi hoặc bool tính nó hoạt động tốt .

Tôi chỉ gặp sự cố nếu liên kết thuộc tính bitmapImage.

tài sản BitmapImage được init như:

ProfilePhoto = new BitmapImage(new Uri("http://pokec.azet.sk/vanes90?i9=1f104a294997", UriKind.RelativeOrAbsolute)) 

URI là url của hình ảnh hoặc đường dẫn đến tập tin.

Điều gì là sai? Cảm ơn sự giúp đỡ và tư vấn.

Kiểu là tốt, nó chỉ hoạt động nếu tôi không làm mới dữ liệu bằng lời gọi phương thức trong chuỗi khác.

Trả lời

71

Nếu bạn đang tạo BitmapImage trên bất kỳ chủ đề nào khác ngoài chuỗi giao diện người dùng, điều đó sẽ giải thích vấn đề này. Bạn có thể đóng băng số BitmapImage để đảm bảo có thể truy cập vào bất kỳ chủ đề nào:

+0

Ông Boogaart cảm ơn sự giúp đỡ, vấn đề được giải quyết. –

+5

Tuyệt vời! Làm thế nào bạn tìm thấy điều này? –

+0

Vì 'BitmapImage' kế thừa từ' System.Windows.Freezable' - "Một Freezable đông lạnh cũng có thể được chia sẻ qua các luồng, trong khi Freezable không bị đóng băng thì không thể." - http://msdn.microsoft.com/en-us/library/ms750509(v=vs.110).aspx – codekaizen

Các vấn đề liên quan