Reputation: 1945
I'm very new to WPF, Visual Studio, and C#. I have coded in java (though it's been awhile), so a lot of the C# code looks sort of familiar. I'm trying to test out the code listed here: http://www.c-sharpcorner.com/uploadfile/mahesh/image-viewer-in-wpf/
Now, when I copied the code there to use, the window and button displays fine using the .xaml code. However, in the code behind, I get 8 errors all saying "the name 'xxx' does not exist in the current context". These are: InitializeComponent, ImageViewer1, FileNameLabel, selectedFileName, RotationList, and ImageControl. If you download the files, the .xaml file is different and only the BrowseButton_Click method is used. Using those two files, I don't get any errors and the program runs fine. I'm just looking for some hints of why it's giving me an error when I use the .xaml and rotatebutton method listed in that link.
Edit: Fixed the "does not exist in the current context" for the "does not exist" errors except two "selectedFileName" ones. Some names in the .xaml were different than the names in the code behind. So besides those two errors, I'm getting one that says "'WPFImageViewer.MainWindow' does not contain a definition for 'RotationList_SelectionChanged' and no extension method 'RotationList_SelectionChanged' accepting a first argument of type 'WPFImageViewer.MainWindow' could be found (are you missing a using directive or assembly reference?).
Here's the .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>
Here's the code behind:
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;
}
}
}
}
Upvotes: 0
Views: 3742
Reputation: 514
I think you should check namespace in background code. It should be WPFImageViewer. Like this
namespace WPFImageViewer
{
public sealed partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
}
// code...
}
Upvotes: 2