1

我在一个新的 C# 项目的主窗口中添加了一个 Image 对象

<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">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="150*" />
        <RowDefinition Height="161*" />
    </Grid.RowDefinitions>
    <Image Grid.RowSpan="2" Height="240" HorizontalAlignment="Left" Margin="80,26,0,0"     Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="320" DataContext="{Binding     ElementName=image1}" />
    <TextBox Grid.Row="1" Height="25" HorizontalAlignment="Left" Margin="155,119,0,0"     Name="kinectStatusTB" VerticalAlignment="Top" Width="111" Text="Disconnected" DataContext="    {Binding}" />
    <TextBlock Grid.Row="1" Height="18" HorizontalAlignment="Left" Margin="80,122,0,0"     Name="textBlock1" Text="Kinect Status" VerticalAlignment="Top" Width="69" />
    </Grid>
</Window>

我在 .cs 代码中的这一行出现错误,提示“当前上下文中不存在名称‘image1’”

 image1.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height,
                96, 96, //DPI
                PixelFormats.Bgr32, //format
                null,
                pixels, //where the data is stored
                stride);

我不知道我是否做错了,我对 C# 还很陌生。

4

2 回答 2

1

你试过x:Name代替Name吗?

于 2013-02-03T09:17:34.037 回答
1

尝试将您的 ImageSource 绑定到 UI 上的属性,它比在代码中引用控件更好。

例子:

<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" Name="UI">
 <Grid DataContext="{Binding ElementName=UI}">
        <Grid.RowDefinitions>
            <RowDefinition Height="150*" />
            <RowDefinition Height="161*" />
        </Grid.RowDefinitions>
        <Image Source="{Binding MyImageSource}" Stretch="Fill" Grid.RowSpan="2" Width="320" Height="240" Margin="80,26,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />
        <TextBox Grid.Row="1" Height="25" HorizontalAlignment="Left" Margin="155,119,0,0"  Name="kinectStatusTB" VerticalAlignment="Top" Width="111" Text="Disconnected" />
        <TextBlock Grid.Row="1" Height="18" HorizontalAlignment="Left" Margin="80,122,0,0"  Name="textBlock1" Text="Kinect Status" VerticalAlignment="Top" Width="69" />
    </Grid>

</Window>

代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private ImageSource _myImageSource;

    public MainWindow()
    {
        InitializeComponent();
    }

    public ImageSource MyImageSource
    {
        get { return _myImageSource; }
        set { _myImageSource = value; NotifyPropertyChanged("MyImageSource"); }
    }

    private void SetImage()
    {
        // Your logic

        MyImageSource = BitmapSource.Create(colorFrame.Width, colorFrame.Height,
          96, 96, //DPI
          PixelFormats.Bgr32, //format
          null,
          pixels, //where the data is stored
          stride);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
于 2013-02-03T02:49:33.957 回答