2015-11-03 15 views
2

我製作了一個UWP自定義模板控件,它包含ICommandBarElement的集合,非常類似於標準的CommandBar控件。問題是,只要我點擊我的CommandBar實現中包含的任何AppBarButton,就會發生未處理的異常:「沒有支持這樣的接口」。在我的自定義控件中單擊按鈕時發生異常

有一些我必須做錯,但我不能看到它。下面是定製控件的代碼:

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> 

我已經作出了small project you can download能重現問題。任何幫助解決這個例外,將不勝感激。

回答

1

爲什麼不從CommandBar控件繼承,並且不需要自己管理PrimaryCommands。

所以,簡單地更改您的代碼下面就可以解決這一問題:

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; 

    } 

} 

[UPDATE1]

我覺得這是基本的類型轉換,我們不能看到或者我們必須使用一些接口像ICommandBar,但由於其保護級別,我們無法使用。

如果你確實希望自己實現CommandBar,我認爲最好創建自己的Command按鈕,而不是使用AppBarButton。 但是你需要確保你有一個堅實的理由來自己做。

例如,如果我使用默認按鈕,以下方法將起作用。

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中

<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> 
+1

奇怪 - 我使用* AppBarButtons * *之外*命令欄和他們沒有問題的工作。關於在命令欄以外的其他地方不使用這些控件的更多詳細信息, – Romasz

+0

你說得對。它可以在CommandBar之外使用。其實,我的意思是PrimaryCommands集合。我相信有一種辦法可以做到OP所需要的,所以我仍在對此進行調查。我沒有訪問源代碼,所以仍然不知道它背後有什麼樣的奧祕。將會更新,如果我找到了什麼。 –

+0

我也嘗試使用* IObservableVector *而不是集合(像原始的PrimaryCommands),但它導致相同的異常。我不知道它在尋找什麼樣的界面。 – Romasz

相關問題