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

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

问题描述

如何在 .xaml 文件中为单独的 Silverlight 页面类指定通用基类?我有一些想要在页面之间共享的通用属性,但我不知道如何在每次生成 .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

这是我更新的文件:

<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天全站免登陆