Windows Phone 7, Conditional Rectangle fIll for the new ImageSource?

So, I have a seemingly simple question:

How do I switch to the encoding "if rectangle.fill is X, change the fill to this particular image" in C #? Here is my rectangle in XAML:

<Rectangle Height="auto" Width="auto" x:Name="Honey" ManipulationStarted="Honey_Started"> <Rectangle.Fill> <ImageBrush ImageSource="image100.jpg" /> </Rectangle.Fill> </Rectangle> 

It was simple, but what would the code look like? The following example is obviously wrong, but I just turn it on in the hope of showing what I want to accomplish.

 private void Honey_Started(object sender, ManipulationStartedEventArgs e) { if (Honey.Fill == image100.jpg) { Honey.Fill = 900image.jpg; } } 
+4
source share
1 answer

Instead of checking the Fill file name, you can change the Rectangle Tag property (or create your own UserControl ). Then, for example, you can do this:

 <Rectangle Height="auto" Width="auto" x:Name="Honey" ManipulationStarted="Honey_Started" Tag="image1"> <Rectangle.Fill> <ImageBrush ImageSource="image100.jpg" /> </Rectangle.Fill> </Rectangle> 

I added Tag="image1" as an attribute in the Rectangle . Then you can access this in code and check.

 private void Honey_Started(object sender, ManipulationStartedEventArgs e) { if (Honey.Tag.ToString() == "image1") { ImageBrush ib = new ImageBrush(); BitmapImage bImage = new BitmapImage(new Uri("900image.jpg",UriKind.Relative)); ib.ImageSource = bImage; Honey.Tag = "900image.jpg"; Honey.Fill = ib; } } 

There are other methods. All I did in the Honey_Started event was to create a new ImageBrush code, assign it an image ( 900image.jpg ), and then set ImageBrush to Fill for the rectangle.

An alternative method could be to store two ImageBrushes as a class level variable and use them to compare values.

 BitmapImage biImage100 = new BitmapImage(new Uri("/Jar/image100.jpg",UriKind.Relative)); ImageBrush ibImage100 = new ImageBrush() { ImageSource = biImage100 }; ImageBrush ib900image = new ImageBrush() { /*assign the 900image.jpg image*/ }; private void Honey_Started(object sender, ManipulationStartedEventArgs e) { if (Honey.Fill == ibImage100) { Honey.Fill = ib900image; } } 
+3
source

All Articles