繁体   English   中英

Caliburn.micro和一个用户控件单击处理程序

[英]Caliburn.micro and a usercontrol click handler

我创建了一个非常简单的用户控件,一个ImageButton

<UserControl x:Class="SampleApp.Controls.ImageButton"
             Name="ImageButtonControl"
             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"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <Button>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" />
                <RowDefinition Height="6*" />
                <RowDefinition Height="2*" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>
            <Image Grid.Row="1" Source="{Binding ElementName=ImageButtonControl, Path=Image}" VerticalAlignment="Stretch" HorizontalAlignment="Center" />
            <TextBlock Grid.Row="2" Text="{Binding ElementName=ImageButtonControl, Path=Text}" VerticalAlignment="Stretch" HorizontalAlignment="Center" />
        </Grid>
    </Button>
</UserControl>

后面有代码:

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

namespace SampleApp.Controls
{
    /// <summary>
    /// Interaction logic for ImageButton.xaml
    /// </summary>
    public partial class ImageButton : UserControl
    {
        public ImageButton()
        {
            InitializeComponent();
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
          DependencyProperty.Register("Text", typeof(string), typeof(ImageButton), new UIPropertyMetadata(""));

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

        public static readonly DependencyProperty ImageProperty =
           DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null)); 
    }
}

现在,我想在我的小示例应用程序中使用它

xmlns:controls="clr-namespace:SampleApp.Controls"

<controls:ImageButton Grid.Row="1"
                              Grid.Column="1"
                              Margin="2"
                              Image="/Images/link.png"
                              Text="DoSomething">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <cal:ActionMessage MethodName="DoSomething" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </controls:ImageButton>

如果我给它ax:Name,就像

<controls:ImageButton x:Name="DoSomething" 

例如DoSomething的方法DoSomething与名称直接叫做显示视图时,即当我主动包含按钮,就像我按一下按钮(如果它是一个正常的按钮,而不是用户控件的视图模型,它的工作是方式),但从不调用按钮单击处理程序。

现在,我尝试添加一个如上所述的ActionMessage,但是它也不起作用...

怎么了

那是因为没有为您的用户控件类型配置任何约定。 您可以通过ConventionManager添加约定(请参见http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Conventions ),也可以从Button派生类型。

您也不能使用自定义用户控件,而只是将图像添加到视图中ButtonContent属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM