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

本文关键字:代码 找不到 对象 mainwindow xaml 添加 | 更新日期: 2023-09-27 17:57:05

我在一个新的 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# 相当陌生。

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

尝试将 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));
    }
}

您是否尝试过x:Name而不是Name