未能分配给属性';Microsoft.Phone.Controls.MenuItem.单击';
本文关键字:Phone Controls MenuItem 单击 Microsoft 属性 分配 | 更新日期: 2023-09-27 17:57:43
我第一次尝试继承用户控件,但遇到了很多错误。
这是基本用户控制-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"。[Line: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,内部异常与以前相同。
编辑
那么你在找什么呢?
- 底部的按钮栏像安卓系统一样
- 上下文菜单图标
关于继承,
正如我所说,只使用Control
,并且考虑到您的需要(为派生类型设置图像),这是非常不合适的。此外,在构造函数中设置图像源只是一种糟糕的做法;你应该坚持拥有1个控件,并创建许多这样的实例:
<StackPanel>
<phoneApp1:MyIconControl ImageSource="Icon1.png" />
<phoneApp1:MyIconControl ImageSource="Icon2.png" />
</StackPanel>
或者,您可以为每种类型的按钮定义样式(更好的是IMO)
因此,给出你在UI中期望的确切细节和你期望的行为,然后我会编写一些代码。
首先,你不能像你那样从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" />
清楚地解释你想要实现什么,删除图像的上下文菜单对我来说没有任何意义。什么样的应用程序。你想做什么?我会从那里更新我的答案