Learn About Microsoft .NET Framework 3.5
By Certification Magazine —
1 | 2 | 3 |
public MediaElement Media
{
get { return mediaElement; }
set { mediaElement = value; }
}
}
C. public class MediaPlayer : System.Windows.Controls.Control
{
private MediaElement mediaElement;
[Bindable(BindableSupport.Yes)]
public MediaElement Media
{
get { return mediaElement; }
set { mediaElement = value; }
}
}
D. public class MediaPlayer : System.Windows.Controls.Control
{
private MediaElement mediaElement;
[Bindable(true)]
public MediaElement Media
{
get { return mediaElement; }
set { mediaElement = value; }
}
}
Answer:
A
Tutorial:
You should use the following code segment:
public class MediaPlayer : System.Windows.Controls.Control
{
public static readonly DependencyProperty MediaProperty = DependencyProperty.Register("Media", typeof(MediaElement), typeof(MediaPlayer));
public MediaElement Media
{
get { return (MediaElement)GetValue(MediaProperty); }
set { SetValue(MediaProperty, value); }
}
}
This code defines the Media property as a dependency property. Only dependency properties support WPF data binding. To create a dependency property, you must call the Register method of the DependencyProperty class and save the returned DependencyProperty instance to a static variable. In the get and set property accessors, you must call the GetValue and SetValue methods, respectively.
The GetValue method accepts a DependencyProperty instance that represents the dependency property whose value should be retrieved. The SetValue method also accepts a DependencyProperty instance, in addition to an Object instance that represents the value to which the property is to be set.
You should not use the following code segment:
public class MediaPlayer : System.Windows.Controls.Control
{
public static readonly DependencyProperty MediaProperty = DependencyProperty.Register("Media", typeof(MediaElement), typeof(MediaPlayer));
private MediaElement mediaElement;
public MediaElement Media
{
get { return mediaElement; }
set { mediaElement = value; }
}
}
Although this code registers a dependency property, it does not call the GetValue and SetValue methods to get and set property values, respectively.
You should not use the following code segment:
public class MediaPlayer : System.Windows.Controls.Control
{
private MediaElement mediaElement;
[Bindable(BindableSupport.Yes)]
public MediaElement Media
{
get { return mediaElement; }
set { mediaElement = value; }
}
}
This code does not create a dependency property. It simply stores the value of the property in a private field. Also, the Bindable attribute does not cause a property to support WPF animation.
You should not use the following code segment:
public class MediaPlayer : System.Windows.Controls.Control
{
private MediaElement mediaElement;
[Bindable(true)]
public MediaElement Media
{
get { return mediaElement; }
set { mediaElement = value; }
}
}
This code does not create a dependency property. It simply stores the value of the property in a private field. Also, the Bindable attribute does not cause a property to support WPF animation.
References:
Control Authoring Overview, MSDN, http://msdn2.microsoft.com/en-us/library/ms745025.aspx
How to: Implement a Dependency Property, MSDN, http://msdn2.microsoft.com/en-us/library/ms750428.aspx
Dependency Properties Overview, MSDN, http://msdn2.microsoft.com/en-us/library/ms752914.aspx
Custom Dependency Properties, MSDN, http://msdn2.microsoft.com/en-us/library/ms753358.aspx
Objective: Add and manage content.
Sub-objective: Manage images.
Single answer, multiple-choice
You are developing a Microsoft WPF application. The following XAML resource is defined in the application definition file:
You need to write code to display a 180-degree rotation of this bitmap in an Image control. Which code segment should you use?
A. BitmapImage bitmap = new BitmapImage(new Uri("pack://application:,,,/ResourceImage"));
bitmap.Rotation = Rotation.Rotate180;
TransformedBitmap transformedBitmap = new TransformedBitmap();
Image image = new Image();
image.Source = transformedBitmap;
B. BitmapImage bitmap = (BitmapImage)Application.Current.Resources["ResourceImage"];
RotateTransform transform = new RotateTransform(180);
TransformedBitmap transformedBitmap = new TransformedBitmap(bitmap, transform);
Image image = new Image();
image.Source = transformedBitmap;
C. BitmapImage bitmap = (BitmapImage)Application.Current.Resources["ResourceImage"];
Transform transform = new Transform(TransformKind.Rotate,180);
TransformedBitmap transformedBitmap = new TransformedBitmap(bitmap, transform);
Image image = new Image();
image.Source = transformedBitmap;
D. BitmapImage bitmap = new BitmapImage(new Uri("pack://application:,,,/ResourceImage"));
RotateTransform transform = new RotateTransform(180);
TransformedBitmap transformedBitmap = new TransformedBitmap(bitmap, transform);
Image image = new Image();
image.Source = bitmap;
Answer:
B
Tutorial:
You should use the following code segment:
BitmapImage bitmap = (BitmapImage)Application.Current.Resources["ResourceImage"];
RotateTransform transform = new RotateTransform(180);
TransformedBitmap transformedBitmap = new TransformedBitmap(bitmap, transform);
Image image = new Image();
image.Source = transformedBitmap;
This code first retrieves the BitmapImage instance from the application-scope resources. It then creates a TransformedBitmap instance that is chained to the original BitmapImage instance. The TransformedBitmap instance uses a RotateTransform instance to rotate the bitmap 180 degrees. The Image control uses the TransformedBitmap instance as its source because that instance represents the rotated bitmap.
You should not use the following code segment:
BitmapImage bitmap = new BitmapImage(new Uri("pack://application:,,,/ResourceImage"));
bitmap.Rotation = Rotation.Rotate180;
TransformedBitmap transformedBitmap = new TransformedBitmap();
Image image = new Image();
image.Source = transformedBitmap;
This code attempts to create a BitmapImage instance by retrieving the image as a binary resource. However, in this scenario, the bitmap is defined as a logical resource in the application definition file. Also, this code sets the Source property of an Image control to a TransformedBitmap instance that is not associated with a source bitmap or a transformation.
1 | 2 | 3 |



