日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

WPF項目在設計界面調用后臺代碼_實用技巧

作者:農碼一生 ? 更新時間: 2022-06-19 編程語言

一、簡介

如下面代碼所示,在WPF項目的設計界面可以通過<x:Code> <![CDATA[? //write your code?]]></x:Code>節點,可添加后臺邏輯執行的代碼和方法,實現方法的調用和執行。

二、代碼

WPF設計部分代碼:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="800" Width="500">

    <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
         <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="-150,-165,0,0" VerticalAlignment="Top" Width="85" Click="button_Click"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="-150,-75,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    </Grid>
  
    <x:Code>
        <!--此處可以寫后臺代碼-->
        <![CDATA[
         private void button_Click(object sender, RoutedEventArgs e)
        {
            test();
            string test_message = "dddddd";
            textBox.Text = test_message;
        }
   ]]>
    </x:Code>
</Window>

后臺的test方法:

當然,此方法寫在設計界面里也可以,此處為了清晰的看到測試對比效果,前后端方法分開寫。

        public void test()
        {
            string mes = "test message";
            MessageBox.Show(mes);
        }

二、測試效果

原文鏈接:https://www.cnblogs.com/wml-it/p/14885505.html

欄目分類
最近更新