简体   繁体   English

处理基类中的ButtonClick

[英]Handle ButtonClick in Base Class

there is a small problem which I don't understand. 有一个我不明白的小问题。 I habe a BasePage (of type PhoneApplicationPage ) where I want to handle all my ButtonClicks. 我有一个BasePage (类型为PhoneApplicationPage ),我想在其中处理所有的ButtonClicks。

My BasePage class is nothing special, it just implements a Button_Click handler: 我的BasePage类没有什么特别的,它仅实现一个Button_Click处理程序:

using Microsoft.Phone.Controls;
using System.Windows;

namespace TestProject
{
    public partial class BasePage : PhoneApplicationPage
    {
        protected void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Button clicked");
        }
    }
}

The actual page looks like this: 实际页面如下所示:

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using TestProject.Resources;

namespace TestProject
{
    public partial class MainPage : BasePage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }
}

... and in the xaml i created a button which should be handled by the BasePage class: ...并在xaml中创建了一个按钮,该按钮应由BasePage类处理:

<local:BasePage
    x:Class="TestProject.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TestProject"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Button
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Content="Button"
            Click="Button_Click"/>

    </Grid>

</local:BasePage>

If I try to run the application I allways get an exception: 如果我尝试运行该应用程序,我总是会遇到异常:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll
An exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll but was not handled in user code
An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
An exception of type 'System.Reflection.TargetInvocationException' occurred in Microsoft.Phone.ni.dll and wasn't handled before a managed/native boundary
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Windows.Markup.XamlParseException: Failed to assign to property 'System.Windows.Controls.Primitives.ButtonBase.Click'. [Line: 23 Position: 19]
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at TestProject.MainPage.InitializeComponent()
   at TestProject.MainPage..ctor()
   --- End of inner exception stack trace ---
   at System.Windows.Navigation.PageResourceContentLoader.EndLoad(IAsyncResult asyncResult)
   at System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(IAsyncResult result)
An exception of type 'System.Reflection.TargetInvocationException' occurred in Microsoft.Phone.ni.dll and wasn't handled before a managed/native boundary
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll

If I add the implementation for the Button_Click also to the MainPage, then everything works find: 如果我还将Button_Click的实现也添加到MainPage,则一切正常,找到:

    new protected void Button_Click(object sender, RoutedEventArgs e)
    {
        base.Button_Click(sender, e);
    }

Can someone explain to me how I can handle the Button_Click only in the BasePage class (is it possible?) or what I am doing wrong? 有人可以向我解释我如何只能在BasePage类中处理Button_Click(可能吗?),或者我做错了什么?

Many thanks in advance ;) 提前谢谢了 ;)

Try to change the access level of Button_Click . 尝试更改Button_Click的访问级别。

Try this 尝试这个

public partial class BasePage : PhoneApplicationPage
{
    public void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Button clicked");
    }
}


 new public void Button_Click(object sender, RoutedEventArgs e)
 {
    base.Button_Click(sender, e);
 }

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

相关问题 处理基类异常 - Handle base class exception 如何在C#中处理基类构造函数? - How to handle base class constructors in C#? 派生类应该处理基类的事件吗? (C#/ WPF) - Should a derived class handle the events of a base class? (C# / WPF) 在控制台应用程序中处理行为,自定义功能等的基类? - Base Class to handle behavior, customize features, etc in Console Application? 具有抽象方法的基类如何用于处理委托调用? - How can a base class with abstract methods be used to handle delegate calls? 如何在实体框架中处理抽象基础 class(代码优先)? - How to to handle abstract base class in entity framework(code first)? 使用公共基类作为函数参数时,如何处理依赖类并避免类型检查? - How to handle dependent class and avoid type checking when using common base class as function parameter? 如果对象具有相同的基数 class,如何处理具有不同返回类型的方法? - How to handle methods with different return types if the objects have same base class? 如何使用可处理整数或字符串值的泛型创建抽象基类? - How can I create an abstract base class with a generic that can handle either an integer or string value? Asp.Net MVC如何通过buttonClick事件从类的实例调用方法 - Asp.Net MVC how to call a method from a instanc of a class by buttonClick event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM