加载自定义用户控件时出错

Error while loading Custom UserControl

我为我的应用程序创建了一个自定义按钮,在内容中它有一个 Textblock 和一个 SymbolIcon,都放在 Grid 中。 这是非常简单的代码

<UserControl
    ...>

        <Grid>
            <Button Name="ButtonControl">
                <Button.Content>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>

                        <SymbolIcon Grid.Column="0"
                                    Symbol="Italic"
                                    Name="SymbolIconColumn"
                                     />
                        <TextBlock Grid.Column="1"
                                   Text=""
                                   Name="TextBlockColumn"
                                   />
                    </Grid>
                </Button.Content>
            </Button>
        </Grid>

</UserControl>

并且在其 class 文件中,我声明了几个属性:

  1. SymbolIcon 基本上设置了 Symbol 属性 SymbolIcon
  2. Text 设置 Text 属性 的 Textblock
  3. ClickButton
  4. 设置 Click 事件处理程序

这是背后的代码:

public string Text
{
    get
    {
        return (string)GetValue(TextProperty);
    }
    set
    {
        SetValue(TextProperty, value);
        TextBlockColumn.Text = value;
    }
}

public Symbol SymbolIcon
{
   get
   {
       return (Symbol)GetValue(SymbolIconProperty);
   }
   set
   {
       SetValue(SymbolIconProperty, value);
   }
}


public RoutedEventHandler Click
{
    get
    {
        return (RoutedEventHandler)GetValue(ClickProperty);
    }
    set
    {
        SetValue(ClickProperty, value);
        ButtonControl.Click += value;
    }
}

public RoutedEventHandler Click
{
    get
    {
        return (RoutedEventHandler)GetValue(ClickProperty);
    }
    set
    {
        SetValue(ClickProperty, value);
        ButtonControl.Click += value;
    }
}

我已经用他们的 DependencyProperty.Register 功能注册了他们所有人

public static readonly DependencyProperty SymbolIconProperty =
            DependencyProperty.Register("SymbolIcon", typeof(Symbol), 
                typeof(TimerButton), new PropertyMetadata(Symbol.Italic));

public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string),
                typeof(TimerButton), new PropertyMetadata(""));

public static readonly DependencyProperty ClickProperty =
            DependencyProperty.Register("Click", typeof(RoutedEventHandler),
                typeof(TimerButton), new PropertyMetadata(null));

我想我已经做了一切,对吧? 所以,如果我将命名空间添加到我的页面并尝试加载它,就像这样

<Page
    ...
    xmlns:bt="using:MyProject.Resources"...>

<bt:TimerButton
                SymbolIcon="Accept"
                Text="Accept"
                />

设计器甚至不会加载,它给了我这个

System.Runtime.Remoting.RemotingException [9652] Designer process terminated unexpectedly!

此外,当必须加载页面时,调试会崩溃。

感谢@Clemens,我通过删除不必要的指令和 Click EventHandler 属性 更正了代码。 EventHandler 不能添加为 DependencyProperty,因此所有调试崩溃和所有异常抛出。我还在考虑如何添加它。

public string ButtonText
{
    get
    {
        return (string)GetValue(TextProperty);
    }
    set
    {
        SetValue(TextProperty, value);
    }
}

public Symbol Icon
{
   get
   {
       return (Symbol)GetValue(SymbolIconProperty);
   }
   set
   {
       SetValue(SymbolIconProperty, value);
   }
}

我还在 TextBlockTextSymbolIconSymbol 属性中添加了绑定。我通过为 UserControl (MyButton) 提供 x:Name 并将 属性 与字符串 {Binding Path=/*name of the property of the UserControl to bind*/, ElementName="MyButton"

绑定来完成此操作
<UserControl
...
x:Name="MyButton">
....
....
<!--only showing for the TextBlock, but I've done the same for the SymbolIcon-->
<TextBlock Grid.Column="1"
           Text="{Binding Path=ButtonText,ElementName=MyButton}"/>