Skip to main content

Microsoft Silverlight

Answered Question Border.Child - System.ArgumentException and System.AccessViolationExceptionRSS Feed

(0)

UncleRedz
UncleRedz

Member

Member

8 points

7 Posts

Border.Child - System.ArgumentException and System.AccessViolationException

Hi,

Using SL2B1 with C# (and IE7), the Border control will throw an exception if the Child property is set to the same object twice.

Example:

UserControl A;
UserControl B;

myBorder.Child = A; //OK
myBorder.Child = B; //OK
myBorder.Child = B; //Exception

An exception of type 'System.ArgumentException' occurred in System.Windows.dll but was not handled in user code
Additional information: Value does not fall within the expected range.

Also along these lines, setting Child to null produces another exception,

myBorder.Child = null;

An exception of type 'System.AccessViolationException' occurred in System.Windows.dll but was not handled in user code
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Cheers

Pranav Goel - MSFT
Pranav G...

Member

Member

26 points

3 Posts

Answered Question

Re: Border.Child - System.ArgumentException and System.AccessViolationException

Thank you for reporting this. We have logged it at our end and are investigating it.

BryX
BryX

Member

Member

4 points

8 Posts

Re: Border.Child - System.ArgumentException and System.AccessViolationException

Is there a workaround yet? or a fix? I have a similar problem adding a new self defined XAML User Control to a Canvas, it gives me:

 An exception of type 'System.ArgumentException' occurred in System.Windows.dll but was not handled in user code

Additional information: Value does not fall within the expected range.

public void timeToXAML(SA11Demo2.Selector.Course course,Canvas destination, int x, int y)

{

    Course newCourse = new Course(course.cName, x, y, course.ccID, 1);

    destination.Children.Add((Course)newObj);

}

Here are the course classes....

//---- Course.cs ------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace SA11Demo2

{

public partial class Course : UserControl

{

public string Id { get; set; }

bool startDrag = false;

public Course(string id, double x, double y, string courseCode, int type) //where type= Lec/Lab

{

InitializeComponent();

// add eventhandlers

CourseRec.MouseEnter += new MouseEventHandler(CourseRec_MouseEnter);

CourseRec.MouseLeave += new MouseEventHandler(CourseRec_MouseLeave);

CourseRec.MouseLeftButtonDown += new MouseButtonEventHandler(CourseRec_MouseLeftButtonDown);

this.MouseLeftButtonUp += new MouseButtonEventHandler(Course_MouseLeftButtonUp);

this.MouseMove += new MouseEventHandler(Course_MouseMove);

 

// give a value to the properties

this.Id = id;

this.ccID.Text = courseCode;

//this.cName.Text = courseName;

this.type.Text = getCourseType(type);

 

// set the top end left position on the parent canvas for the course

this.SetValue(Canvas.LeftProperty, x);this.SetValue(Canvas.TopProperty, y);

}

string getCourseType(int type)

{

if(type.Equals(1)) //for when its a LEC

{

return("LEC");

}

else if (type.Equals(2)) //For when its a LAB

{

return ("LAB");

}

else

{

return ("NULL");

}

}

void Course_MouseMove(object sender, MouseEventArgs e)

{

// only execute this code when the user has started dragging

if (startDrag)

{

// position the center of the course to the cursor of the mouse

this.SetValue(Canvas.LeftProperty, e.GetPosition((Canvas)this.Parent).X - CourseRec.ActualWidth / 2);this.SetValue(Canvas.TopProperty, e.GetPosition((Canvas)this.Parent).Y - CourseRec.ActualHeight / 2);

}

}

void Course_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

{

// stop forcing the capture of the moue to this element

CourseRec.ReleaseMouseCapture();

startDrag =
false;

//TODO: use these property mutators to trigger a 'drop event' which will socket the course into place of the closest allowable position

this.SetValue(Canvas.LeftProperty, e.GetPosition((Canvas)this.Parent).X - CourseRec.ActualWidth / 2);this.SetValue(Canvas.TopProperty, e.GetPosition((Canvas)this.Parent).Y - CourseRec.ActualHeight / 2);

 

 

// double xPos = Convert.ToDouble(this.GetValue(Canvas.LeftProperty));

//double yPos = Convert.ToDouble(this.GetValue(Canvas.TopProperty));

 

Canvas main = this.Parent as Canvas;

CourseRec_Background.Opacity = 1;

}

void CourseRec_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

{

// force the capture of the mouse to this element

CourseRec.CaptureMouse();

startDrag =
true;

CourseRec_Background.Opacity = 0.45;

}

void CourseRec_MouseLeave(object sender, MouseEventArgs e)

{

 

}

void CourseRec_MouseEnter(object sender, MouseEventArgs e)

{

 

}

 

 

 

<UserControl x:Class="SA11Demo2.Course"

xmlns="http://schemas.microsoft.com/client/2007"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

Width="171" Height="122" mc:Ignorable="d" x:Name="CourseControl">

<UserControl.Resources>

<Storyboard x:Name="fadeIn">

<DoubleAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="(UIElement.Opacity)" BeginTime="00:00:00">

<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>

<SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>

</DoubleAnimationUsingKeyFrames>

</Storyboard>

</UserControl.Resources>

 

<Grid x:Name="LayoutRoot" Background="White" Width="171" Height="129">

<Rectangle Cursor="Hand" HorizontalAlignment="Stretch" Margin="-1,-0.500999987125397,0,0.00100000004749745" VerticalAlignment="Stretch" Stroke="#FF000000" x:Name="CourseRec_Background">

<Rectangle.Fill>

<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" SpreadMethod="Repeat">

<GradientStop Color="#FF6C7C8C" Offset="1"/>

<GradientStop Color="#FF2363A3" Offset="0"/>

</LinearGradientBrush>

</Rectangle.Fill>

</Rectangle>

<TextBlock Height="20" HorizontalAlignment="Right" Margin="0,0,8,11" VerticalAlignment="Bottom" Text="&lt;LEC?&gt;" TextWrapping="Wrap" FontSize="22" Foreground="#FFFFFFFF" x:Name="type" FontFamily="Courier New" Width="79.497" d:LayoutOverrides="HorizontalAlignment, Width"/>

<TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Top" FontSize="22" Text="&lt;ccID*1234&gt;" TextWrapping="Wrap" Foreground="#FFFFFFFF" x:Name="ccID" FontFamily="Courier New" Height="23.333"/>

<Button Height="23" HorizontalAlignment="Left" Margin="0,0,0,8" VerticalAlignment="Bottom" Width="25.09" Content="X" FontSize="17" x:Name="ExitButton" Opacity="0.585" d:LayoutOverrides="Width"/>

 

<Rectangle Cursor="Hand" HorizontalAlignment="Right" Margin="0,0,0,0.00100000004749745" VerticalAlignment="Bottom" Stroke="#FF000000" x:Name="CourseRec" Width="172" Height="129.5" Opacity="0">

<Rectangle.Fill>

<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" SpreadMethod="Repeat">

<GradientStop Color="#FF6C7C8C" Offset="1"/>

<GradientStop Color="#FF2363A3" Offset="0"/>

</LinearGradientBrush>

</Rectangle.Fill>

</Rectangle>

</Grid>

</UserControl>

 

 

jenhansen
jenhansen

Member

Member

2 points

1 Posts

Re: Border.Child - System.ArgumentException and System.AccessViolationException

I ran into the same problem.  I was getting the ArgumentException error until I named all the elements I was adding with unique names. (Before they didn't have names)   This definitely seems like a bug, but at least the work around was easy enough.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities