2014-03-18 61 views
0

我目前正在做一個應用程序,我需要將選定寵物的詳細信息傳遞到新的「配置文件」頁面。就目前而言,我已經獲得了所有需要傳遞的數據(姓名,年齡等),以便進入我的新配置文件頁面,但是現在我遇到了問題,因爲我嘗試通過所選寵物的照片來通過它崩潰應用程序。在C中傳遞圖像#

首先這是我宣佈我的泛型列表,在App.xaml中

private void Application_Launching(object sender, LaunchingEventArgs e) 
    { 
      myshop.Add(new Shop{Name = "Johnny", Age= 2, Breed="Husky", Type= "Dog", Stock = 1, Price = 125, Photo = "/Assignment 1;component/Images/Husky.jpg"}); 
      myshop.Add(new Shop{Name = "Billy", Age= 1, Breed="Shiba Inu", Type= "Dog", Stock = 1, Price = 250, Photo = "/Assignment 1;component/Images/Shiba Inu.jpg"}); 
      myshop.Add(new Shop{Name = "Sammy", Age = 8, Breed="Siamese", Type="Cat", Stock = 1, Price = 15, Photo = "/Assignment 1;component/Images/Siamese Cat.jpg"}); 
      myshop.Add(new Shop{Name = "Molly", Age = 6, Breed="Norwegian", Type="Cat", Stock = 1, Price = 30, Photo = "/Assignment 1;component/Images/NorwegianForestCat.jpeg"}); 
      myshop.Add(new Shop{Name = "Nemo", Age = 3, Breed="Clown Fish", Type="Fish", Stock = 1, Price = 10, Photo = "/Assignment 1;component/Images/clown Fish.jpg"}); 
      myshop.Add(new Shop{Name = "Dory", Age = 1, Breed="Palette SurgeonFish", Type="Fish", Price = 75, Stock = 1, Photo = "/Assignment 1;component/Images/Palette Surgeonfish.jpg"}); 
      myshop.Add(new Shop{Name = "Keith", Age = 4, Breed="Bearded Dragon", Type="Lizard", Stock = 1, Price = 750, Photo = "/Assignment 1;component/Images/Bearded Dragon.jpg"}); 
      myshop.Add(new Shop {Name = "Oisin", Age = 12, Breed = "Gecko", Type = "Lizard", Stock = 1, Price = 90, Photo = "/Assignment 1;component/Images/Gecko.jpg" }); 
    } 

這裏就是我試圖通過細節

private void list_SelectionChanged_1(object sender, SelectionChangedEventArgs e) 
    { 
     Shop selectedPet = list.SelectedItem as Shop; 

     String photo = selectedPet.Photo; 
     String breed = selectedPet.Breed; 
     String name = selectedPet.Name; 
     int age = selectedPet.Age; 
     int price = selectedPet.Price; 

     NavigationService.Navigate(new Uri("/Profile.xaml?name=" + name + "&breed=" + breed + "&age=" + age + "&price=" + "€" + price + "&photo=" + photo, UriKind.Relative)); 
    } 

打發那部分代碼工作得很好,並正確地傳遞了細節,但現在我已經添加到照片中通過它正在崩潰。這就是我所說的傳送數據到新的頁面

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     string photo; 
     string name; 
     string age; 
     string breed; 
     string price; 

      if (NavigationContext.QueryString.TryGetValue("photo", out photo)) 
      { 
       imgPhotoHolder.Source = null; 

       BitmapImage myImage = new BitmapImage 
       (new Uri("/Assignment 1;component/Images/" + photo, UriKind.Relative)); 

       imgPhotoHolder.Source = myImage; 
      } 
      if (NavigationContext.QueryString.TryGetValue("name", out name)) 
      { 
       nameTxtBlock.Text = name; 
      } 
      if(NavigationContext.QueryString.TryGetValue("age", out age)) 
      { 
       ageTxtBlock.Text = age; 
      } 
      if(NavigationContext.QueryString.TryGetValue("breed", out breed)) 
      { 
       breedTxtBlock.Text = breed; 
      } 
      if (NavigationContext.QueryString.TryGetValue("price", out price)) 
      { 
       priceTxtBlock.Text = price; 
      } 
    } 

這個問題似乎從這個代碼片段

+ "&photo=" + photo 

我知道這是之所以產生的代碼,因爲當你註釋掉只是這一點的代碼,它會工作得很好,並通過細節,但當取消註釋時,它會導致應用程序崩潰,當你選擇一個商店的項目(見下圖)

