簡體   English   中英

有關線程的WPF錯誤

[英]WPF error regarding threading

我正在嘗試開始學習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組件都需要STA。

我一直堅持如何解決此問題,特別是因為我沒有使用任何多線程。 另外,我不知道這是否重要,除了似乎所有組件(例如Window,Grid,DataGrid等)都是類而不是結構。 我已經不得不修改我的DatagridHelper結構,以避免如果使用類可以避免的構造錯誤。 WPF是否偏向於面向對象編程?

您必須在main方法中添加[STAThread] ,例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM