0

这是我第一次尝试使用 WPF 构建 C# 应用程序;我过去一直使用 Windows 窗体。我遇到了一个看似简单的任务的错误。当尝试对按钮的.IsEnabled属性进行以下分配System.Reflection.TargetIncovationException时,PresentationFramework.dll 中出现异常。当我按下 Break 时,我只得到 Source Not Available 窗口和选项 ti 查看反汇编信息。

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>

代码:

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;
        }
    }
}
4

1 回答 1

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。这可以让你开始。

于 2014-02-12T22:52:11.717 回答