I am in the process of converting a pretty big 1.1 project to 2.0 and I have a couple of questions;
1. The original code uses controls as resources (build action: embedded resources) and they are loading the xaml from the Manifest Resource Stream to initiate the controls. That does still work, however it doesn't work on nested controls. ie:
If Control2 is inside Control1 it's fails when trying to XamlReader.Load (XamlParseException) Control1 to add it on a page . I recreated this with a very simple example and as far as I can see this just doesn't work anymore.
2. When using usercontrol I would like to use a base class like you can do in WPF (see this post for example:
http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx) but this as well gives me XamlParseException. It's kind of strange because the editor seems to support it fine.
It does work if you don't do the xaml bit but then you have to manually change the *g.cs file anytime you do a change.
I can of course fix these things with workaround but it's a project with 100+ controls and I would to not have to rebuild them all. So if anyone know to fix either one of the two please let me know.
If Control2 is inside Control1 it's fails when trying to XamlReader.Load (XamlParseException) Control1 to add it on a page . I recreated this with a very simple example and as far as I can see this just doesn't work anymore.
That's interesting. Can you send your sample to me? My id is mchlsync AT gmail.com. I would like to take a look and will get back to you if I found the solution.
bratta
2. When using usercontrol I would like to use a base class like you can do in WPF (see this post for example:
http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx) but this as well gives me XamlParseException. It's kind of strange because the editor seems to support it fine.
It does work if you don't do the xaml bit but then you have to manually change the *g.cs file anytime you do a change.
I tried as below. It works for me.
Create SL project.
MyUserControlBase.cs
Inherits from UserControl
namespace SL2Test {
public class MyUserControlBase : UserControl {
public string DoSomething() {
return "Ahh! bad attemps!";
}
}
}
In Page.xaml.cs
namespace SL2Test {
public partial class Page : MyUserControlBase {
public Page() {
InitializeComponent();
Re 2: You are right. That does seems to work the way you explained. However my problem is that I have my baseclasses in another namespace (in this case another assembly as well, but it doesn't matter, it errors as soon as you specify namespace).
So it's something like this:
<
euc:Test1 x:Class="UserControls.Page"
xmlns=http://schemas.microsoft.com/client/2007
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:euc="clr-namespace:ExternalUserControl;assembly=ExternalUserControl">
...
</euc:Test1>
For me that it kind of makes sense that it errors because it doesn't know about the namespace until afterwards.. Or something like that. Though in the example of the before mentioned url that works (in WPF).
Yes, nesting usercontrols works fine, which is what I am doing now (just annoying having to change all the current controls). The problem arises when they are controls with a build action of "embedded resources" and you use the
InitializeFromXaml to load in the XAML (please see the example I sent you).
I have made all the controls that had custom controls inside them to usercontrols to make this work. Only real problem I have now is the second one, having to manually update all the *g.cs files everytime I change a xaml file or if I do a rebuild....
Yes I can. But that limits me to use a class in the same namespace as the control. Since I have loads of controls they are divided in different namespaces and I have one class in another namespace that I want to use as the baseclass. In WPF that works by
referencing the namespace with an xmlns.
Example (app is called SilverlightApplication2):
BaseClass:
namespace SilverlightApplication2.AnotherNameSpace
{
public class
Class1: UserControl
{}
}
Page.xaml.cs:
public partial
class Page : SilverlightApplication2.AnotherNameSpace.Class1
{
public Page()
{
InitializeComponent();
}
}
Page.xaml (need to add a xmlns to tell it where to find the namespace):
<test:Class1 x:Class="SilverlightApplication2.Page"
xmlns:test="clr-namespace:SilverlightApplication2.AnotherNameSpace"
xmlns=http://schemas.microsoft.com/client/2007
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</test:Class1>
It works in the designer, it builds fine etc but in InitializeComponent it errors on this line:
System.Windows.Application.LoadComponent(this,
new System.Uri("/SilverlightApplication2;component/Page.xaml", System.UriKind.Relative));
Error:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll
Additional information: AG_E_UNKNOWN_ERROR [Line: 6 Position: 95]
So it seems the application load component doesn't support that xaml syntax.
Actually it shouldn't work. MyUserControlBase is not in the namespace
http://schemas.microsoft.com/client/2007. This xml namespaces maps to a series of clr namespaces such as System.Windows.Controls. But there's no way it can map to your own namespace. Also this behavior is inconsistent with WPF.
This is likely to be a bug. I'll redirect this to our product team. Thanks for letting us know.
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
bratta
Member
12 Points
8 Posts
Porting a 1.1 project to 2.0 - Usercontrol inheritance and nested resources
Mar 08, 2008 04:25 AM | LINK
Hey all.
I am in the process of converting a pretty big 1.1 project to 2.0 and I have a couple of questions;
1. The original code uses controls as resources (build action: embedded resources) and they are loading the xaml from the Manifest Resource Stream to initiate the controls. That does still work, however it doesn't work on nested controls. ie:
If Control2 is inside Control1 it's fails when trying to XamlReader.Load (XamlParseException) Control1 to add it on a page . I recreated this with a very simple example and as far as I can see this just doesn't work anymore.
2. When using usercontrol I would like to use a base class like you can do in WPF (see this post for example: http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx) but this as well gives me XamlParseException. It's kind of strange because the editor seems to support it fine. It does work if you don't do the xaml bit but then you have to manually change the *g.cs file anytime you do a change.
I can of course fix these things with workaround but it's a project with 100+ controls and I would to not have to rebuild them all. So if anyone know to fix either one of the two please let me know.
Cheers,
-Thomas-
mchlSync
Star
14968 Points
2799 Posts
Re: Porting a 1.1 project to 2.0 - Usercontrol inheritance and nested resources
Mar 08, 2008 04:57 AM | LINK
That's interesting. Can you send your sample to me? My id is mchlsync AT gmail.com. I would like to take a look and will get back to you if I found the solution.
I tried as below. It works for me.
namespace SL2Test {
public class MyUserControlBase : UserControl {
public string DoSomething() {
return "Ahh! bad attemps!";
}
}
}
namespace SL2Test {
public partial class Page : MyUserControlBase {
public Page() {
InitializeComponent();
}
}
}
<MyUserControlBase x:Class="SL2Test.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Canvas Width="400" Height="300" Background="Red">
</Canvas>
</MyUserControlBase>
Regards,
Michael Sync
Silverlight MVP
Blog : http://michaelsync.net
bratta
Member
12 Points
8 Posts
Re: Porting a 1.1 project to 2.0 - Usercontrol inheritance and nested resources
Mar 08, 2008 06:00 AM | LINK
Hey.
Thanks for your quick reply. I will give the namespace thing another try. It's 5am and I am getting quite tired so that might be why ;)
I sent you an example on the nesting resources problem.
Thanks,
-Thomas-
bratta
Member
12 Points
8 Posts
Re: Porting a 1.1 project to 2.0 - Usercontrol inheritance and nested resources
Mar 08, 2008 06:14 AM | LINK
Re 2: You are right. That does seems to work the way you explained. However my problem is that I have my baseclasses in another namespace (in this case another assembly as well, but it doesn't matter, it errors as soon as you specify namespace).
So it's something like this:
<
euc:Test1 x:Class="UserControls.Page"xmlns=http://schemas.microsoft.com/client/2007
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:euc="clr-namespace:ExternalUserControl;assembly=ExternalUserControl">
...
</euc:Test1> For me that it kind of makes sense that it errors because it doesn't know about the namespace until afterwards.. Or something like that. Though in the example of the before mentioned url that works (in WPF).
mchlSync
Star
14968 Points
2799 Posts
Re: Porting a 1.1 project to 2.0 - Usercontrol inheritance and nested resources
Mar 09, 2008 05:09 AM | LINK
Hi,
I received your mail and also replied to you. It might be something wrong with your project. I have tested with new porject. It's working fine.
You can download the sample from this link. http://michaelsync.net/demo/SL2TestNestedCtls.zip
Regards,
Michael Sync
Silverlight MVP
Blog : http://michaelsync.net
bratta
Member
12 Points
8 Posts
Re: Porting a 1.1 project to 2.0 - Usercontrol inheritance and nested resources
Mar 09, 2008 07:13 AM | LINK
Hey again.
Yes, nesting usercontrols works fine, which is what I am doing now (just annoying having to change all the current controls). The problem arises when they are controls with a build action of "embedded resources" and you use the InitializeFromXaml to load in the XAML (please see the example I sent you).
I have made all the controls that had custom controls inside them to usercontrols to make this work. Only real problem I have now is the second one, having to manually update all the *g.cs files everytime I change a xaml file or if I do a rebuild....
Cheers,
-Thomas-
mchlSync
Star
14968 Points
2799 Posts
Re: Porting a 1.1 project to 2.0 - Usercontrol inheritance and nested resources
Mar 09, 2008 09:50 AM | LINK
oh. I though you are having the problem with nested controls.
You mean, you can't use like that?
<MyUserControlBase x:Class="SL2Test.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Canvas Width="400" Height="300" Background="Red">
</Canvas>
</MyUserControlBase>
Regards,
Michael Sync
Silverlight MVP
Blog : http://michaelsync.net
bratta
Member
12 Points
8 Posts
Re: Porting a 1.1 project to 2.0 - Usercontrol inheritance and nested resources
Mar 09, 2008 10:01 PM | LINK
Yes I can. But that limits me to use a class in the same namespace as the control. Since I have loads of controls they are divided in different namespaces and I have one class in another namespace that I want to use as the baseclass. In WPF that works by referencing the namespace with an xmlns.
Example (app is called SilverlightApplication2):
BaseClass:
Page.xaml.cs:namespace SilverlightApplication2.AnotherNameSpace
{
public class Class1: UserControl
{}
}
public partial class Page : SilverlightApplication2.AnotherNameSpace.Class1
{
public Page()
{
InitializeComponent();
}
}
Page.xaml (need to add a xmlns to tell it where to find the namespace):
<test:Class1 x:Class="SilverlightApplication2.Page"
xmlns:test="clr-namespace:SilverlightApplication2.AnotherNameSpace"
xmlns=http://schemas.microsoft.com/client/2007
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</test:Class1> It works in the designer, it builds fine etc but in InitializeComponent it errors on this line:
System.Windows.Application.LoadComponent(this, new System.Uri("/SilverlightApplication2;component/Page.xaml", System.UriKind.Relative));
Error:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll
Additional information: AG_E_UNKNOWN_ERROR [Line: 6 Position: 95]
So it seems the application load component doesn't support that xaml syntax.
Any ideas?
Thanks,
-Thomas-
Guntae Park
Member
2 Points
2 Posts
Re: Porting a 1.1 project to 2.0 - Usercontrol inheritance and nested resources
Mar 10, 2008 02:15 PM | LINK
Hi.
If you want to use "{yourUserControl}.g.cs" for automatic binding xaml object, I don't know how it's possible.
But if you want to port a 1.1 project to 2.0, there is a simple way.
Try this.
1. xaml file's BuildAction must be changed 'SilverlightPage'(or 'EmbededResource') to 'Resource'.
2. in 1.1,
System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("{xaml file's path in 1.1 Style}");
_root = (Canvas)this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());
Change this part as follows.
System.Windows.Resources.StreamResourceInfo sri = System.Windows.Application.GetResourceStream(new System.Uri("{xaml file's path in 2.0 Style}", System.UriKind.Relative));
string sXaml = (new System.IO.StreamReader(sri.Stream)).ReadToEnd();
this._root = InitializeFromXaml(sXaml) as Canvas;
and I make a remark about 'xaml file's path'
in 1.1 -> '{namespace}.{filename}.xaml'
in 2.0 -> '/{assemblyname}; component/{subdirectory}/{filename}.xaml'
If you change like this, you cannot use auto-generation of xaml object. So you must manually bind all xaml object to use, as you did in 1.1 alpha.
but you don't need to update all the *g.cs files everytime.
I hope that this answered your question. Good luck!
migration port 1.1 to 2.0 Silverlight usercontrol
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: Porting a 1.1 project to 2.0 - Usercontrol inheritance and nested resources
Mar 11, 2008 04:32 AM | LINK
Hello, there're some issues related to UserControl inheriting that we're investigating... I'm surprised to see this works:
<MyUserControlBase x:Class="SL2Test.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Canvas Width="400" Height="300" Background="Red">
</Canvas>
</MyUserControlBase>
Actually it shouldn't work. MyUserControlBase is not in the namespace http://schemas.microsoft.com/client/2007. This xml namespaces maps to a series of clr namespaces such as System.Windows.Controls. But there's no way it can map to your own namespace. Also this behavior is inconsistent with WPF.
This is likely to be a bug. I'll redirect this to our product team. Thanks for letting us know.