2013-01-22 40 views
1

我需要在WP8中插入圖片。我有一堆圖像。一旦我點擊圖像,它必須設置爲網格的背景。所以我創建了一個空的「grid1」和按鈕。在按鈕點擊事件中編寫了下面的代碼,但圖像沒有顯示!如何使用C#爲WP8在網格中顯示圖像?

private void bg6_Click(object sender, RoutedEventArgs e) 
    { 
     System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush(); 
     Image image = new Image(); 
     image.Source = new System.Windows.Media.Imaging.BitmapImage(
      new Uri("\\PhoneApp2\\PhoneApp2\\Assets\\bg\\bg5.jpg")); 
     myBrush.ImageSource = image.Source; 
     // Grid grid1 = new Grid(); 
     grid1.Background = myBrush;   
    } 
+0

您的grid1位於頁面上嗎?你確定你的圖片加載正確嗎? – Haspemulator

+0

@Haspemulator我在畫布中添加了我的grid1。 ...

回答

2

首先,您不需要使用圖像來填充來自URI的背景。

private void bg6_Click(object sender, RoutedEventArgs e) 
{ 
    System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush(new Uri("\\PhoneApp2\\PhoneApp2\\Assets\\bg\\bg5.jpg")); 
    // Grid grid1 = new Grid(); 
    grid1.Background = myBrush;   
} 

第二,它是一個更好的方式通過創建具有知名度和源屬性的輔助類來設計它的XAML和操縱它從代碼的可見性和來源。不要忘記將INotifyPropertyChanged接口實現到該類中。

<Grid x:Name="myGrid" DataContext="{Binding}" Visibility="{Binding Path=VisibleProperty}"> 
<Grid.Background> 
<ImageBrush x:Name="myBrush" ImageSource="{Binding Path=SourceProperty}"></ImageBrush> 
</Grid.Background> 

而在代碼:

private void bg6_Click(object sender, RoutedEventArgs e) 
{ 
    myGrid.DataContext=new myImagePresenterClass(new Uri("\\PhoneApp2\\PhoneApp2\\Assets\\bg\\bg5.jpg"), Visibility.Visible) 
} 

public class myImagePresenterClass:INotifyPropertyChanged 
{ 
private URI sourceProperty 
Public URI SourceProperty 
{ 
get 
{ 
    return sourceProperty; 
} 
set 
{ 
    sourceProperty=value; 
    if(PropertyChanged!=null){PropertyChanged(this, new PropertyChangedEventArgs("SourceProperty"));} 
} 
} 

//Don't forget to implement the Visible property the same way as SourceProperty and the class constructor. 
} 
3

很難知道您的圖像文件是否在正確的位置並設置爲正確的構建類型。我建議添加一個事件處理程序到Image failed event

private void bg6_Click(object sender, RoutedEventArgs e) 
{ 
    System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush(); 
    Image image = new Image(); 
    image.ImageFailed += (s, e) => MessageBox.Show("Failed to load: " + e.ErrorException.Message); 
    image.Source = new System.Windows.Media.Imaging.BitmapImage(
    new Uri("\\PhoneApp2\\PhoneApp2\\Assets\\bg\\bg5.jpg")); 
    myBrush.ImageSource = image.Source; 
    // Grid grid1 = new Grid(); 
    grid1.Background = myBrush;   
} 
+0

我將我的圖像放在正確的位置。但沒用。我在處理異常方面非常忙碌。所以你可以告訴我清楚如何處理ImageFailed異常。它顯示了這樣的消息「在System.ni.dll中發生類型'System.UriFormatException'的異常,但未在用戶代碼 –

+0

'code' private void bg6_Click(object sender,RoutedEventArgs e) {System.Windows。 Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush(); Image image = new Image(); image.ImageFailed + =(s,i)=> MessageBox.Show(「Failed to load:」+ i .ErrorException.Message); image.Source = new System.Windows.Media.Imaging.BitmapImage( new Uri( 「\\ Assets \\ bg \\ bg5.jpg」)); myBrush.ImageSource = image。來源; // Grid grid1 = new Grid(); grid1.Background = myBrush; } –

+0

該消息告訴你,你的Uri無效。由於您沒有向我們提供有關文件位置的信息,因此很難知道您的確切Uri應該是什麼。我建議將它改爲親戚,就像這個新的Uri(「[Your Path」],UriKind.Relative)。 – Bryant

0

我發現這個錯誤......我對不起你們。我沒有按照正確的語法。我錯過了Uri方法中的'@'。代表此的正確方法是

private void bg1_Click(object sender, RoutedEventArgs e) 
    { 
     System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush(); 
     Image image = new Image(); 
     image.ImageFailed += (s, i) => MessageBox.Show("Failed to load: " + i.ErrorException.Message); 
     image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(@"/Assets/bg/bg1.jpg/", UriKind.RelativeOrAbsolute)); 
     myBrush.ImageSource = image.Source; 
     grid1.Background = myBrush; 

    } 
相關問題