我是第一次尝试继承用户控件,但一直面临很多错误。
以下是 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);
}
}
派生的UserControl相机 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,内部异常与之前相同。
答案 0 :(得分:0)
修改强>
那你在找什么?
关于继承,
正如我所说的那样只有Control
并且根据您的需要(为派生类型设置图像)才会这样做,真的不合适。在构造函数中设置图像源也是一种不好的做法;你应该坚持使用1控制并创建它的许多实例,如下所示:
<StackPanel>
<phoneApp1:MyIconControl ImageSource="Icon1.png" />
<phoneApp1:MyIconControl ImageSource="Icon2.png" />
</StackPanel>
或者,您可以为每种类型的按钮定义样式(更好的IMO方式)
因此,请详细说明您在UI中所期望的内容以及您期望的行为,然后我会制作一些代码。
首先,你不能像UserControl
那样派生Control
,你可以使用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>
依赖项属性,该属性将更新其中的图像。
XAML:
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" />
用法:
{{1}}
用清晰的术语解释您想要实现的目标,删除图像的上下文菜单对我没有任何意义。什么样的应用程序。你想做什么?我会从那里更新我的答案。