簡體   English   中英

處理基類中的ButtonClick

[英]Handle ButtonClick in Base Class

有一個我不明白的小問題。 我有一個BasePage (類型為PhoneApplicationPage ),我想在其中處理所有的ButtonClicks。

我的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");
        }
    }
}

實際頁面如下所示:

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();
        }
    }
}

...並在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>

如果我嘗試運行該應用程序,我總是會遇到異常:

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

如果我還將Button_Click的實現也添加到MainPage,則一切正常,找到:

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

有人可以向我解釋我如何只能在BasePage類中處理Button_Click(可能嗎?),或者我做錯了什么?

提前謝謝了 ;)

嘗試更改Button_Click的訪問級別。

嘗試這個

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM