這是我第一次嘗試使用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;
}
}
}
你在使用ui線程嗎? –
什麼是InnerException和堆棧跟蹤? – SLaks
創建一個最小自包含程序來重現此問題,並將其發佈。應該可能少於50行cs + xaml。因爲這不應該發生,並且有些事情你沒有發佈。 –