0
我想學習WPF。我加PresentationCore,PresentationFramework,System.XAML和WindowsBase的引用我的類庫和輸入以下代碼:關於線程的WPF錯誤
internal struct Program
{
static void Main(string[] args)
{
new MainScreen().Open();
}
}
internal struct MainScreen
{
private Window _Window;
private Grid _LayoutRoot;
internal void Open()
{
_Window = new Window();
_LayoutRoot = new Grid();
_Window.Content = _LayoutRoot;
addBackground();
addDataGrid();
_Window.WindowState = WindowState.Maximized;
_Window.ShowDialog();
}
private void addBackground()
{
LinearGradientBrush bkg = new LinearGradientBrush();
GradientStopCollection grdCol = new GradientStopCollection();
GradientStopCollection grdStops = new GradientStopCollection();
grdStops.Add(new GradientStop(Color.FromArgb(255, 150, 150, 150), 0));
grdStops.Add(new GradientStop(Color.FromArgb(255, 235, 235, 235), .7));
grdStops.Add(new GradientStop(Color.FromArgb(255, 150, 150, 150), 1));
bkg.GradientStops = grdStops;
_LayoutRoot.Background = bkg;
}
private void addDataGrid()
{
DataGrid dgrRecords = new DataGrid();
dgrRecords.Margin = new Thickness(0, 70, 0, 0);
dgrRecords.IsReadOnly = true;
DataGridColumn colID = new DataGridTextColumn();
DataGridHelper grdHelper = new DataGridHelper(dgrRecords, new GetSelected(processGridSelection));
grdHelper.SetDoubleClick();
grdHelper.AddColumn("colID", "ID", "ID");
grdHelper.AddColumn("colLastName", "Last Name", "LastName");
grdHelper.AddColumn("colFirstName", "First Name", "FirstName");
List<Person> People = new List<Person>();
People.Add(new Person(0, "FName1", "LName1"));
People.Add(new Person(1, "FName100", "LName100"));
dgrRecords.ItemsSource = People;
_LayoutRoot.Children.Add(dgrRecords);
}
private void processGridSelection(object item)
{
Person person = (Person)item;
MessageBox.Show(person.LastName);
}
}
internal struct DataGridHelper
{
private DataGrid _DataGrid;
private GetSelected _GetSelected;
internal DataGridHelper(DataGrid dgr, GetSelected getSelected)
{
_DataGrid = dgr;
_GetSelected = getSelected;
_DataGrid.AutoGenerateColumns = false;
}
internal void SetDoubleClick()
{
_DataGrid.MouseDoubleClick += new MouseButtonEventHandler(mouseDoubleClick);
}
internal void AddColumn(string colName, string header, string fieldBinding)
{
DataGridTextColumn col = new DataGridTextColumn();
col.Binding = new Binding(fieldBinding);
col.Header = header;
_DataGrid.Columns.Add(col);
}
private void mouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (_DataGrid.SelectedIndex < 0)
return;
_GetSelected.Invoke(_DataGrid.SelectedItem);
}
}
internal enum Screen
{
Person,
People
}
internal delegate void GetSelected(object item);
我收到以下錯誤,當我嘗試打開應用程序:
的PresentationCore.dll中發生未處理的異常'System.InvalidOperationException'
附加信息:調用線程必須是STA,因爲很多UI組件都需要這個。
我被困在如何解決這個問題,特別是因爲我沒有使用任何多線程。另外,我不知道這是否重要,除了看起來像Window,Grid,DataGrid等所有組件都是類而不是結構。我已經不得不修改我的DatagridHelper結構,以避免構建錯誤,如果我曾經使用過類,那麼我可以避免這種錯誤。 WPF是否偏向於面向對象編程?
添加
[STAThread]
順便說一句,爲什麼你的所有類型的結構?創建自己的結構非常不是個好主意......當然不適用於UI屏幕等。 –你的主要方法應該是[STAThread] static void Main(string [] args) –
我主要使用結構,因爲我從中學到的書只使用它們。但是,我可以使用類並像結構一樣對待它們。這可能會讓事情變得更加平滑。 – user3814870