Windows Phone 8:将事件处理程序移动到基本视图类中。

时间:2022-06-14 00:05:18

I have a number of views in my Windows Phone 8 app that share a lot of similarity, so I created a base class to contain the common logic. In general, this approach has worked great, but with one limitation - I am unable to move the common event handling logic into the base class. Here is the simplified version of what I am trying to achieve:

我的Windows Phone 8应用程序中有很多视图,它们有很多相似之处,所以我创建了一个基类来包含公共逻辑。通常,这种方法非常有效,但是有一个限制——我无法将公共事件处理逻辑移动到基类中。以下是我想要达到的目标的简化版本:

  1. The base class defines a method which my sub-classes should use to handle a button click event in their respective application bars.

    基类定义了一个方法,我的子类应该使用这个方法在它们各自的应用程序条中处理按钮单击事件。

    public class BaseView : PhoneApplicationPage { protected void OnButtonClick(object sender, EventArgs e) { MessageBox.Show("Button Pressed"); } }

    公共类BaseView: PhoneApplicationPage {protected void OnButtonClick(object sender, EventArgs e) {MessageBox。显示(“按钮按下”);} }

  2. The XAML for the inheriting view looks like this (showing only the relevant part). Note the ApplicationBarIconButton's event handler. My intention is that the OnButtonClick method, defined in the base view, is called.

    继承视图的XAML如图所示(只显示相关部分)。注意ApplicationBarIconButton的事件处理程序。我的目的是调用在基本视图中定义的OnButtonClick方法。

    <base:BaseView x:Class="EventHandlersInBaseClassBugRepro.MainPage" xmlns:base ="clr-namespace:EventHandlersInBaseClassBugRepro" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" ..... >

    <phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar IsMenuEnabled="False"> <shell:ApplicationBarIconButton IconUri="/Assets/Tiles/FlipCycleTileSmall.png" Text="Click" Click="OnButtonClick"/> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar>

    <电话:phoneapplicationpage。

The application compiles, but at runtime I am getting this exception:

应用程序编译,但是在运行时我得到了这个异常:

 {System.Windows.Markup.XamlParseException: Failed to assign to property     'Microsoft.Phone.Shell.ApplicationBarIconButton.Click'. [Line: 22 Position: 23]
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at EventHandlersInBaseClassBugRepro.MainPage.InitializeComponent()
   at EventHandlersInBaseClassBugRepro.MainPage..ctor()}

I saw a similar question here Silverlight: Can I set an event to a base class event handler in XAML?. My interpretation of the answer suggests that the approach I am using should work, yet the exception. Is what I am trying to do possible?

我在这里看到了一个类似的问题:我可以在XAML中将事件设置为基类事件处理程序吗?我对答案的解释表明,我所使用的方法应该有效,但也有例外。我想做的事情可能吗?

1 个解决方案

#1


0  

Remember Events are a delegate that should only be invoked in the class that declared them. In your scenario you havent declared an Event in the BaseView. Also mark the OnButtonClick invoke method as Virtual so that it can be overridden in derived classes. Try

请记住,Events是一个委托,应该只在声明它们的类中调用它。在您的场景中,您没有在BaseView中声明事件。还可以将OnButtonClick调用方法标记为Virtual,以便在派生类中重写它。试一试

public class BaseView : PhoneApplicationPage
{
    public event EventHandler ButtonClicked;

    public virtual void OnButtonClick(object sender, EventArgs e)
    {
        MessageBox.Show("Base Button Click");
    }
}

In your derived class .cs you will need to override OnButtonClick like this

在派生类.cs中,需要像这样覆盖OnButtonClick

    public override void OnButtonClicked(object sender, EventArgs e)
    {
        // do derived class logic here
        MessageBox.Show("Derived class hit");
        // calls BaseView event invoke method
        base.OnButtonClicked(sender, e);
    }

Hope that helps

希望这有助于

#1


0  

Remember Events are a delegate that should only be invoked in the class that declared them. In your scenario you havent declared an Event in the BaseView. Also mark the OnButtonClick invoke method as Virtual so that it can be overridden in derived classes. Try

请记住,Events是一个委托,应该只在声明它们的类中调用它。在您的场景中,您没有在BaseView中声明事件。还可以将OnButtonClick调用方法标记为Virtual,以便在派生类中重写它。试一试

public class BaseView : PhoneApplicationPage
{
    public event EventHandler ButtonClicked;

    public virtual void OnButtonClick(object sender, EventArgs e)
    {
        MessageBox.Show("Base Button Click");
    }
}

In your derived class .cs you will need to override OnButtonClick like this

在派生类.cs中,需要像这样覆盖OnButtonClick

    public override void OnButtonClicked(object sender, EventArgs e)
    {
        // do derived class logic here
        MessageBox.Show("Derived class hit");
        // calls BaseView event invoke method
        base.OnButtonClicked(sender, e);
    }

Hope that helps

希望这有助于