0

这是我的整个代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void sldChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        double red, green, blue;
        red = sldRed.Value;
        green = sldGreen.Value;
        blue = sldBlue.Value;
        changeColors(red, green, blue);
    }

    private void inputChanged(object sender, TextChangedEventArgs e)
    {
        double red, green, blue;
        red = Convert.ToDouble(txtRed.Text);
        green = Convert.ToDouble(txtGreen.Text);
        blue = Convert.ToDouble(txtBlue.Text);
        sldRed.Value = red;
        sldGreen.Value = green;
        sldBlue.Value = blue;
        changeColors(red, green, blue);
    }

    void changeColors(double red, double green, double blue)
    {

    }

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        txtRed.Text = Convert.ToString(sldRed.Value);
        txtGreen.Text = Convert.ToString(sldGreen.Value);
        txtBlue.Text = Convert.ToString(sldBlue.Value);
    }
}

这是我的整个 .xaml 代码。

<Grid x:Name="frmGrid" Margin="0,0,2,0">
    <Slider x:Name="sldRed" HorizontalAlignment="Left" Margin="57,10,0,0" VerticalAlignment="Top" Width="757" ValueChanged="sldChanged" ClipToBounds="True" Maximum="255" TickPlacement="BottomRight" IsSnapToTickEnabled="True" SmallChange="1" Value="1"/>
    <Slider x:Name="sldBlue" HorizontalAlignment="Left" Margin="57,76,0,0" VerticalAlignment="Top" Width="757" ValueChanged="sldChanged" ClipToBounds="True" Maximum="255" TickPlacement="BottomRight" IsSnapToTickEnabled="True" SmallChange="1" Value="1"/>
    <Slider x:Name="sldGreen" HorizontalAlignment="Left" Margin="57,45,0,0" VerticalAlignment="Top" Width="757" ValueChanged="sldChanged" ClipToBounds="True" Maximum="255" TickPlacement="BottomRight" IsSnapToTickEnabled="True" SmallChange="1" Value="1"/>
    <Label Content="Red" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
    <Label Content="Blue" HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top"/>
    <Label Content="Green" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="txtRed" HorizontalAlignment="Left" Height="23" Margin="819,9,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="41" TextChanged="inputChanged"/>
    <TextBox x:Name="txtBlue" HorizontalAlignment="Left" Height="23" Margin="819,74,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="41" TextChanged="inputChanged"/>
    <TextBox x:Name="txtGreen" HorizontalAlignment="Left" Height="23" Margin="819,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="41" TextChanged="inputChanged" Text="1"/>

</Grid>

您可以看到我已经为这些文本框分配了一个值以用于测试目的。

出于某种原因,我不断收到此错误 在此处输入图像描述

其中指的是这两行。

green = Convert.ToDouble(txtGreen.Text);
blue = Convert.ToDouble(txtBlue.Text);

请注意它上面的类似行如何完美地工作

red = Convert.ToDouble(txtRed.Text);

我还没有 C# 方面的经验,也没有阅读此类错误的经验。如果您对我收到此错误的原因有任何想法,并可能为我提供可能的解决方案,我们将不胜感激。

PS:我刚刚认为可能对您有用的东西,我什至在程序启动之前就收到了这个错误。

这是 2 个屏幕截图,其中 1 个根本没有加载,而第 2 个可以完美加载并且可以正常工作。 在此处输入图像描述 在此处输入图像描述

4

4 回答 4

2

txtRed and txtBlue不包含值。但 txtRed 包含值

您可以添加验证脚本以确保您的控件不为空

于 2012-10-11T18:12:27.720 回答
0

当输入更改时,事件处理程序会触发。如果您在任何一个框中按退格键,您的 Text 现在是一个空字符串或 null。你不能把它们变成双打。尝试使用 Double.TryParse() 而不是 Convert.ToDouble() 来确定它是否实际上是可解析的输入,如下所示:

private void inputChanged(object sender, TextChangedEventArgs e)
{
    double red, green, blue;

    if(Double.TryParse(txtRed.Text, out red) &&
        Double.TryParse(txtGreen.Text, out green) &&
        Double.TryParse(txtBlue.Text, out blue)) 
    {
        sldRed.Value = red;
        sldGreen.Value = green;
        sldBlue.Value = blue;
        changeColors(red, green, blue);
    }
}

如果 RGB 文本框中的所有值都有效,则 if 语句将为 true,并且红色、绿色和蓝色将被正确设置。

这当然是假设 sldGreen 和 sldBlue 不为空。

于 2012-10-11T18:29:10.813 回答
0

失败的原因是,当您最初在 xaml 中创建滑块并包含以下内容时会调用 valuechanged 事件:Value="1"。该事件在创建其他控件之前引发,因此是空异常。

WPF 在很大程度上建立在绑定的概念之上,你应该尽可能多地利用它,因为它会让你的生活变得更轻松。我更改了您的代码以向您展示如何使用绑定来执行此操作。

