我是WPF,Visual Studio和C#的新手。我用java编写了代码(虽然已经有一段时间了),所以很多C#代码看起来都很熟悉。我正在尝试测试此处列出的代码:http://www.c-sharpcorner.com/uploadfile/mahesh/image-viewer-in-wpf/
现在,当我将代码复制到那里使用时,窗口和按钮使用.xaml代码显示正常。但是,在后面的代码中,我得到8个错误,都说“当前上下文中不存在名称'xxx'”。它们是:InitializeComponent,ImageViewer1,FileNameLabel,selectedFileName,RotationList和ImageControl。如果下载文件,则.xaml文件不同,仅使用BrowseButton_Click方法。使用这两个文件,我没有得到任何错误,程序运行正常。我正在寻找一些提示,当我使用该链接中列出的.xaml和rotatebutton方法时,为什么它会给我一个错误。
编辑:修复了“当前上下文中不存在”的“不存在”错误,除了两个“selectedFileName”错误。 .xaml中的某些名称与后面的代码中的名称不同。所以除了这两个错误之外,我得到一个说''WPFImageViewer.MainWindow'不包含'RotationList_SelectionChanged'的定义,并且没有扩展方法'RotationList_SelectionChanged'可以找到类型'WPFImageViewer.MainWindow'的第一个参数'(你错过了使用指令或汇编引用吗?)。
这是.xaml:
<Window x:Class="WPFImageViewer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="409" Width="574">
<Grid >
<Label Content="Label" Height="32" HorizontalAlignment="Left" Margin="11,10,0,0"
Name="FileNameLabel" VerticalAlignment="Top" Width="393"
Background="LightGray" BorderBrush="Gray" BorderThickness="1"/>
<Button Content="Browse a File" Height="34" HorizontalAlignment="Left" Margin="410,8,0,0"
Name="BrowseButton" VerticalAlignment="Top" Width="119"
Foreground="Maroon" FontSize="16" FontFamily="Georgia" Click="BrowseButton_Click" />
<Image Height="305" HorizontalAlignment="Left" Margin="14,53,0,0"
Name="ImageControl" Stretch="Fill" VerticalAlignment="Top" Width="390" />
<Button Content="Rotate" FontFamily="Georgia" FontSize="12" Foreground="Maroon"
Height="26" HorizontalAlignment="Left" Margin="410,61,0,0"
Name="RotateButton" VerticalAlignment="Top" Width="56" Click="RotateButton_Click" />
<ComboBox Height="30" HorizontalAlignment="Right" Margin="0,57,12,0" Name="RotationList"
VerticalAlignment="Top" Width="68" SelectedIndex="0" SelectionChanged="RotationList_SelectionChanged">
<ComboBoxItem Content="Rotate0" ContentStringFormat="Rotate0" />
<ComboBoxItem Content="Rotate90" ContentStringFormat="Rotate90" />
<ComboBoxItem Content="Rotate180" ContentStringFormat="Rotate180" />
<ComboBoxItem Content="Rotate270" ContentStringFormat="Rotate270" />
</ComboBox>
</Grid>
</Window>
这是背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Windows.Forms;
namespace ImageViewer
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
ImageViewer1.Source = new BitmapImage(new Uri("Creek.jpg", UriKind.Relative));
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = "c:\\";
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string selectedFileName = dlg.FileName;
FileNameLabel.Content = selectedFileName;
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(selectedFileName);
bitmap.EndInit();
ImageViewer1.Source = bitmap;
}
}
private void RotateButton_Click(object sender, RoutedEventArgs e)
{
if (selectedFileName.Length > 0)
{
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(selectedFileName);
bitmap.Rotation = (Rotation)Enum.Parse(typeof(Rotation),
RotationList.SelectionBoxItemStringFormat);
bitmap.EndInit();
ImageControl.Source = bitmap;
}
}
}
}
答案 0 :(得分:2)
我认为您应该在后台代码中检查命名空间。它应该是WPFImageViewer。喜欢这个
namespace WPFImageViewer
{
public sealed partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
}
// code...
}