如何在.xaml文件(Silverlight)中指定不同的基类? [英] How do you specify a different base class in .xaml files (Silverlight)?

查看:118
本文介绍了如何在.xaml文件(Silverlight)中指定不同的基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在.xaml文件中为单独的Silverlight Page类指定公共基类?我有一些常见的属性,我想在页面之间共享,但我不知道如何在不生成每次生成的情况下手动更改.g.cs文件中的基类。

How can you specify a common base class in .xaml files for seperate Silverlight Page classes? I have a few common properties that I would like to share across pages, but I don't know how to do this without manually changing the base class in the .g.cs files each time they are generated.

这可能吗?我认为这是可能的,因为例如,Toolkit中的ChildControl来自不同的类。我忽略了一些明显的东西吗

Is this possible? I assume it is possible, since the ChildControl in the Toolkit, for example, derives from a different class. Am I overlooking something obvious?

推荐答案

默认情况下,所有Silverlight页面实际上都是从UserControl派生的。所以,这就是你需要做的。简单的例子,当然你可能想要声明依赖属性,事件等。

All Silverlight "pages" are actually deriving from UserControl by default. So, here's what you need to do. Simple example, of course you'd probably want to declare Dependency properties, events, and more.

1。使用公共属性创建类

public class YourUserControlBase : UserControl
{
    public bool SomeProperty {get; set; }
}

2。创建/修改页面的XAML

为包含新基类的本地程序集和命名空间添加XML命名空间,并记住保留 x:Class 属性位于文件顶部,但将UserControl根元素修改为本地名称

Add a XML namespace for the local assembly and namespace that contains your new base class, and remember you keep the x:Class attribute at the top of the file, but modify the UserControl root element to be the local name

这是我的更新file:

Here's my updated file:

<local:YourUserControlBase
xmlns:local="clr-namespace:SilverlightApplication1"
x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
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" 
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">




修改代码隐藏

(Page.xaml.cs文件,而不是自动生成的文件)到正确继承自 YourUserControlBase

(the Page.xaml.cs file, not the auto-generated one) to properly inherit from YourUserControlBase:

public partial class MainPage : YourUserControlBase
{
    public MainPage()
    {
        InitializeComponent();
    }
}

那应该是它!祝你好运。

That should be it! Good luck.

这篇关于如何在.xaml文件(Silverlight)中指定不同的基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