在一个基本的WPF项目中,如何避免进行大范围的类初始化
本文关键字:何避免 初始化 范围 项目 一个 WPF | 更新日期: 2023-09-27 18:22:39
这是一个非常基本的问题,我确信我没有找到答案的原因是因为我不是100%肯定如何搜索它。
我有一个非常简单的项目,我想使用一个包含我的信息的类。
public class MyInfo
{
public string name = String.Empty;
public string ssn = String.Empty;
public string dob = String.Empty;
}
非常基础。
我有一个带有按钮的xaml文件,我只想将类中变量的值设置为按钮中的内容。这是xaml。
主窗口.xaml
<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>
<Button Name="name" Content="John Doe" HorizontalAlignment="Left" Margin="117,60,0,0" VerticalAlignment="Top" Width="75" Click="name_click"/>
<Button Name="ssn" Content="222-22-2222" HorizontalAlignment="Left" Margin="117,120,0,0" VerticalAlignment="Top" Width="75" Click="ssn_click"/>
<Button Name="dob" Content="June 1st 1992" HorizontalAlignment="Left" Margin="117,178,0,0" VerticalAlignment="Top" Width="75" Click="dob_click"/>
</Grid>
</Window>
我遇到的问题是,我知道如何做到这一点(作为一个C#noob)的唯一方法是在超宽范围内初始化我的类。
例如,这是我的cs文件,其中对我最初所做的事情有评论,但对我认为应该做的事情没有评论。
主窗口.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace WpfApplication1
{
public class MyInfo
{
public string name = String.Empty;
public string ssn = String.Empty;
public string dob = String.Empty;
}
// If i create a class instance here like this
//MyInfo infoTest = new MyInfo();
// then I can just set the values in the click functions like
//infoTest.dob = dob.Content.ToString();
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void name_click(object sender, RoutedEventArgs e)
{
//save name.content into MyInfo.name
}
private void ssn_click(object sender, RoutedEventArgs e)
{
//save ssn.content into MyInfo.ssn
}
private void dob_click(object sender, RoutedEventArgs e)
{
//save dob.Content into MyInfo.dob
}
}
}
我认为最好在InitializeComponent();
之后的MainWindow类中创建我的类的实例,但我不确定如何将我的值放入具有封闭作用域的类对象中。
就像这样,您也应该使用Auto-Implemented Properties
而不是公共字段:
public Window1()
{
InitializeComponent();
MyInfo myinfo = new MyInfo();
myinfo.Name = name.Content.ToString();
myinfo.Ssn = ssn.Content.ToString();
myinfo.Dob = dob.Content.ToString();
}
public class MyInfo
{
public string Name { get; set; }
public string Ssn { get; set; }
public string Dob { get; set; }
}
如果你想在点击按钮后设置属性,你应该试试这个:
MyInfo myinfo = new MyInfo();
public Window1()
{
InitializeComponent();
}
然后:
private void name_Click(object sender, RoutedEventArgs e)
{
myinfo.Name = name.Content.ToString();
}
实现这一点的另一种方法是使用Binding
。
public class MyInfo
{
public string Name { get; set; }
public string Ssn { get; set; }
public string Dob { get; set; }
}
public class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MyInfo();
}
...
您的XAML将如下所示:
<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>
<Button Name="name" Content="{Binding Name}" HorizontalAlignment="Left" Margin="117,60,0,0" VerticalAlignment="Top" Width="75"/>
<Button Name="ssn" Content="{Binding Ssn}" HorizontalAlignment="Left" Margin="117,120,0,0" VerticalAlignment="Top" Width="75"/>
<Button Name="dob" Content="{Binding Dob}" HorizontalAlignment="Left" Margin="117,178,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
这将使您避免使用Click
事件来设置对象的属性。你可以在这里找到一个优秀的教程。
答案已经标记,但我想使用绑定给您另一个POV。我更喜欢将GUI/业务层分离。
部分代码:
public partial class MainWindow : Window
{
public class MyInfo
{
public string Name { get; set; }
public string Ssn { get; set; }
public string Dob { get; set; }
}
private MyInfo _myInfo;
public MainWindow()
{
InitializeComponent();
_myInfo = new MyInfo();
_myInfo.Name = "My name";
this.DataContext = _myInfo;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("My name is '" + _myInfo.Name + "'");
}
}
XAML
<Window x:Class="BindingExample.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">
<StackPanel Width="200" VerticalAlignment="Center">
<TextBox Text="{Binding Name}" />
<TextBox Text="{Binding Ssn}" />
<TextBox Text="{Binding Dob}" />
<Button Content="Click me" Click="Button_Click" />
</StackPanel>
</Window>
甚至可以从代码更新TextBoxes(无需直接写入Text属性),分配您的类。使用INotifyPropertyChanged