2014-02-12 41 views
0

这是我第一次尝试使用WPF构建C#应用程序;我以前一直使用Windows Forms。我正在遇到一个似乎是一个简单任务的错误。当试图执行按钮的.IsEnabled属性的以下分配时,PresentationFramework.dll中会发生异常System.Reflection.TargetIncovationException。当我按Break时,我只是得到Source Not Available窗口,而选项ti查看反汇编信息。btnExample.IsEnabled赋值抛出WPF中的异常

private void txtFileLoc_TextChanged(object sender, TextChangedEventArgs e) { 
    if (txtFileLoc.Text.EndsWith(".txt")) 
     btnExecute.IsEnabled = true; 
    else 
     btnExecute.IsEnabled = false; 
} 

我已经验证它是通过将它们替换为Console.WriteLine来进行调试来引发异常。

编辑:按照要求,这里是程序XAML & CS

<Window x:Name="winMain" x:Class="IP_Extractor_2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="IP Extractor" Height="250" Width="410" ResizeMode="CanMinimize" Background="#FFECEFF4"> 
    <Grid x:Name="grdMain"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="39*"/> 
      <RowDefinition Height="161*"/> 
      <RowDefinition Height="21*"/> 
     </Grid.RowDefinitions> 
     <ProgressBar x:Name="prgProgress" Margin="0,0,0,0" Grid.Row="2" VerticalAlignment="Bottom" Height="17" Foreground="#FF7A6BA6" Value="50"/> 
     <TextBox x:Name="txtFileLoc" HorizontalAlignment="Left" Height="20" Margin="10,10,0,0" Text="Browse for a Text File" VerticalAlignment="Top" Width="290" TextChanged="txtFileLoc_TextChanged"/> 
     <Button x:Name="btnBrowse" Content="Browse" HorizontalAlignment="Left" Margin="305,10,0,0" VerticalAlignment="Top" Width="52" Height="20" Click="btnBrowse_Click"/> 
     <Button x:Name="btnExecute" Content="Go" HorizontalAlignment="Left" Margin="362,10,0,0" VerticalAlignment="Top" Width="22" Height="20" Click="btnExecute_Click" IsEnabled="False"/> 
     <ListBox x:Name="lboResults" HorizontalAlignment="Left" Height="151" Margin="10,0,0,0" Grid.Row="1" VerticalAlignment="Top" Width="374"/> 
    </Grid> 
</Window> 

CODE:

using Microsoft.Win32; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
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 IP_Extractor_2 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     OpenFileDialog openFileDialog1; 

     public MainWindow() { 
      InitializeComponent(); 
     } 

     private void btnBrowse_Click(object sender, RoutedEventArgs e) { 
      // Create an instance of the open file dialog box. 
      openFileDialog1 = new OpenFileDialog(); 

      // Set filter options and filter index. 
      openFileDialog1.Filter = "Text Files (.txt)|*.txt"; 
      openFileDialog1.FilterIndex = 1; 
      openFileDialog1.Multiselect = false; 

      // Call the ShowDialog method to show the dialog box. 
      bool? userClickedOK = openFileDialog1.ShowDialog(); 

      // Process input if the user clicked OK. 
      if (userClickedOK == true) 
       txtFileLoc.Text = openFileDialog1.FileName; 
     } 

     private void txtFileLoc_TextChanged(object sender, TextChangedEventArgs e) { 
      if (txtFileLoc.Text.EndsWith(".txt")) 
       btnExecute.IsEnabled = true; 
      else 
       btnExecute.IsEnabled = false; 
     } 
    } 
} 
+0

你在使用ui线程吗? –

+2

什么是InnerException和堆栈跟踪? – SLaks

+0

创建一个最小自包含程序来重现此问题,并将其发布。应该可能少于50行cs + xaml。因为这不应该发生,并且有些事情你没有发布。 –

回答

0

检查设置,看看有没有改变以前的事情空/空字符串。

if (btnExecute != null) 
{ 
    btnExecute.IsEnabled = ((txtFileLoc != null) && 
          (string.IsNullOrEmpty(txtFileLoc.Text) == false) && 
          (txtFileLoc.Text.ToLower().EndsWith(".txt"))); 
} 

编辑

虽然作品背后的代码,可能是做这件事的有效途径,它不是标准的方式。 WPF是关于绑定和INotifyProperty更改相关的事件。

我有我的按键绑定到视图模型布尔这将是对页面的数据上下文(这将通过按钮来继承),并结合诸如

<Button IsEnabled="{Binding IsExecuteButtonOperational }"/> 

我建议你开始考虑MVVM编程范例(将其视为分离业务逻辑表单视图逻辑关注点的方式)。我在我的博客Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding.上提供了一个例子,可以帮助您开始。

+0

不幸的是,抛出了同样的异常。 – Rawrcasm

+0

@Rawrcasm我逐字创建了你的代码并且遇到了两个错误。首先,当btnExecute尚未准备就绪时,它在初始化期间触发,因此我添加了if检查。其次是当一个文件有TXT或Txt命名时,所以我放入了ToLower。试试上面的代码,它适用于我。 – OmegaMan

+0

@Rawrcasm我的意思是说,在准备好您的回复后,我的代码段运行,我_then_创建。所以上面的代码是新的。 – OmegaMan