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

本文关键字:出错 控件 用户 自定义 加载 | 更新日期: 2023-09-27 18:01:23

我为我的应用程序创建了一个自定义按钮,在内容中它有一个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>

在其类文件中,我声明了几个属性:

  1. SymbolIcon基本上设置了 Symbol 属性 SymbolIcon
  2. Text设置TextblockText 属性
  3. Click设置Click事件处理程序Button

这是背后的代码:

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

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

系统.运行时.远程处理.远程处理异常 [9652] 设计器进程意外终止!

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

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

多亏了

@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属性中添加了绑定。我通过向用户控件 ( 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}"/>