Error with calling event inside another event - silverlight

I am using silverlight, My code is set up for a usercontrol as follows:
myxaml.xaml (Just showing the toggle button [line 119])
<ToggleButton x:Name="btnToggleResizeMap" Checked="btnToggleResizeMap_Checked" Unchecked="btnToggleResizeMap_Unchecked" IsChecked="True"/>
codebehind.cs
public partial class MapRadar : UserControl
{
public delegate void OnMapExpandChange(object sender, EventArgs e);
public event OnMapExpandChange Expanded;
public event OnMapExpandChange NotExpanded;
private void btnToggleResizeMap_Checked(object sender, System.Windows.RoutedEventArgs e)
{
NotExpanded(this, null); //If i remove this line, the app runs fine
}
private void btnToggleResizeMap_Unchecked(object sender, System.Windows.RoutedEventArgs e)
{
Expanded(this, null); //If i remove this line, the app runs fine
}
}
Visual studio throws this error before the application is completely loaded:
AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 119 Position: 285]
at:
System.Windows.Application.LoadComponent(this, new System.Uri("/Xormis.Silverlight.ExSys;component/Views/Map/MapRadar.xaml", System.UriKind.Relative));
which is located inside a function named public void InitializeComponent()
I have no idea what is happening here, is there something against having event calls inside another event?

The problem is that you have null events. As soon as the checkbox is created, it immediately raises the Unchecked event, which calls your btnToggleResizeMap_Unchecked handler, which tries to call your Expanded event. Since Expanded is null, an exception is thrown, and it never finishes running the XAML.
Your code should look like this:
private void btnToggleResizeMap_Checked(object sender, System.Windows.RoutedEventArgs e)
{
if (NotExpanded != null)
NotExpanded(this, null);
}
private void btnToggleResizeMap_Unchecked(object sender, System.Windows.RoutedEventArgs e)
{
if (Expanded != null)
Expanded(this, null);
}
For a more thorough description of events, see C# Events and Thread Safety

Related

Why does WPFMediaKit VideoCaptureElement created by code land ever in MediaFailed?

I add a VideoCaptureElement to a window in runtime but when I run this code it fires MediaFailed. But if I add the same element in XAML then it works fine, I can see the video from the laptop camera.
Am I doing anything wrong? Please help!
public partial class MainWindow : Window
{
WPFMediaKit.DirectShow.Controls.VideoCaptureElement VCE;
public MainWindow()
{
InitializeComponent();
VCE = new WPFMediaKit.DirectShow.Controls.VideoCaptureElement();
Content = VCE;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
VCE.MediaOpened += VCE_MediaOpened;
VCE.MediaFailed += VCE_MediaFailed;
VCE.VideoCaptureDevice = WPFMediaKit.DirectShow.Controls.MultimediaUtil.VideoInputDevices[0]; // This is my laptop webcam
}
void VCE_MediaOpened(Object sender, RoutedEventArgs e) { ... }
void VCE_MediaFailed(object sender, WPFMediaKit.DirectShow.MediaPlayers.MediaFailedEventArgs e) { ... }
}
I had a similar problem with a MediaUriElement working in XAML but not working when instantiated in code-behind.
The solution for me was to Init the control:
VCE.BeginInit();
VCE.EndInit();
This would fit between instantiating (VCE = new...) and assigning (Content = VCE). I haven't tested your particular scenario, but it sounds like the same cause - there must be some extra work done in Init that happens automatically when using XAML.

Dispose ReportViewer object

How to Dispose ReportViewer object safely if application closed unexpectedly
Public Shared rv As New Microsoft.Reporting.WinForms.ReportViewer
I guess you are talking about issue mentioned here.
As mentioned in the link, you need to manually Dispose() the reportViewer instance on form closing event.
private void frmMyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (rv != null && !rv.Disposing && !rv.IsDisposed)
{
rv.Dispose();
}
}
OR
Either calling reportViewer.LocalReport.ReleaseSandboxAppDomain() in form closing event:
private void frmMyForm_FormClosing(object sender, FormClosingEventArgs e)
{
rv.LocalReport.ReleaseSandboxAppDomain();
}

How to use the DependencyProperties of a Blend Behavior during its initialization?

Here I have a simple Blend Behavior. It has a single DependencyProperty which I want to do something with during initialization in OnAttached. But I can't because the behavior appears to be attached before it is even initialized!
Usage:
<Button>
<e:Interaction.Behaviors>
<local:TestBehavior Test1="{Binding ValueOnViewModel}"/>
</e:Interaction.Behaviors>
<Button>
Definition:
class TestBehavior : Behavior<FrameworkObject>
{
public static readonly DependencyProperty Test1Property
= DependencyProperty.Register("Test1", typeof(int), typeof(TestBehavior),
new PropertyMetadata(OnTest1Property_Changed));
private static void OnTest1Property_Changed(DependencyObject sender,
DependencyPropertyChangedEventArgs args)
{
// This gets called _after_ OnAttached!
}
public int Test1
{
get { return (int)GetValue(Test1Property); }
set { SetValue(Test1Property, value); }
}
protected override void OnAttached()
{
base.OnAttached();
// No matter what Test1Property is bound to in XAML 'a' will be default
// value because as this point the Behavior is attached, but not
// initialized!
int a = Test1;
}
}
This strikes me as really bizarre. In this simple case I can get around the issue by performing my initialization in OnTest1Property_Changed instead of in OnAttached, (albeit with the mild inconvenience of being in a static context rather than an instance context).
However, what if I have a rather less trivial Behavior which has multiple properties? In some usages of the behavior all the DP's might be explicitly set, whereas in other cases only some of the DP's might be set while using the default values of the DP's that are not explicitly set. I can handle the change event for all of the DP's the behavior defines, but I have no way of knowing which DP's the client has set explicitly in XAML, so I have no way of knowing with change notifications have to have occurred before initialization is complete.
So how in that non-trivial case can I possibly know when initialization of the behavior is complete? This seems like such a glaring weakness that I can only assume I've missed something obvious.
[Update]
Using Sorskoot's suggestion I knocked up this simple base class which I can use as the base for my Behaviors instead.
public class BehaviorBase<T> : Behavior<T> where T : FrameworkElement
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += AssociatedObject_Loaded;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Loaded -= AssociatedObject_Loaded;
}
protected virtual void OnLoaded()
{
}
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
OnLoaded();
}
}
I would suggest attaching to the Loaded event during in the OnAttached method and do you initialization in there.
protected override void OnAttached()
{
base.OnAttached();
base.AssociatedObject.Loaded += onloaded;
}
private void onloaded(object sender, RoutedEventArgs e)
{
int a = Test1;
// Test1 should contain a value here.
}

Close current UserControl

I have a Window1.xaml main Window; and after some event, I display a UserControl EditFile.xaml.
The code behind is:
public static int whichSelected = -1;
private void button1_Click(object sender, RoutedEventArgs e)
{
//searchEditPanel.Children.Clear();
whichSelected = listViewFiles.SelectedIndex;
searchEditPanel.Children.Add(_EditFileControle); //this is Grid
}
And now, how can I close the opened/added UserControl from its content by clicking a Cancel button or something like that?
Window.GetWindow(this).Close();
You don't need to use a new variable, you can use it directly.
In your button click handler try :
Window parentWindow = (Window)this.Parent;
parentWindow.Close();
You could set the Visibility property of the control you want to "close" to Collapsed.
This way it will not be displayed anymore but will still be present in the visual tree if you need to reuse it later.
Have you tried this?
searchEditPanel.Children.Remove(_EditFileControle);
Another Suggestion:
Maybe this helps: http://sachabarber.net/?p=162
if it doesn't: Add a property to your UserControl:
public UserControl ParentControl {get;set;}
Now modify your code:
private void button1_Click(object sender, RoutedEventArgs e)
{
//searchEditPanel.Children.Clear();
whichSelected = listViewFiles.SelectedIndex;
_EditFileControle.ParentControl = this;
searchEditPanel.Children.Add(_EditFileControle); //this is Grid
}
Now you should be able to do this:
// Somewhere in your UserControl
if (this.ParentControl != null)
this.ParentControl.Children.Remove(this);
private void Button_Click(object sender, RoutedEventArgs e)
{
(this.Parent as searchEditPanel).Children.Remove(this);
}

Winforms StatusStrip - why are there periods where it is blank when I'm updating it?

BACKGROUND: I have a WindowForms v3.5 application with a StatusStrip set to be used as a TooStripStatusLabel. I'm issues quite a lot of updates to it during a task that is running, however there are noticable periods where it is BLANK. There are no points when I am writing a blank to the status strip label either.
QUESTION: Any ideas why I would be seeing period where the status strip label is blank, when I don't expect it to be?
How I update it:
private void UpdateStatusStrip(string text)
{
toolStripStatusLabel1.Text = text;
toolStripStatusLabel1.Invalidate();
this.Update();
}
PS. Calling Application.DoEvents() after the this.Update() does not seem to help. I actually am calling this via the backgroundworker control, so:
(a) I start up the background worker:
private void Sync_Button_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
DisableUpdateButtons();
}
(b) the background worker calls updates:
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(1, "Example string");
MainForm.MyC.SyncFiles(sender);
}
(c) The MyC business class uses it too, e.g.
public void SyncFiles(object sender)
{
BackgroundWorker bgw = (System.ComponentModel.BackgroundWorker) sender;
bgw.ReportProgress(1, "Starting sync...");
.
.
.
}
(d) This event picks it up:
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
UpdateStatusStrip((string)e.UserState);
}
(e) And again the update status strip
private void UpdateStatusStrip(string text)
{
toolStripStatusLabel1.Text = text;
toolStripStatusLabel1.Invalidate();
this.Update();
}
Does this help?
The reason is possibly in the caller of this function. If you call it from another thread, use Control.BeginInvoke instead of direct call. If you call it from the main application thread during long processing, try Application.DoEvents after UpdateStatusStrip call.

Resources