WPF一段代码搞定所有的点击事件

 

 

用一个方法来控制转发所有的点击事件,界面图如下:

 

  

<Page x:Class="WPFNavigate.Index"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Home">
    <Page.Resources>
        <Style TargetType="Button">
            <Setter Property="Opacity" Value="1"/>
            <Setter Property="Width" Value="40"/>
            <Setter Property="Height" Value="40"/>
        </Style>
    </Page.Resources>
    <!--设置背景图片-->
    <Page.Background>
        <ImageBrush ImageSource="Resources\Phone\Home3.png"  Stretch="Fill" />
        <!--Stretch伸展-->
    </Page.Background>
    <StackPanel Name="MyHome" ButtonBase.Click="MyHome_Click">
        <Canvas Height="400" Width="240">
            <!--时钟-->
            <Button Opacity="0"  x:Name="Colck"  Canvas.Left="10" Canvas.Top="20" />
            <!--日历-->
            <Button Opacity="0"  x:Name="RiLi"  Canvas.Left="70" Canvas.Top="22" />
            <!--音乐-->
            <Button Opacity="0"  x:Name="Muesic"  Canvas.Left="130" Canvas.Top="22" />
            <!--网络-->
            <Button Opacity="0"  x:Name="Web"  Canvas.Left="190" Canvas.Top="22" />
            <!--相机-->
            <Button Opacity="0"  x:Name="Camary"  Canvas.Left="10" Canvas.Top="90" />
            <!--邮件-->
            <Button Opacity="0"  x:Name="Email"  Canvas.Left="70" Canvas.Top="90" />
            <!--相册-->
            <Button Opacity="0"  x:Name="photos"  Canvas.Left="130" Canvas.Top="90" />
            <!--搜索-->
            <Button Opacity="0"  x:Name="Seching"  Canvas.Left="190" Canvas.Top="90" />
            <!--录像-->
            <Button Opacity="0"  x:Name="Viodue"  Canvas.Left="10" Canvas.Top="165" Click="Viodue_Click" />
            <!--收音机-->
            <Button Opacity="0"  x:Name="FM"  Canvas.Left="70" Canvas.Top="165" />
            <!---->
        <Button Opacity="0"  x:Name="AsmsungApp"  Canvas.Left="130" Canvas.Top="165" />
        <!--设置-->
            <Button Opacity="0"  x:Name="Stetting"  Canvas.Left="190" Canvas.Top="165" />
            <!--社交-->
            <Button Opacity="0"  x:Name="Cluble"  Canvas.Left="10" Canvas.Top="235" />
            <!--WebViolue-->
            <Button Opacity="0"  x:Name="WebViolu"  Canvas.Left="70" Canvas.Top="235" />
            <!---->
        <Button Opacity="0"  x:Name="Read"  Canvas.Left="130" Canvas.Top="235" />
        <Button Opacity="0"  x:Name="backNode"  Canvas.Left="190" Canvas.Top="235" />
         <Button Opacity="0"  x:Name="Call"  Canvas.Left="10" Canvas.Top="350" />
        <!--WebViolue-->
            <Button Opacity="0"  x:Name="TongXunlu"  Canvas.Left="70" Canvas.Top="350" />
            <!---->
        <Button Opacity="0"  x:Name="Messing"  Canvas.Left="130" Canvas.Top="350" />
     
        <Button Opacity="0"  x:Name="Home"  Canvas.Left="190" Canvas.Top="350" />
    </Canvas>
    </StackPanel>
</Page>

 

资源文件:

处理代码逻辑

/// <summary>
        ///  点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyHome_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)e.OriginalSource;//获取点击的Button
            Type type = this.GetType();//得到他的类型
            Assembly assenbly = type.Assembly;//程序集的信息
            Page page = (Page)assenbly.CreateInstance(type.Namespace + "." + btn.Name);//创建一个实例
            NavigationService.Navigate(new Uri(btn.Name + ".xaml", UriKind.Relative));//开始导航
        }

 

posted @ 2016-07-23 10:43  清风小筑  阅读(9084)  评论(3编辑  收藏  举报