How do you specify a different base class in .xaml files (Silverlight)?

后端 未结 2 2034
眼角桃花
眼角桃花 2020-12-17 16:08

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 d

相关标签:
2条回答
  • 2020-12-17 17:04

    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. Create your class with the common properties

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

    2. Create/modify a Page's XAML

    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

    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">
    

    Modify the code-behind

    (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.

    0 讨论(0)
  • 2020-12-17 17:07

    Also: when your base class is in an other assembly (project) you can do this:

    xmlns:Custom="clr-namespace:SilverlightApplication1;assembly=[Other.Assembly]"

    0 讨论(0)
提交回复
热议问题