简体   繁体   中英

Having trouble with URI Image in Visual Studio 2012?

I am currently creating a C# XAML Windows Store App I have successfully gotten the app to do what i want it to do which is when I click on a listviewitem the image to the right changes, however to do this for each listview item i am having to create a bitmap image for every listviewitem.

I find it very time consuming would anybody be able to recommend a type of method i could create to save me from writing this in every listviewitem Selection_changed.

BitmapImage PlatoImage = new BitmapImage(new Uri(this.BaseUri,"example.jpg"));
PhilosopherImage.Source = PlatoImage;

Below is the original method

private void PhilosopherList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (PhilosopherList.SelectedIndex ==1)
            {
                BitmapImage Aristotle = new BitmapImage(new Uri(this.BaseUri, "aristotle.png"));
                PhilosopherImage.Source = Aristotle;

All i have done for the rest is insert 'else if' statements to compensate for the other 'selectedindex' I haven't created a method yet to save me time to create a new bitmap image for each listviewitem yet

It sounds like your listview items are strings.

Without using data binding, you can change the ListView items to objects that contain the string you want to display and the image url, then add a tostring() method that returns the string you want to display. Then your SelectionChanged handler can simply grab the image url from the selected item.

The listview item type would be something like this:

public class ItemType
{
   public string DisplayString{get;set;}
   public string ImageUrl{get;set;}
   public override string ToString()
   {
      return DisplayString;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM