有关线程的WPF错误

时间:2014-07-08 06:00:06

标签: c# wpf

我正在尝试开始学习WPF。我在我的类库中添加了PresentationCore,PresentationFramework,System.XAML和WindowsBase的引用,并输入以下代码:

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是否偏向于面向对象的编程?

1 个答案:

答案 0 :(得分:2)

您必须在主要方法中添加[STAThread],如

[STAThread]
static void Main(string[] args)
{
    new MainScreen().Open();
}