static void DeviceProperty_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e) { // Get the sender CapPlayer typedSender = sender as CapPlayer; if (typedSender != null) { // Make sure that we are not in design mode if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(typedSender)) { return; } // Unsubscribe from previous device CapDevice oldDevice = e.OldValue as CapDevice; if (oldDevice != null) { // Clean up typedSender.CleanUpDevice(oldDevice); } // Subscribe to new one CapDevice newDevice = e.NewValue as CapDevice; if (newDevice != null) { // Subscribe newDevice.NewBitmapReady += typedSender.device_OnNewBitmapReady; } } }
/// <summary> /// Cleans up a specific device /// </summary> /// <param name="device">Device to clean up</param> void CleanUpDevice(CapDevice device) { // Check if there even is a device if (device == null) { return; } // Stop device.Stop(); // Unsubscribe device.NewBitmapReady -= device_OnNewBitmapReady; }
/// <summary> /// Invoked when a new bitmap is ready /// </summary> /// <param name="sender">Sender</param> /// <param name="e">EventArgs</param> void device_OnNewBitmapReady(object sender, EventArgs e) { // Create new binding for the framerate Binding b = new Binding(); b.Source = Device; b.Path = new PropertyPath(CapDevice.FramerateProperty); SetBinding(CapPlayer.FramerateProperty, b); // Get the sender CapDevice typedSender = sender as CapDevice; if (typedSender != null) { // Set the source of the image Source = typedSender.BitmapSource; } }
/// <summary> /// Invoked when the MonikerString dependency property has changed /// </summary> /// <param name="sender">Sender</param> /// <param name="e">EventArgs</param> private static void MonikerString_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e) { // Get typed sender CapDevice typedSender = sender as CapDevice; if (typedSender != null) { // Always stop the device typedSender.Stop(); // Get the new value string newMonikerString = e.NewValue as string; // Check if we have a valid moniker string if (!string.IsNullOrEmpty(newMonikerString)) { // Initialize device typedSender.InitializeDeviceForMoniker(newMonikerString); // Start typedSender.Start(); } } }
/// <summary> /// Cleans up a specific device /// </summary> /// <param name="device">Device to clean up</param> void CleanUpDevice(CapDevice device) { // Check if there even is a device if (device == null) return; // Stop device.Stop(); // Unsubscribe device.NewBitmapReady -= device_OnNewBitmapReady; }
private void OnSelectedWebcamChanged() { if (SelectedWebcam == null) { SelectedWebcam = new CapDevice(""); } SelectedWebcamPlayer.MonikerString = newMonikerString; }