在代码中找不到添加到mainwindow.xaml的对象

时间:2013-02-03 02:10:51

标签: c# visual-studio-2010 visual-studio xaml reference

我在一个新的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#的新手。

2 个答案:

答案 0 :(得分: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));
    }
}

答案 1 :(得分:1)

您是否尝试x:Name代替Name