Skip to main content
Home Forums General Silverlight Getting Started Error:InitializeComponent();
5 replies. Latest Post by elmore.adam on July 4, 2009.
(0)
Nofootbird
Member
7 points
22 Posts
07-03-2009 12:54 PM |
Hi,
im a new one to silverlight. when i was trying to put a example of Introducing Microsoft Silverlight2 into practice, i encountered this problem.
Here is the code.
1 public partial class Page : UserControl 2 { 3 double beginX = 0; 4 double beginY = 0; 5 bool isMouseDown = false; 6 7 public Page() 8 { 9 InitializeComponent(); 10 } 11 12 private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 13 { 14 Ellipse b = sender as Ellipse; 15 beginX = e.GetPosition(this).X; 16 beginY = e.GetPosition(this).Y; 17 isMouseDown = true; 18 b.CaptureMouse(); 19 } 20 21 private void Ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 22 { 23 Ellipse b = sender as Ellipse; 24 isMouseDown = false; 25 b.ReleaseMouseCapture(); 26 } 27 28 private void Ellipse_MouseMove(object sender, MouseButtonEventArgs e) 29 { 30 if (isMouseDown == true) 31 { 32 Ellipse b = sender as Ellipse; 33 double currX = e.GetPosition(this).X; 34 double currY = e.GetPosition(this).Y; 35 b.SetValue(Canvas.TopProperty, currX); 36 b.SetValue(Canvas.TopProperty, currY); 37 } 38 } 39 }
1 <Grid x:Name="LayoutRoot" Background="White"> 2 <Canvas Width="400" Height="300" Background="Wheat"> 3 <Ellipse Fill="Black" Width="20" Height="20" 4 MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" 5 MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" 6 MouseMove="Ellipse_MouseMove"/> 7 <Ellipse Canvas.Top="20" Fill="Black" Width="20" Height="20" 8 MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" 9 MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" 10 MouseMove="Ellipse_MouseMove"/> 11 </Canvas> 12 </Grid>
The code is to drag ellipse from startpoint to somewhere else.
It was error discription AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 10 Position: 32] and it related to InitializeComponent();
elmore.adam
482 points
67 Posts
07-03-2009 1:14 PM |
The first thing you need to realize is that your corresponding XAML must contain a UserControl element with the x:Class attribute value pointing to your fully qualified class name. This allows you to take advantage of partial classes and is the key to working with XAML files. Examine the following revised code sample and see if this helps you out.
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Shapes;
namespace ExampleNamespace
{
public partial class Page : UserControl
double beginX = 0;
double beginY = 0;
bool isMouseDown = false;
public Page()
InitializeComponent();
}
private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
Ellipse b = sender as Ellipse;
beginX = e.GetPosition(this).X;
beginY = e.GetPosition(this).Y;
isMouseDown = true;
b.CaptureMouse();
private void Ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
isMouseDown = false;
b.ReleaseMouseCapture();
private void Ellipse_MouseMove(object sender, MouseButtonEventArgs e)
if (isMouseDown == true)
double currX = e.GetPosition(this).X;
double currY = e.GetPosition(this).Y;
b.SetValue(Canvas.TopProperty, currX);
b.SetValue(Canvas.TopProperty, currY);
<UserControl
x:Class="ExampleNamespace.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid
x:Name="LayoutRoot"
Background="White">
<Canvas
Width="400"
Height="300"
Background="Wheat">
<Ellipse
Fill="Black"
Width="20"
Height="20"
MouseLeftButtonDown="Ellipse_MouseLeftButtonDown"
MouseLeftButtonUp="Ellipse_MouseLeftButtonUp"
MouseMove="Ellipse_MouseMove" />
Canvas.Top="20"
</Canvas>
</Grid>
</UserControl>
07-04-2009 12:58 AM |
Thanks, but i've done what you said. Here is the whole code. It's doesn't work.
using System; using System.Collections.Generic; using System.Linq; using System.Net; 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 SilverlightApplication6 { public partial class Page : UserControl { double beginX = 0; double beginY = 0; bool isMouseDown = false; public Page() { InitializeComponent(); } private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Ellipse b = sender as Ellipse; beginX = e.GetPosition(this).X; beginY = e.GetPosition(this).Y; isMouseDown = true; b.CaptureMouse(); } private void Ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { Ellipse b = sender as Ellipse; isMouseDown = false; b.ReleaseMouseCapture(); } private void Ellipse_MouseMove(object sender, MouseButtonEventArgs e) { if (isMouseDown == true) { Ellipse b = sender as Ellipse; double currX = e.GetPosition(this).X; double currY = e.GetPosition(this).Y; b.SetValue(Canvas.TopProperty, currX); b.SetValue(Canvas.TopProperty, currY); } } } }
1 <UserControl x:Class="SilverlightApplication6.Page" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Width="400" Height="300"> 5 <Grid x:Name="LayoutRoot" Background="White"> 6 <Canvas Width="400" Height="300" Background="Wheat"> 7 <Ellipse Fill="Black" Width="20" Height="20" 8 MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" 9 MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" 10 MouseMove="Ellipse_MouseMove"/> 11 <Ellipse Canvas.Top="20" Fill="Black" Width="20" Height="20" 12 MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" 13 MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" 14 MouseMove="Ellipse_MouseMove"/> 15 </Canvas> 16 </Grid> 17 </UserControl>
Krasshirsch
Participant
1032 points
300 Posts
07-04-2009 4:40 AM |
Your mouse move event handler has an invalid signature.
You need to use MouseEventArgs instead of MouseButtonEventArgs.
07-04-2009 7:41 AM |
Thank you.
And how stupid i am.
07-04-2009 11:13 AM |
I'm afraid I was staring at the forest and missed the tree!