2013-06-24 47 views
1

我正在開發我的第一個Windows Store應用程序,並且我遇到問題。 我試圖從我的電腦打開一張圖像,並將其加載到圖像控件中,該圖像控件位於FlipView內的DataTemplate cotrol中。我FlipView看起來是這樣的:如何訪問FlipView中的XAML DataTemplate中的圖像控件

<FlipView 
     x:Name="flipView" 
     AutomationProperties.AutomationId="ItemsFlipView" 
     AutomationProperties.Name="Item Details" 
     TabIndex="1" 
     Grid.RowSpan="2" 
     ItemsSource="{Binding Source={StaticResource itemsViewSource}}" SelectionChanged="flipView_SelectionChanged" Margin="10,-58,334,126" Grid.ColumnSpan="2"> 

     <FlipView.ItemTemplate> 
      <DataTemplate> 
       <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates"> 
         <common:RichTextColumns x:Name="richTextColumns" Margin="80,0,60,30"> 
          <RichTextBlock x:Name="richTextBlock" Width="560" Style="{StaticResource ItemRichTextStyle}" IsTextSelectionEnabled="False"> 
           <Paragraph> 
            <Run FontSize="20" FontWeight="Light" Text="{Binding Title}"/> 
            <LineBreak/> 
            <LineBreak/> 
            <Run FontWeight="Normal" Text="{Binding Subtitle}"/> 
           </Paragraph> 
           <Paragraph LineStackingStrategy="MaxHeight"> 
            <InlineUIContainer> 
             <Image x:Name="image" MaxHeight="480" Margin="0,20,0,-109" Stretch="Uniform" Source="{Binding Image}" AutomationProperties.Name="{Binding Title}"/> 
            </InlineUIContainer> 
           </Paragraph> 

           <Paragraph> 
            <Run FontWeight="SemiLight" Text="{Binding Content}"/> 
           </Paragraph> 
          </RichTextBlock> 

          <common:RichTextColumns.ColumnTemplate> 
           <DataTemplate> 
            <RichTextBlockOverflow Width="560" Margin="80,0,0,0"> 
             <RichTextBlockOverflow.RenderTransform> 
              <TranslateTransform X="-1" Y="4"/> 
             </RichTextBlockOverflow.RenderTransform> 
            </RichTextBlockOverflow> 
           </DataTemplate> 
          </common:RichTextColumns.ColumnTemplate> 
         </common:RichTextColumns> 
       </UserControl> 
      </DataTemplate> 
     </FlipView.ItemTemplate> 
    </FlipView> 

在尋找一個解決這個問題,我發現了一些後一種方法,我結合它在功能打開圖像:

private async void Open_Click(object sender, RoutedEventArgs e) 
{ 
    FileOpenPicker openPicker = new FileOpenPicker(); 
    openPicker.ViewMode = PickerViewMode.Thumbnail; 
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
    openPicker.FileTypeFilter.Add(".jpg"); 
    openPicker.FileTypeFilter.Add(".jpeg"); 
    openPicker.FileTypeFilter.Add(".png"); 
    file = await openPicker.PickSingleFileAsync(); 

    if (file != null) 
    { 
     var Stream = await file.OpenAsync(FileAccessMode.Read); 
     BitmapImage bi = new BitmapImage(); 
     bi.SetSource(Stream); 

     if (flipView.SelectedItem == null) 
     { 
      return; 
     } 

     var _Container = flipView.ItemContainerGenerator.ContainerFromItem(flipView.SelectedItem); 
     var _Children = AllChildren(_Container); 
     var imageControl = _Children.OfType<Image>().First(c => c.Name.Equals("image")); 
      imageControl.Source = bi; 
    } 
    else 
    { 
     var messageDialog = new MessageDialog("Error!"); 
     await messageDialog.ShowAsync(); 
    } 

AllChildren功能:

public List<Control> AllChildren(DependencyObject parent) 
{ 
    var _List=new List<Control>{}; 

    for(int i=0; i<VisualTreeHelper.GetChildrenCount(parent); i++) 
    { 
     var _Child=VisualTreeHelper.GetChild(parent, i); 

     if(_Child is Control) 
     { 
      _List.Add(_Child as Control); 
     } 

     _List.AddRange(AllChildren(_Child)); 
    } 

    return _List; 
} 

當我選擇圖片打開,我得到一個Exception

An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code.

任何人都可以幫我嗎?

回答

0

看起來真的很複雜...你只是想在flipview中查看圖像(並用FileOpenPicker控件添加新圖像)?

我會將我的FlipView綁定到圖像集合(或包含圖像屬性的viewmodel),並將從FileOpenPicker獲得的每個新圖像添加到此集合中。

也許這樣大家有點幫助: http://code.msdn.microsoft.com/windowsapps/XAML-FlipView-control-0ae45312 http://code.msdn.microsoft.com/windowsapps/File-picker-sample-9f294cba

也有看看微軟樣本資源: http://code.msdn.microsoft.com/windowsapps/

相關問題