xml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        x:Name="TheMainWindow">
    <Grid x:Name="frmGrid" Margin="0,0,2,0">
        <Slider x:Name="sldRed" HorizontalAlignment="Left" Margin="57,10,0,0" VerticalAlignment="Top" Width="757" ClipToBounds="True" Maximum="255" TickPlacement="BottomRight" IsSnapToTickEnabled="True" 
                SmallChange="1" Value="{Binding Path=Red, ElementName=TheMainWindow, Mode=TwoWay}"/>
        <Slider x:Name="sldBlue" HorizontalAlignment="Left" Margin="57,76,0,0" VerticalAlignment="Top" Width="757" ClipToBounds="True" Maximum="255" TickPlacement="BottomRight" IsSnapToTickEnabled="True" 
                SmallChange="1" Value="{Binding Path=Blue, ElementName=TheMainWindow, Mode=TwoWay}"/>
        <Slider x:Name="sldGreen" HorizontalAlignment="Left" Margin="57,45,0,0" VerticalAlignment="Top" Width="757" ClipToBounds="True" Maximum="255" TickPlacement="BottomRight" IsSnapToTickEnabled="True"
                SmallChange="1" Value="{Binding Path=Green, ElementName=TheMainWindow, Mode=TwoWay}"/>
        <Label Content="Red" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <Label Content="Blue" HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top"/>
        <Label Content="Green" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtRed" HorizontalAlignment="Left" Height="23" Margin="819,9,0,0" TextWrapping="Wrap" Text="{Binding Path=Red, ElementName=TheMainWindow}" VerticalAlignment="Top" Width="41"/>
        <TextBox x:Name="txtBlue" HorizontalAlignment="Left" Height="23" Margin="819,74,0,0" TextWrapping="Wrap" Text="{Binding Path=Blue, ElementName=TheMainWindow}" VerticalAlignment="Top" Width="41" />
        <TextBox x:Name="txtGreen" HorizontalAlignment="Left" Height="23" Margin="819,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top"  Text="{Binding Path=Green, ElementName=TheMainWindow}" Width="41" />
    </Grid>
</Window>

代码

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private double _red = 1;
    public double Red
    {
        get { return _red; }
        set
        {
            _red = value;
            OnPropertyChanged("Red");
        }
    }

    private double _green = 1;
    public double Green
    {
        get { return _green; }
        set
        {
            _green = value;
            OnPropertyChanged("Green");
        }
    }

    private double _blue = 1;
    public double Blue
    {
        get { return _blue; }
        set
        {
            _blue = value;
            OnPropertyChanged("Blue");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
于 2012-10-11T21:13:28.393 回答
0

我认为问题在于您的Window_Loaded_1它会更改文本框的文本值,从而导致inputChanged调用该方法。尝试注释掉代码Window_Loaded_1,看看异常是否仍然发生。

解决此问题的一种方法是使用绑定将滑块和文本框的值连接在一起。您的 xaml 应如下所示:

<Grid x:Name="frmGrid" Margin="0,0,2,0">
    <Slider x:Name="sldRed" HorizontalAlignment="Left" Margin="57,10,0,0" VerticalAlignment="Top" Width="757" ValueChanged="sldChanged" ClipToBounds="True" Maximum="255" TickPlacement="BottomRight" IsSnapToTickEnabled="True" SmallChange="1" Value="1"/>
    <Slider x:Name="sldBlue" HorizontalAlignment="Left" Margin="57,76,0,0" VerticalAlignment="Top" Width="757" ValueChanged="sldChanged" ClipToBounds="True" Maximum="255" TickPlacement="BottomRight" IsSnapToTickEnabled="True" SmallChange="1" Value="1"/>
    <Slider x:Name="sldGreen" HorizontalAlignment="Left" Margin="57,45,0,0" VerticalAlignment="Top" Width="757" ValueChanged="sldChanged" ClipToBounds="True" Maximum="255" TickPlacement="BottomRight" IsSnapToTickEnabled="True" SmallChange="1" Value="1"/>
    <Label Content="Red" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
    <Label Content="Blue" HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top"/>
    <Label Content="Green" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top"/>

    <TextBox x:Name="txtRed" HorizontalAlignment="Left" Height="23" Margin="819,9,0,0" TextWrapping="Wrap" Text="{Binding ElementName=sldRed, Path=Value, Mode=TwoWay}" VerticalAlignment="Top" Width="41"/>

    <TextBox x:Name="txtBlue" HorizontalAlignment="Left" Height="23" Margin="819,74,0,0" TextWrapping="Wrap" Text="{Binding ElementName=sldBlue, Path=Value, Mode=TwoWay}" VerticalAlignment="Top" Width="41"/>

    <TextBox x:Name="txtGreen" HorizontalAlignment="Left" Height="23" Margin="819,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="41" Text="{Binding ElementName=sldGreen, Path=Value, Mode=TwoWay}"/>


</Grid>

你的代码应该是这样的:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void sldChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        red = sldRed.Value;
        green = sldGreen.Value;
        blue = sldBlue.Value;
        changeColors(red, green, blue);
    }

    void changeColors(double red, double green, double blue)
    {

    }
}

您可以摆脱inputChanged并仅响应滑块更改

于 2012-10-11T20:19:07.870 回答