所以我只是想知道如果有人有一個想法如何解決這一點的代碼或如何正確得到我法師傳遞到下一頁。 由於提前, 傑森

的地步,你選擇一個圖像:

enter image description here

當你點擊的圖像,並在細節應顯示:

enter image description here

+0

和錯誤是...? –

+0

事實上,當我試圖傳遞一個圖像到個人資料頁面,它只是崩潰的應用程序 – JasonL95

+0

...它崩潰的應用程序與異常? *什麼是錯誤?* –

回答

0

您的照片字符串值的格式如下

「/ Assignment 1; component/Images/SomeImageName.jpg」

並且您將該參數作爲參數發送。

在第二頁上,則取該值出並形成開放的是這樣的:

new Uri("/Assignment 1;component/Images/" + photo... 

此其有效地創建URI:

/分配1;成分/圖像//分配1; component/Images/SomeImageName.jpg

這看起來不對。 你或許應該只需要創建一個開放的基礎上,照片串

BitmapImage myImage = new BitmapImage(new Uri(photo, UriKind.Relative)); 

,並確保您的照片文件的生成操作設置爲資源。

+0

我已採取了建議的步驟,但不幸的是應用程序仍在崩潰 – JasonL95

0

在需要設置寫入路徑爲您的圖像這樣這的OnNavigatedTo方法:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     try 
     { 
     string photo; 
     string name; 
     string age; 
     string breed; 
     string price; 

      if (NavigationContext.QueryString.TryGetValue("photo", out photo)) 
      { 
       imgPhotoHolder.Source = null; 

       BitmapImage myImage = new BitmapImage 
       (new Uri(photo, UriKind.RelativeOrAbsolute)); 

       imgPhotoHolder.Source = myImage; 
      } 
      if (NavigationContext.QueryString.TryGetValue("name", out name)) 
      { 
       nameTxtBlock.Text = name; 
      } 
      if(NavigationContext.QueryString.TryGetValue("age", out age)) 
      { 
       ageTxtBlock.Text = age; 
      } 
      if(NavigationContext.QueryString.TryGetValue("breed", out breed)) 
      { 
       breedTxtBlock.Text = breed; 
      } 
      if (NavigationContext.QueryString.TryGetValue("price", out price)) 
      { 
       priceTxtBlock.Text = price; 
      } 
      }catch(Exception ex) 
      { 

      } 
    } 
+0

我已採取了建議的步驟,但不幸的是應用程序仍在崩潰 – JasonL95

+0

您是否調試了此OnNavigatedTo方法以及您獲得了什麼照片參數,並檢查異常,並使用代碼中的try catch塊查找異常類型 –

+0

使用上面的代碼檢查異常類型 –

1

私人無效btnDetails_Click(對象發件人,RoutedEventArgs E) { 動物selectedAnimals = lstAnimals.SelectedItem作爲動物;

 if (!(lstAnimals.SelectedItem == null)) 
     { 
      NavigationService.Navigate(new Uri("/Details.xaml?" + "name=" + selectedAnimals.Name + "&" 
      + "breed=" + selectedAnimals.Breed + "&" + "gender=" + selectedAnimals.Gender + "&" 
      + "DOB=" + selectedAnimals.DOB + "&" + "price=" + selectedAnimals.Price + "&" 
      + "photo=" + selectedAnimals.Photo, UriKind.RelativeOrAbsolute)); 
     } 

     else 
     { 
      MessageBox.Show("No Pet Selected"); 
     } 

    } 

國際收支平衡表的國際收支平衡表的國際收支平衡表

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     string name, breed, photo, gender, DOB, price; 
     if (NavigationContext.QueryString.TryGetValue("name", out name)) 
      txtName.Text = name; 
     if (NavigationContext.QueryString.TryGetValue("breed", out breed)) 
      txtBreed.Text = breed; 
     if (NavigationContext.QueryString.TryGetValue("gender", out gender)) 
      txtGender.Text = gender; 
     if (NavigationContext.QueryString.TryGetValue("DOB", out DOB)) 
      txtDOB.Text = DOB; 
     if (NavigationContext.QueryString.TryGetValue("price", out price)) 
      txtPrice.Text = price; 
     if (NavigationContext.QueryString.TryGetValue("photo", out photo)) 
     { 
      BitmapImage image = new BitmapImage(new Uri("/PetShop_A2;component/" + photo, UriKind.Relative)); 
      imgDetails.Source = image; 
     } 
    } 

你是設計師