【发布时间】:2014-04-09 21:44:41
【问题描述】:
我第一次尝试继承用户控件,但遇到了很多错误。
这里是 Base UserControl - 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);
}
}
派生的用户控制摄像头 XAML
<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");
}
}
当我尝试将此 Camera UserControl 放入 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 也许你是对的。但我在继承 xaml + 代码方面没有足够的经验。你能指出我正确的方向吗?我找不到任何有用的例子。
-
解释你想要达到的目标,可能有一种比你目前正在做的更简单的方法。
-
@Aybe 我想制作一个名为 Generic_Icons 的模板 UserControl。其 XAML 与上述相同,因为从它派生的所有图标都将具有相同的 Xaml。有两个事件(删除、替换)也将被继承。单击 ContextMenu 项目时会触发它们。一个函数 Icon_button_Click 也被继承,但将在所有基类中被覆盖。在基类构造函数中更改了图标图像和图标标题
-
我的意思是,这个的最终用途是什么?目前,您在 Button 和 ContextMenu 中有一个 Image,但为什么呢?您正在寻找什么样的互动方式?
标签: c# xaml windows-phone-8