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>

 

資源文件:this

處理代碼邏輯spa

/// <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));//開始導航
        }
相關文章
相關標籤/搜索