在我的自定义控件中单击按钮时发生异常 [英] Exception occurs when button clicked inside my custom control

查看:24
本文介绍了在我的自定义控件中单击按钮时发生异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 UWP 自定义模板化控件,其中包含 ICommandBarElement 的集合,很像标准的 CommandBar 控件.问题是,只要我单击 CommandBar 实现中包含的任何 AppBarButton,就会发生未处理的异常:不支持此类接口".

I have made a UWP custom templated control that contains a collection of ICommandBarElement, much like the standard CommandBar control. The problem is that as soon as I click on any of the AppBarButton contained in my CommandBar implementation, an unhandled exception occurs: "No such interface supported".

有些事情我一定做错了,但我就是看不到.这是自定义控件的代码:

There is something I must doing wrong but I just can't see it. Here is the code for the custom control:

public sealed class MyCommandBarControl : ContentControl
{
    public MyCommandBarControl()
    {
        this.DefaultStyleKey = typeof(MyCommandBarControl);

        this.PrimaryCommands = new ObservableCollection<ICommandBarElement>();
    }

    public ObservableCollection<ICommandBarElement> PrimaryCommands
    {
        get { return (ObservableCollection<ICommandBarElement>)GetValue(PrimaryCommandsProperty); }
        set { SetValue(PrimaryCommandsProperty, value); }
    }

    /// <summary>
    /// PrimaryCommands Dependency Property
    /// </summary>
    public static readonly DependencyProperty PrimaryCommandsProperty =
        DependencyProperty.Register(
            "PrimaryCommands",
            typeof(ObservableCollection<ICommandBarElement>),
            typeof(MainPage),
            new PropertyMetadata(null, null));


    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var primaryItemsControl = GetTemplateChild("PrimaryItemsControl") as ItemsControl;
        primaryItemsControl.ItemsSource = this.PrimaryCommands;

    }

}

以及相关的样式:

<Style TargetType="local:MyCommandBarControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:MyCommandBarControl">
                <Grid x:Name="LayoutRoot" Background="{TemplateBinding Background}">
                    <Grid x:Name="ContentRoot"
                          VerticalAlignment="{TemplateBinding VerticalAlignment}"
                          Margin="{TemplateBinding Padding}"
                          Height="{TemplateBinding Height}"
                          Background="{TemplateBinding Background}"
                          Opacity="{TemplateBinding Opacity}">

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <ContentControl
                              x:Name="ContentControl"
                              Content="{TemplateBinding Content}"
                              ContentTemplate="{TemplateBinding ContentTemplate}"
                              ContentTransitions="{TemplateBinding ContentTransitions}"
                              Foreground="{TemplateBinding Foreground}"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                             IsTabStop="False"
                             Margin="10, 0, 0, 0"/>

                        <ItemsControl 
                              x:Name="PrimaryItemsControl"
                              IsTabStop="False"
                              Grid.Column="1"
                              Margin="10, 0, 0, 0"
                              HorizontalAlignment="Right"  
                              HorizontalContentAlignment="Right">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我做了一个小项目你可以下载重现问题的.任何解决此异常的帮助将不胜感激.

I have made a small project you can download that reproduces the problem. Any help resolving this exception would be appreciated.

推荐答案

为什么不继承自CommandBar控件,也不需要自己管理PrimaryCommands.

Why not inherit from CommandBar control, and don't need to manage the PrimaryCommands by ourselves.

因此只需将代码更改为以下内容即可解决问题:

So simply change your code to the following can solve the issue:

public sealed class MyCommandBarControl : CommandBar
{
    public MyCommandBarControl()
    {
        this.DefaultStyleKey = typeof(MyCommandBarControl);

        //this.PrimaryCommands = new ObservableCollection<ICommandBarElement>();
    }

    //public ObservableCollection<ICommandBarElement> PrimaryCommands
    //{
    //    get { return (ObservableCollection<ICommandBarElement>)GetValue(PrimaryCommandsProperty); }
    //    set { SetValue(PrimaryCommandsProperty, value); }
    //}

    ///// <summary>
    ///// PrimaryCommands Dependency Property
    ///// </summary>
    //public static readonly DependencyProperty PrimaryCommandsProperty =
    //    DependencyProperty.Register(
    //        "PrimaryCommands",
    //        typeof(ObservableCollection<ICommandBarElement>),
    //        typeof(MainPage),
    //        new PropertyMetadata(null, null));


    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var primaryItemsControl = GetTemplateChild("PrimaryItemsControl") as ItemsControl;
        primaryItemsControl.ItemsSource = this.PrimaryCommands;

    }

}

[更新 1]

我认为存在我们看不到的底层类型转换,或者我们必须使用一些接口,例如 ICommandBar,但由于其保护级别而无法使用.

I think there is underlying type conversion we cannot see or we have to use some interfaces like ICommandBar but we cannot use due to its protection level.

如果您确实希望自己实现 CommandBar,我认为最好创建自己的 Command 按钮而不是使用 AppBarButton.但您需要确保自己有充分的理由自行完成.

If you do hope to implement CommandBar by yourself, I think it's better to create your own Command button instead of using AppBarButton. But you need to make sure you have a solid reason to do it by yourself.

例如,如果我使用默认按钮,以下内容将起作用.

For example, the following will work if I use a default Button.

public sealed class MyCommandBarControl : ContentControl
{
    public MyCommandBarControl()
    {
        this.DefaultStyleKey = typeof(MyCommandBarControl);

        this.PrimaryCommands = new ObservableCollection<Button>();
    }

    public ObservableCollection<Button> PrimaryCommands
    {
        get { return (ObservableCollection<Button>)GetValue(PrimaryCommandsProperty); }
        set { SetValue(PrimaryCommandsProperty, value); }
    }

    /// <summary>
    /// PrimaryCommands Dependency Property
    /// </summary>
    public static readonly DependencyProperty PrimaryCommandsProperty =
        DependencyProperty.Register(
            "PrimaryCommands",
            typeof(ObservableCollection<Button>),
            typeof(MainPage),
            new PropertyMetadata(null, null));


    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var primaryItemsControl = GetTemplateChild("PrimaryItemsControl") as ItemsControl;
        primaryItemsControl.ItemsSource = this.PrimaryCommands;

    }

}

在 MainPage.xaml 中

In MainPage.xaml

    <local:MyCommandBarControl>
        <local:MyCommandBarControl.Content>
            <TextBlock Text="My CommandBar" />
        </local:MyCommandBarControl.Content>

        <local:MyCommandBarControl.PrimaryCommands>
            <Button Content="Pin" Click="PinBarButton_Click" />
            <Button Content="UnPin" />
            <Button Content="Sync"  />
            <Button Content="Remove" />
        </local:MyCommandBarControl.PrimaryCommands>
    </local:MyCommandBarControl>

这篇关于在我的自定义控件中单击按钮时发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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