2014-04-09 74 views
0

我試圖繼承一個usercontrol的第一次,但一直面臨着很多錯誤。未能分配給屬性'Microsoft.Phone.Controls.MenuItem.Click'

這裏是基本用戶控件 - Generic_Icon

XAML

<UserControl x:Class="project.Icons.Generic_Icon" 
    xmlns:ob="clr-namespace:project.Objects" 
    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" 
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
    mc:Ignorable="d" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    d:DesignHeight="120" d:DesignWidth="120"> 
    <UserControl.Resources> 
     <Style x:Key="ButtonStyle1" TargetType="Button"> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> 
      <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
      <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
      <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> 
      <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/> 
      <Setter Property="Padding" Value="10,5,10,6"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Grid Background="Transparent"> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualState x:Name="Normal"/> 
            <VisualState x:Name="MouseOver"/> 
            <VisualState x:Name="Pressed"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Disabled"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="0"> 
           <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
          </Border> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </UserControl.Resources> 

    <Button x:Name="button" Click="Icon_button_Click" Style="{StaticResource ButtonStyle1}" Padding="0"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition/> 
       <RowDefinition Height="25"/> 
      </Grid.RowDefinitions> 
      <!--<Grid.Background> 
       <RadialGradientBrush> 
        <GradientStop Color="#ff31c8f5" Offset="0.514"/> 
        <GradientStop Color="#0016DAC0" Offset="1"/> 
       </RadialGradientBrush> 
      </Grid.Background>--> 
      <Image x:Name="Img" Margin="6" Source="/Assets/Icon Pack/Camera.png" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
      <TextBlock x:Name="Title" Grid.Row="1" Text="Camera" FontSize="16" FontFamily="Segoe WP SemiLight" HorizontalAlignment="Center" /> 
     </Grid> 
    </Button> 
    <toolkit:ContextMenuService.ContextMenu> 
     <toolkit:ContextMenu> 
      <toolkit:MenuItem Header="Remove" Click="Remove_Icon_Click"/> 
      <toolkit:MenuItem Header="Replace" Click="Replace_Icon_Click"/> 
     </toolkit:ContextMenu> 
    </toolkit:ContextMenuService.ContextMenu> 
</UserControl> 

CS

public partial class Generic_Icon : UserControl 
    { 
     public event EventHandler remove; 
     public event EventHandler replace; 
     public EventArgs e = null; 
     public delegate void EventHandeler(object c, EventArgs e); 
     public Generic_Icon() 
     { 
      InitializeComponent(); 
     } 
     public ImageSource get_image_source() 
     { 
      return Img.Source; 
     } 
     public String get_title() 
     { 
      return Title.Text; 
     } 
     public virtual void Icon_button_Click(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show("clicked"); 
     } 
     public void Remove_Icon_Click(object sender, System.EventArgs e) 
     { 
      remove(this, e); 
     } 
     public void Replace_Icon_Click(object sender, System.EventArgs e) 
     { 
      replace(this, e); 
     } 

    } 

派生的用戶控件相機 XA ML

<icon:Generic_Icon x:Class="project.Icons.Camera" 
    xmlns:ob="clr-namespace:project.Objects" 
    xmlns:icon="clr-namespace:project.Icons" 
    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" 
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
    mc:Ignorable="d" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    d:DesignHeight="120" d:DesignWidth="120"> 

</icon:Generic_Icon> 

CS

public partial class Camera : Generic_Icon 
    { 

     public Camera() 
     { 
      InitializeComponent(); 
      //Title.Text = "Camera"; 
      //Uri imageUri = new Uri("/Assets/Icon Pack/Camera.png", UriKind.Relative); 
      //BitmapImage imageBitmap = new BitmapImage(imageUri); 
      //Img.Source = imageBitmap; 
     } 
     public override void Icon_button_Click(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show("clicked"); 
     } 
    } 

當我試圖把這個相機用戶控件在我的MainPage.xaml中,我得到這個錯誤 - 「無法分配財產「Microsoft.Phone.Controls .MenuItem.Click」。 [行:83的位置:53]

此行和位置使我Generic_Icon.xaml在這條線 - <toolkit:MenuItem Header="Remove" Click="Remove_Icon_Click"/>

我試圖此功能Remove_Icon_Click的參數從

public void Remove_Icon_Click(object sender, System.EventArgs e) 

更改爲

public void Remove_Icon_Click(object sender, RoutedEventArgs e) 

但兩者都給出相同的錯誤。 代碼不會編譯,但會在Generic_Icons的InitializeComponent()處的應用程序的開始處發出XamlParseError,並且內部異常與之前相同。

+0

