I use the XamlReader.Load to create a grid on runtime. After updating to beta 2, I cannot access the <Grid.Resources> anymore.
UIElement grid = XamlReader.Load(xaml) as UIElement;
Although the grid's resource dictionary is not empty, the Count property is 0.
((Grid)grid ).Resources.Count --> 0
I was using the XAML for object creation what worked fine with beta 1. But now, as the resource dictionary seems to be empty after loading the xaml, that's not working any longer. Any idea?
No, everything is fine with the casting, although he could have used Grid instread of UIElement in the first place. There is no need to cast to a UIElement and then to a Grid.
If XamlReader.Load returns a Grid, casting it to one of its base classes (UIElement) does not strip anything from the object. It is still the same obhject. And then he casts it back to a Grid to get the Resources, so that's fine, just a little unnecessary.
There is either something strange in the XAML or it is a ResourceDIctionary bug. If you share the XAML, I can try to figure out which.
In my case I initially tried to setup the resource on the local object [ie. Rectangle.Resources], but then simplified it down so that the resource was on the UserControl itself. In 1.1 this was fine, and I only see this now in beta 2.
Hi there! I am having a problem when i add a resource too... but in my case i am doing it trough a Foreach...
int index = 0;
foreach (UIElement element in collection)
{
myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.Duration = duration;
myDoubleAnimation.From = 0;
sbImageAppear = new Storyboard();
sbImageAppear.Duration = duration;
sbImageAppear.Children.Add(myDoubleAnimation);
TimeSpan time = new TimeSpan(0, 0, 0, 0, timeCounter);
sbImageAppear.BeginTime = time;
Storyboard.SetTarget(myDoubleAnimation, element);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(UIElement.Opacity)"));
myDoubleAnimation.To = opacity; this.Resources.Add(index.ToString(), sbImageAppear); <<<<---!!! An item with the same key has already been added
sbImageAppear.Begin();
timeCounter = timeCounter + 200;
index++;
}
ANY HELP????
Ivana A. Tilca | Microsoft Student Partner | Channel 8 editor | Microsoft
It may be because you are not giving the resources names? I thought that with Silverlight 1.1 and above, all elements in a ResourceDictionary [including Storyboard] must have their x:Name set... I may be wrong though, I'll have to double check with the
actual code tomorrow.
var u = from element in LayoutRoot.Children
where element is UIElement
select element;
foreach (var itm in u)
{
myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.Duration = duration;
myDoubleAnimation.From = 0;
sbImageAppear = new Storyboard();
sbImageAppear.Duration = duration;
sbImageAppear.Children.Add(myDoubleAnimation);
TimeSpan time = new TimeSpan(0, 0, 0, 0, timeCounter);
sbImageAppear.BeginTime = time;
fred_himself
Member
2 Points
2 Posts
Object creation by using xaml fails
Jun 20, 2008 09:13 AM | LINK
I use the XamlReader.Load to create a grid on runtime. After updating to beta 2, I cannot access the <Grid.Resources> anymore.
UIElement grid = XamlReader.Load(xaml) as UIElement;
Although the grid's resource dictionary is not empty, the Count property is 0.
((Grid)grid ).Resources.Count --> 0
I was using the XAML for object creation what worked fine with beta 1. But now, as the resource dictionary seems to be empty after loading the xaml, that's not working any longer. Any idea?
XamlReader Resources Beta 2
robhouweling
Contributor
3308 Points
565 Posts
Re: Object creation by using xaml fails
Jun 20, 2008 06:16 PM | LINK
This is because you're using a UIElement as the object for your Grid. UIElement does not containt Resources.
Try using:
Grid grid = XamlReader.Load(xaml) as Grid;
instead of:
UIElement grid = XamlReader.Load(xaml) as UIElement;
Cheers!
Rob Houweling
My blog
Dave Relyea
Participant
1090 Points
252 Posts
Microsoft
Re: Re: Object creation by using xaml fails
Jun 21, 2008 04:23 PM | LINK
No, everything is fine with the casting, although he could have used Grid instread of UIElement in the first place. There is no need to cast to a UIElement and then to a Grid.
If XamlReader.Load returns a Grid, casting it to one of its base classes (UIElement) does not strip anything from the object. It is still the same obhject. And then he casts it back to a Grid to get the Resources, so that's fine, just a little unnecessary.
There is either something strange in the XAML or it is a ResourceDIctionary bug. If you share the XAML, I can try to figure out which.
http://blogs.msdn.com/devdave
SteveWong
Contributor
6719 Points
1346 Posts
Re: Re: Re: Object creation by using xaml fails
Jun 21, 2008 05:25 PM | LINK
I have also tried it. It should be a bug.
Or we should set the Resouces after Loaded the XAML by calling its name
SteveWong (HongKong)
Please mark post as answer if they help you
ssawchenko
Member
438 Points
214 Posts
Re: Re: Re: Object creation by using xaml fails
Jun 24, 2008 12:01 AM | LINK
I have also run into this.
In my case I initially tried to setup the resource on the local object [ie. Rectangle.Resources], but then simplified it down so that the resource was on the UserControl itself. In 1.1 this was fine, and I only see this now in beta 2.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WrapPanel="clr-namespace:WrapPanelExt;assembly=Graphics4"
xmlns:local="clr-namespace:Graphics4;assembly=Graphics4"
Width="1000" Height="700">
<Storyboard x:Name="MyAnimatedRectangle_Storyboard">
<DoubleAnimation
Storyboard.TargetName="MyAnimatedRectangle"
Storyboard.TargetProperty="Opacity"
From="1.0" To="0.0" Duration="0:0:1"
AutoReverse="True" RepeatBehavior="Forever" />
</Storyboard>
</UserControl.Resources>
{
int testCount = this.Resources.Count; // <== 0 [should be 1?]
newSB.SetValue(FrameworkElement.NameProperty, "testing");
In my case, I will not know the name of the resources when I load the XAML, so I MUST be able to enumerate through them in the code behind.
SteveWong
Contributor
6719 Points
1346 Posts
Re: Re: Re: Re: Object creation by using xaml fails
Jun 24, 2008 02:41 AM | LINK
Just post that in the bug forum [;)]
SteveWong (HongKong)
Please mark post as answer if they help you
Allen Chen –...
Star
13895 Points
1808 Posts
Microsoft
Re: Object creation by using xaml fails
Jun 24, 2008 06:15 AM | LINK
Hi:
Thanks for your reporting. It's a known issue that being worked on.
Regards
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
ivanatilca
Member
113 Points
110 Posts
Re: Object creation by using xaml fails
Jun 24, 2008 11:24 PM | LINK
Hi there! I am having a problem when i add a resource too... but in my case i am doing it trough a Foreach...
int index = 0;
foreach (UIElement element in collection)
{
myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.Duration = duration;
myDoubleAnimation.From = 0;
sbImageAppear = new Storyboard();
sbImageAppear.Duration = duration;
sbImageAppear.Children.Add(myDoubleAnimation);
TimeSpan time = new TimeSpan(0, 0, 0, 0, timeCounter);
sbImageAppear.BeginTime = time;
Storyboard.SetTarget(myDoubleAnimation, element);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(UIElement.Opacity)"));
myDoubleAnimation.To = opacity;
this.Resources.Add(index.ToString(), sbImageAppear); <<<<---!!! An item with the same key has already been added
sbImageAppear.Begin();
timeCounter = timeCounter + 200;
index++;
}
ANY HELP????
http://ivanatilca.com.ar
ssawchenko
Member
438 Points
214 Posts
Re: Object creation by using xaml fails
Jun 25, 2008 12:09 AM | LINK
It may be because you are not giving the resources names? I thought that with Silverlight 1.1 and above, all elements in a ResourceDictionary [including Storyboard] must have their x:Name set... I may be wrong though, I'll have to double check with the actual code tomorrow.
SteveWong
Contributor
6719 Points
1346 Posts
Re: Re: Object creation by using xaml fails
Jun 25, 2008 02:17 AM | LINK
ivanatilca:
What is the error giving?
I cant see any problem with the lines
But If i were you, I will write this
var u = from element in LayoutRoot.Children
where element is UIElement
select element;
foreach (var itm in u)
{
myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.Duration = duration;
myDoubleAnimation.From = 0;
sbImageAppear = new Storyboard();
sbImageAppear.Duration = duration;
sbImageAppear.Children.Add(myDoubleAnimation);
TimeSpan time = new TimeSpan(0, 0, 0, 0, timeCounter);
sbImageAppear.BeginTime = time;
Storyboard.SetTarget(myDoubleAnimation, ((UIElement)itm));
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(UIElement.Opacity)"));
myDoubleAnimation.To = opacity;
this.Resources.Add(index.ToString(), sbImageAppear);
sbImageAppear.Begin();
timeCounter = timeCounter + 200;
index++;
}
SteveWong (HongKong)
Please mark post as answer if they help you