WPF項目在設(shè)計界面調(diào)用后臺代碼
更新時間:2022年04月21日 11:36:30 作者:農(nóng)碼一生
這篇文章介紹了WPF項目在設(shè)計界面調(diào)用后臺代碼的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
一、簡介
如下面代碼所示,在WPF項目的設(shè)計界面可以通過<x:Code> <![CDATA[ //write your code ]]></x:Code>節(jié)點,可添加后臺邏輯執(zhí)行的代碼和方法,實現(xiàn)方法的調(diào)用和執(zhí)行。
二、代碼
WPF設(shè)計部分代碼:
<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方法:
當然,此方法寫在設(shè)計界面里也可以,此處為了清晰的看到測試對比效果,前后端方法分開寫。
public void test()
{
string mes = "test message";
MessageBox.Show(mes);
}二、測試效果

到此這篇關(guān)于WPF項目在設(shè)計界面調(diào)用后臺代碼的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解ASP.NET與ASP.NET Core用戶驗證Cookie并存解決方案
本篇文章主要介紹了詳解ASP.NET與ASP.NET Core用戶驗證Cookie并存解決方案 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02