注意,圖標如何變成UserControl?你不是在複雜化整個事情嗎? – Aybe

+0

@Aybe可能你是對的。但是我沒有足夠的經驗來繼承xaml +代碼。你能指出我正確的方向嗎?我無法找到任何有用的例子。 – Rishabh876

+0

解釋你想要達到的目標可能比你目前正在做的更簡單。 – Aybe

回答

0

編輯

所以,你在找什麼呢?

  • 底部的按鈕欄像在Android中一樣嗎?
  • 上下文菜單圖標?

關於繼承,

正如我所說的人會做到這一點與Control僅針對您的需求(對於派生類型設置圖像),它是真的不合適。在構造函數中設置圖像源也是一種不好的做法;你應該有1個控制桿和創建它的許多情況下,象下面這樣:

<StackPanel> 
    <phoneApp1:MyIconControl ImageSource="Icon1.png" /> 
    <phoneApp1:MyIconControl ImageSource="Icon2.png" /> 
</StackPanel> 

另外,您可以定義樣式爲每種類型的按鈕(更好的方式IMO)

所以給的是什麼,你的具體細節期待你的用戶界面和你期望的行爲,然後我會製作一些代碼。


首先,你不能從UserControl派生爲你做什麼,與Control你可以,但有了它,你是你自己(不XAML部分)。

我已將ImageSource依賴項屬性添加到將更新其中的圖像的控件。

XAML:

<UserControl x:Class="PhoneApp1.MyIconControl" 
      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" 
      xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
      FontFamily="{StaticResource PhoneFontFamilyNormal}" 
      FontSize="{StaticResource PhoneFontSizeNormal}" 
      Foreground="{StaticResource PhoneForegroundBrush}" 
      d:DesignHeight="480" 
      d:DesignWidth="480" 
      mc:Ignorable="d" 
      x:Name="UserControl1"> 
    <Grid Background="{StaticResource PhoneBackgroundBrush}"> 
     <toolkit:ContextMenuService.ContextMenu> 
      <toolkit:ContextMenu> 
       <toolkit:MenuItem Click="RemoveImageClick" Header="Remove image" /> 
       <toolkit:MenuItem Click="ReplaceImageClick" Header="Replace image" /> 
      </toolkit:ContextMenu> 
     </toolkit:ContextMenuService.ContextMenu> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <Image x:Name="Image1" 
       Margin="6" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center" /> 
     <TextBlock Grid.Row="1" 
        HorizontalAlignment="Center" 
        FontFamily="Segoe WP SemiLight" 
        FontSize="16" 
        Text="Camera" /> 
    </Grid> 
</UserControl> 

代碼:

using System.Windows; 
using System.Windows.Media; 

namespace PhoneApp1 
{ 
    public partial class MyIconControl 
    { 
     public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register(
      "ImageSource", typeof (ImageSource), typeof (MyIconControl), 
      new PropertyMetadata(default(ImageSource), OnImageSourceChanged)); 

     public MyIconControl() 
     { 
      InitializeComponent(); 
     } 

     public ImageSource ImageSource 
     { 
      get { return (ImageSource) GetValue(ImageSourceProperty); } 
      set { SetValue(ImageSourceProperty, value); } 
     } 

     private static void OnImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      var control = (MyIconControl) d; 
      var imageSource = e.NewValue as ImageSource; 
      control.Image1.Source = imageSource; 
     } 

     private void RemoveImageClick(object sender, RoutedEventArgs e) 
     { 
      ImageSource = null; 
     } 

     private void ReplaceImageClick(object sender, RoutedEventArgs e) 
     { 
      /* Here for instance you could open the pictures folder 
      * let the user choose one and set the ImageSource with it 
      * like ImageSource = new BitmapImage(new Uri(yourimage.png)); */ 
     } 
    } 
} 

用法:

<phoneApp1:MyIconControl ImageSource="Assets/ApplicationIcon.png" /> 

說明要在明確的條款來實現,上下文菜單的內容去除圖像沒有按對我沒有任何意義。什麼樣的應用程序。你正在嘗試做什麼?我會從那裏更新我的答案。

+0

下面是試圖實現的屏幕截圖。我已經實現了它,但沒有繼承。所以我仍然需要繼承。 http://imgur.com/k34PBKr(我不想分享它,但你真的想幫助,所以..)。有很多圖標。所有Xaml都有相同的圖像和名稱。當選擇上下文菜單項時,它們也會觸發相同的事件。它們都有一個函數,當這些圖標被點擊導航到相應的Xaml時,這些函數將被調用。 – Rishabh876

+0

看到我的編輯,等待您的評論。 – Aybe

相關問題