2013-09-25 2 views
0

내가 이해하지 못하는 작은 문제가 있습니다. 나는 내 모든 ButtonClicks를 처리하고자하는 (타입이 PhoneApplicationPage 인) habe입니다.기본 클래스의 ButtonClick 처리

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

실제 페이지는 다음과 같습니다 :

내베이스 페이지 클래스는 특별한 아무것도 아니다, 그냥 Button_Click 핸들러 구현

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> 

응용 프로그램을 실행하려고하면 나는에 allways 예외 얻을에 : 나는 MainPage에 또한 Button_Click에 대한 구현을 추가하는 경우

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 

을 한 후 모든 작품은 찾을 수 :

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

누군가가 내가 처리 할 수있는 방법을 나에게 설명 할 수 Button_Click은 BasePage 클래스에서만 가능합니다 (가능합니까?) 또는 내가 잘못하고있는 것은 무엇입니까? 사전에

많은 감사)

답변

0

봅니다 Button_Click의 액세스 수준을 변경합니다. ** 그런데 왜 내가해야합니까 -

나는 MainPage에 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); 
} 
+0

변화를 시도 'MainPage'에서'Button_Click'을 구현하려면 기본 클래스 ('BasePage')에서 파생시킬 수 없습니까? ** –