这是我第一次尝试使用WPF构建C#应用程序;我以前一直使用Windows Forms。我遇到了一个错误,似乎是一个简单的任务。尝试以下分配按钮的.IsEnabled
属性时,PresentationFramework.dll中会出现异常System.Reflection.TargetIncovationException
。当我按下Break时,我只获得Source Not Available窗口和选项ti view disassembly information。
private void txtFileLoc_TextChanged(object sender, TextChangedEventArgs e) {
if (txtFileLoc.Text.EndsWith(".txt"))
btnExecute.IsEnabled = true;
else
btnExecute.IsEnabled = false;
}
我已经通过将Console.WriteLine替换为调试来验证是抛出异常的赋值。
编辑:根据要求,这是XAML& amp; 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 :(得分:0)
在设置之前检查null / empty字符串,看看是否会改变。
if (btnExecute != null)
{
btnExecute.IsEnabled = ((txtFileLoc != null) &&
(string.IsNullOrEmpty(txtFileLoc.Text) == false) &&
(txtFileLoc.Text.ToLower().EndsWith(".txt")));
}
修改强>
尽管代码背后有效并且可能是一种有效的方法,但它并不是标准方法。 WPF是关于绑定和INotifyProperty改变事件相关的。
我希望我的按钮绑定到一个View Model Boolean,它将位于页面的数据上下文中(可以通过按钮继承)并绑定,例如
<Button IsEnabled="{Binding IsExecuteButtonOperational }"/>
我建议您开始研究MVVM编程范例(将其视为分离业务逻辑形式视图逻辑的关注点的方式)。我在我的博客Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding.上提供了一个示例,可以帮助您入门。