当前位置:   article > 正文

WPF 如何调试

WPF 如何调试

简述
它是一种系统机制,用于识别和修复一段代码中的错误或缺陷,这些错误或缺陷的行为与您的预期不同。调试子系统紧密耦合的复杂应用程序并不容易,因为修复一个子系统中的错误可能会在另一个子系统中创建错误。
在 C# 中调试
WPF 应用程序中,程序员处理两种语言,例如 C# 和 XAML。如果您熟悉使用任何过程语言(例如 C# 或 C/C++)进行调试,并且还知道断点的用法,那么您可以轻松地调试应用程序的 C# 部分。
让我们举一个简单的例子来演示如何调试 C# 代码。创建一个名为WPFDebuggingDemo的新 WPF 项目。从工具箱中拖动四个标签、三个文本框和一个按钮。查看以下 XAML 代码。

<Window x:Class = "WPFDebuggingDemo.Window1" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "Window1" Height = "400" Width = "604"> 
   
   <Grid> 
      <TextBox Height = "23" Margin = "0,44,169,0" Name = "textBox1"  
         VerticalMoognment = "Top" HorizontalMoognment = "Right" Width = "120" /> 
         
      <TextBox Height = "23" Margin = "0,99,169,0" Name = "textBox2"  
         VerticalMoognment = "Top" HorizontalMoognment = "Right" Width = "120" /> 
         
      <TextBox HorizontalMoognment = "Right" Margin = "0,153,169,0"  
         Name = "textBox3" Width = "120" Height = "23" VerticalMoognment = "Top" /> 
         
      <Label Height = "28" Margin = "117,42,0,0" Name = "label1"  
         VerticalMoognment = "Top" HorizontalMoognment = "Left" Width = "120">
         Item 1</Label> 
         
      <Label Height = "28" HorizontalMoognment = "Left"  
         Margin = "117,99,0,0" Name = "label2" VerticalMoognment = "Top" Width = "120">
         Item 2</Label> 
         
      <Label HorizontalMoognment = "Left" Margin = "117,153,0,181"  
         Name = "label3" Width = "120">Item 3</Label>
         
      <Button Height = "23" HorizontalMoognment = "Right" Margin = "0,0,214,127"
         Name = "button1" VerticalMoognment = "Bottom" Width = "75"  
         Click = "button1_Click">Total</Button> 
         
      <Label Height = "28" HorizontalMoognment = "Right"  
         Margin = "0,0,169,66" Name = "label4" VerticalMoognment = "Bottom" Width = "120"/> 
         
   </Grid> 
   
</Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

下面给出的是实现按钮单击事件的 C# 代码。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text;
 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes;  
namespace WPFDebuggingDemo { 
   /// <summary> 
      /// Interaction logic for Window1.xaml 
   /// </summary> 
   
   public partial class Window1 : Window { 
   
      public Window1() {
         InitializeComponent();
      }
      
      private void button1_Click(object sender, RoutedEventArgs e) {
      
         if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0 && textBox2.Text.Length > 0) {
            double total = Convert.ToDouble(textBox1.Text) + 
            Convert.ToDouble(textBox2.Text) + Convert.ToDouble(textBox3.Text); 
            label4.Content = total.ToString(); 
         } 
         else { 
            MessageBox.Show("Enter the value in all field.");
         } 
      } 
   } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

当你编译并执行上面的代码时,它会产生如下的窗口。现在在文本框中输入值,然后按总计按钮。在对文本框中输入的所有值求和后,您将获得总值。
调试
如果您尝试输入非真实值的值,则上述应用程序将崩溃。要查找并解决问题(为什么会崩溃),您可以在按钮单击事件中插入断点。
让我们在第 1 项中写上“abc”,如下所示。
项目 1
单击 Total 按钮后,您将看到程序在断点处停止。
程序崩溃
现在将光标移向 textbox1.Text,您将看到程序正在尝试将abc值与其他值相加,这就是程序崩溃的原因。
XAML 中的调试
如果您希望在 XAML 中进行相同类型的调试,那么您会惊讶地发现,还不能像调试任何其他过程语言代码那样调试 XAML 代码。当您在 XAML 代码中听到术语调试时,它意味着尝试查找错误。
在数据绑定中,您的数据没有显示在屏幕上,您不知道为什么
或者一个问题与复杂的布局有关。
或者使用一些扩展模板(如 ListBox 和组合框)出现对齐问题或边距颜色、覆盖等问题。
调试 XAML 程序通常是为了检查绑定是否有效;如果它不工作,然后检查什么是错的。不幸的是,除了 Silverlight 之外,无法在 XAML 绑定中设置断点,但我们可以使用“输出”窗口来检查数据绑定错误。让我们看一下下面的 XAML 代码来查找数据绑定中的错误。

