我試圖繼承一個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,並且內部異常與之前相同。
注意,圖標如何變成UserControl?你不是在複雜化整個事情嗎? – Aybe
@Aybe可能你是對的。但是我沒有足夠的經驗來繼承xaml +代碼。你能指出我正確的方向嗎?我無法找到任何有用的例子。 – Rishabh876
解釋你想要達到的目標可能比你目前正在做的更簡單。 – Aybe