其中是实例化类方法使用的引用对象的最佳位置+;C#
我有一个简单而愚蠢的问题,但我希望找到一个答案,让我的头脑清楚C#reference对象,以及在哪里最好地实例化它们。我在类声明之后声明了其中是实例化类方法使用的引用对象的最佳位置+;C#,c#,wpf,openfiledialog,C#,Wpf,Openfiledialog,我有一个简单而愚蠢的问题,但我希望找到一个答案,让我的头脑清楚C#reference对象,以及在哪里最好地实例化它们。我在类声明之后声明了openfileVaraible,这样我就可以引用类中的任何其他方法。在我按下“添加照片”按钮并选择照片之前,一切都正常。如果我再次按下按钮并取消显示对话框,则openfile.FileName不会保存文件扩展名。为了解决这个问题,我移动了这行代码openfile=newopenfiledialog()到类构造函数。实例化对象的方式是否错误,因为它将在RAM中
openfile
Varaible,这样我就可以引用类中的任何其他方法。在我按下“添加照片”按钮并选择照片之前,一切都正常。如果我再次按下按钮并取消显示对话框,则openfile.FileName
不会保存文件扩展名。为了解决这个问题,我移动了这行代码openfile=newopenfiledialog()代码>到类构造函数。实例化对象的方式是否错误,因为它将在RAM中保存aspace。或者仅在要使用对象的位置实例化对象
private void addPhotoBtn_Click(object sender, RoutedEventArgs e)
{
openfile = new OpenFileDialog();
openfile.Filter = "Images |*.JPG; *.PNG";
if (openfile.ShowDialog() == true)
{
userImage.Source = new BitmapImage(new Uri(openfile.FileName, UriKind.RelativeOrAbsolute));
filename = openfile.SafeFileName;
sourcePath = @openfile.FileName;
targetPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "/Images/users/" + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
}
private void saveUserBtn_Click(object sender, RoutedEventArgs e)
{
string fname, lname, username, password, email, phone, address, role, photo = "";
int spec_id; int dept_id;
fname = firstNameTextBox.Text.ToString();
lname = lastNameTextBox.Text.ToString(); ;
username = usernameTextBox.Text.ToString(); ;
password = passwordBox.Password;
email = emailTextBox.Text.ToString(); ;
phone = phoneTextBox.Text.ToString(); ;
address = addressTextBox.Text.ToString();
spec_id = specComboBox.SelectedIndex;
role = roleComboBox.Text.ToString();
if (deptComboBox.SelectedIndex >= 0)
dept_id = int.Parse(deptComboBox.SelectedValue.ToString());
else
dept_id = 0;
// Input Validation
if (state == "add" && sourcePath != null || sourcePath != null && state != "add")
{
photo = new Uri("pack://application:,,/CMMS;component/images/users/") + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
.
.
.
如果要在任何地方访问文件名而不是为空,请指定一个本地更改,并在选择文件名时保留地址。
后命名空间
private const string FileName = string.Empty;
所以你的代码
private void addPhotoBtn_Click(object sender, RoutedEventArgs e)
{
openfile = new OpenFileDialog();
openfile.Filter = "Images |*.JPG; *.PNG";
if (openfile.ShowDialog() == true)
{
userImage.Source = new BitmapImage(new Uri(openfile.FileName, UriKind.RelativeOrAbsolute));
filename = openfile.SafeFileName;
sourcePath = @openfile.FileName;
this.FileName = @openfile.FileName;
targetPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "/Images/users/" + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
}
private void saveUserBtn_Click(object sender, RoutedEventArgs e)
{
string fname, lname, username, password, email, phone, address, role, photo = "";
int spec_id; int dept_id;
fname = firstNameTextBox.Text.ToString();
lname = lastNameTextBox.Text.ToString(); ;
username = usernameTextBox.Text.ToString(); ;
password = passwordBox.Password;
email = emailTextBox.Text.ToString(); ;
phone = phoneTextBox.Text.ToString(); ;
address = addressTextBox.Text.ToString();
spec_id = specComboBox.SelectedIndex;
role = roleComboBox.Text.ToString();
if (deptComboBox.SelectedIndex >= 0)
dept_id = int.Parse(deptComboBox.SelectedValue.ToString());
else
dept_id = 0;
// Input Validation
if (state == "add" && sourcePath != null || sourcePath != null && state != "add")
{
photo = new Uri("pack://application:,,/CMMS;component/images/users/") + getTime + System.IO.Path.GetExtension(this.FileName);
}
.
.
.
在类构造函数中的addPhoto函数外部实例化openfileDialog,而不是每次按下addPhoto按钮时创建新对象,这是错误的吗?public AddUser(){InitializeComponent();deptComboBox.IsEnabled=false;openfile=new OpenFileDialog();}没问题。你可以在构造函数中构建它,并在任何地方使用它,但一定要像我的代码一样创建一个局部变量,然后在构造函数中设置它,如:private OpenFileDialog OpenFileDialog=null;在构造函数写入后:openFileDialog=newopenfiledialog();