<Window x:Class = "DataBindingOneWay.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "350" Width = "604"> 
   
   <Grid> 
      <StackPanel Name = "Display"> 
         <StackPanel Orientation = "Horizontal" Margin = "50, 50, 0, 0"> 
            <TextBlock Text = "Name: " Margin = "10" Width = "100"/> 
            <TextBlock Margin = "10" Width = "100" Text = "{Binding FirstName}"/> 
         </StackPanel> 
         
         <StackPanel Orientation = "Horizontal" Margin = "50,0,50,0"> 
            <TextBlock Text = "Title: " Margin = "10" Width = "100"/> 
            <TextBlock Margin = "10" Width = "100" Text = "{Binding Title}" /> 
         </StackPanel> 
         
      </StackPanel> 
   </Grid> 
   
</Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

两个文本块的文本属性被静态设置为“姓名”和“职务”,而其他两个文本块的文本属性绑定到“名字”和“职务”,但类变量是 Employee 类中的名称和职务,如下所示。
我们故意写了一个不正确的变量名,以便了解在未显示所需输出时在哪里可以找到这种类型的错误。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks;  
namespace DataBindingOneWay { 
   public class Employee { 
      public string Name { get; set; } 
      public string Title { get; set; }  
      
      public static Employee GetEmployee() { 
      
         var emp = new Employee() { 
            Name = "Moo Ahmed", Title = "Developer" 
         }; 
         
         return emp; 
      }  
   } 
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

这是 C# 代码中 MainWindow 类的实现。

using System; 
using System.Windows; 
using System.Windows.Controls; 
 
namespace DataBindingOneWay { 
   /// <summary> 
      /// Interaction logic for MainWindow.xaml
   /// </summary> 
   
   public partial class MainWindow : Window { 
   
      public MainWindow() { 
         InitializeComponent(); 
         DataContext = Employee.GetEmployee(); 
      } 
   } 
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

让我们运行这个应用程序,您可以立即在 MainWindow 中看到我们已成功绑定到该 Employee 对象的 Title,但名称未绑定。
XAML 中的调试
要检查名称发生了什么,让我们查看生成大量日志的输出窗口。
很容易找到错误只是搜索错误,你会发现以下错误,上面写着“BindingExpression path error: ‘FirstName’ property not found on ‘object’ ''Employe”

System.Windows.Data Error: 40 : BindingExpression path error: ‘FirstName’
property not found on ‘object’ ‘‘Employee’ (HashCode=11611730)’.
BindingExpression:Path = FirstName; DataItem = ‘Employee’ (HashCode = 11611730);
target element is ‘TextBlock’ (Name=‘’); target property is ‘Text’ (type ‘String’)
这清楚地表明 FirstName 不是 Employee 类的成员,因此它有助于在您的应用程序中解决此类问题。
当您再次将 FirstName 更改为 Name 时,您将看到所需的输出。
XAML 的 UI 调试工具
使用 Visual Studio 2015 为 XAML 引入了 UI 调试工具,以在运行时检查 XAML 代码。在这些工具的帮助下,XAML 代码以正在运行的 WPF 应用程序的可视化树以及树中不同的 UI 元素属性的形式呈现。要启用这些工具,请按照以下步骤操作。
转到工具菜单并从工具菜单中选择选项。
它将打开以下对话框。
调试工具
转到左侧调试项下的常规选项。
勾选突出显示的选项,即“为 XAML 启用 UI 调试工具”,然后单击“确定”按钮。
现在运行任何 XAML 应用程序或使用以下 XAML 代码。

<Window x:Class = "XAMLTestBinding.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "350" Width = "604"> 
   
   <StackPanel> 
      <ComboBox Name = "comboBox"  Margin = "50" Width = "100"> 
         <ComboBoxItem Content = "Green" /> 
         <ComboBoxItem  Content = "Yellow" IsSelected = "True" />
         <ComboBoxItem Content = "Orange" /> 
      </ComboBox> 
      
      <TextBox  Name = "textBox" Margin = "50" Width = "100" Height = "23"
         VerticalMoognment = "Top" Text  =
         "{Binding ElementName = comboBox, Path = SelectedItem.Content, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" 
         Background = "{Binding ElementName = comboBox, Path = SelectedItem.Content}"> 
      </TextBox> 
      
   </StackPanel> 
   
</Window> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

当您执行应用程序时,它将显示实时可视树,其中所有元素都显示在树中。
实时视觉树
此 Live Visual Tree 显示了完整的布局结构,以了解 UI 元素的位置。但此选项仅在 Visual Studio 2015 中可用。如果您使用的是 Visual Studio 的较旧选项,则无法使用此工具,但是还有另一个可以与 Visual Studio 集成的工具,例如 XAML Spy for Visual Studio .

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/642351
推荐阅读
相关标签
  

闽ICP备14008679号