Implementing your own base class for user controls in Silverlight 2

Implementing your own base class for user controls in Silverlight 2,第1张

概述The objective is to create your own base class for user controls to implement application related features and at the same time also use the features provided by the Visual Studio (i.e. auto generate @H_403_0@ @H_403_0@

The objective is to create your own base class for user controls to implement application related features and at the same time also use the features provIDed by the Visual Studio (i.e. auto generate a partial class that initialize all UI elements).

The process can be described best with three projects:

 

For the sake of simplicity I created a simple UserControlBase class extending from the UserControl class. This calss can contain the common methods and propertIEs as needed for your application. Here I have added some dummy methods and propertIEs.

namespace Baselibrary {     public class UserControlBase : UserControl     {         public int ID { getset; }         public voID DoSomeThing()         {             //...         }     }

Then,lets create a TestControl class and a TestControl.xaml in the class library where we like the have the custom controls:

namespace CustomControls {     public partial class TestControl : UserControlBase     {         public TestControl()         {             InitializeComponent();         }      }

Now,here is the trick. Look closely to the xaml. Instead of regular UserControl, we used our own base class. To do so,we also have to include the namespace.

<bl:UserControlBase x:Class="MyControls.TestControl"     xmlns="http://schemas.microsoft.com/clIEnt/2007"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:bl="clr-namespace:Baselibrary;assembly=Baselibrary"     WIDth="150" Height="50">     <GrID Background="lightCoral">         <TextBlock Text="I am a test control"/>     </GrID> </bl:UserControlBase> 

In this way,Visual Studio also generates the partial class properly. But there is one sIDe effect: the Visual Studio will not be able to show you the UI prevIEw in designer. I haven't found any work around yet.

 

Update:

I forgot to add the reference in AsseblyInfo.cs file of Baselibrary project. Once you add the following reference the Visual Studio will render the UI properly. Thanks to Michael for pointout the issue.

 

[assembly: XmlnsDeFinition("http://schemas.microsoft.com/clIEnt/2007","Baselibrary")] 

@H_403_0@ 总结

以上是内存溢出为你收集整理的Implementing your own base class for user controls in Silverlight 2全部内容,希望文章能够帮你解决Implementing your own base class for user controls in Silverlight 2所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1053118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存