在当前窗口中插入另一个项目

本文关键字:插入 另一个 项目 窗口 | 更新日期: 2023-09-27 17:56:03

我正在创建一个带有菜单的 WPF 应用程序,当单击菜单项时,我想在我的窗口(插件)中显示 NQueens(另一个项目)。我已经在我的 CurrentDirectory 中放置了一个 NQueens.dll 来使用。为了完成这项工作,我创建了一个程序集对象来加载类,并创建了一个 Nqueens.Nqueen 的实例并调用了这些方法。

NQueens 项目由一个类 NQueen.cs 和一个 MainWindow 组成。

namespace NQueens
{
public class NQueen
{
   public static bool berekenQueens(int Row, int N, bool[,] bord)
   {
       if (Row >= N) return true;  //stopconditie
       for (int Col = 0; Col < N; Col++)
       {
           //Q toevoegen
           bord[Row, Col] = true;
           //Q + Q volgende Row  controleren
           if (bordValidatie(Row, Col, bord, N) && berekenQueens(Row + 1, N, bord))
           {
               return true;
           }
           //Q verwijderen indien niet door controle
           bord[Row, Col] = false;
       }
       return false;
   }
   private static bool bordValidatie(int currentRow, int currentCol, bool[,] currentBord, int N)
   {
       int colstep = 1;
       for (int i = currentRow - 1; i >= 0; i--)
       {
           //rechte lijn 
           if (currentBord[i, currentCol])
               return false;
           //linker diagonaal
           if (currentCol - colstep >= 0)
           {
               if (currentBord[i, currentCol - colstep])
                   return false;
           }
           //rechter diagonaal
           if (currentCol + colstep < N)
           {
               if (currentBord[i, currentCol + colstep])
                   return false;
           }
           colstep += 1;
       }
       return true;
   }
}
}

MainWindow.xaml.cs

namespace NQueens
{
public partial class MainWindow : Window
{
    public int iN { get { return Convert.ToInt32(txtN.Text); } set { txtN.Text = "" + value; } }
    private bool[,] spelbord;
    public MainWindow()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        spelbord = new bool[iN, iN];
        NQueen.berekenQueens(0, iN, spelbord);
        visualise(iN, spelbord);
    }
    private void visualise(int N, bool[,] bord)
    {
        gridTekenen();
        for (int row = 0; row < N; row++)
        {
            for (int col = 0; col < N; col++)
            {
                Rectangle rect = new Rectangle();
                rect.Stretch = Stretch.Fill;
                TextBlock txtB = new TextBlock();
                if (spelbord[row, col])
                {
                    SolidColorBrush mySolidColorBrush = new SolidColorBrush();
                    mySolidColorBrush.Color = Colors.LightGreen;
                    rect.Fill = mySolidColorBrush;
                    txtB.Text = "Q";
                }
                rect.SetValue(Grid.ColumnProperty, col);
                rect.SetValue(Grid.RowProperty, row);
                txtB.SetValue(Grid.ColumnProperty, col);
                txtB.SetValue(Grid.RowProperty, row);
                gridPaneel.Children.Add(rect);
                gridPaneel.Children.Add(txtB);
            }
        }
    }
    private void gridTekenen()
    {
        gridPaneel.ShowGridLines = true;
        int grooteGrid = int.Parse(txtN.Text);
        RowDefinition rowDef;
        ColumnDefinition colDef;

        for (int i = 0; i < grooteGrid; i++)
        {
            rowDef = new RowDefinition();
            GridLengthConverter myGridLengthConverter = new GridLengthConverter();
            GridLength gl1 = (GridLength)myGridLengthConverter.ConvertFromString(150 + "*");
            rowDef.Height = gl1;
            colDef = new ColumnDefinition();
            colDef.Width = gl1;
            gridPaneel.RowDefinitions.Add(rowDef);
            gridPaneel.ColumnDefinitions.Add(colDef);
        }
    }
}
}

单击菜单项时 WPF 应用程序中的代码。

private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        // Create an assembly object to load our classes
        string path = System.Environment.CurrentDirectory + "''NQueens.dll";
        Assembly ass = Assembly.LoadFile(path);
        Console.WriteLine(path);
        Type objType = ass.GetType("NQueens.NQueen");
        // Create an instace of NQueens.NQueen
        var instance = Activator.CreateInstance(objType);
        // public static bool berekenQueens(int Row, int N, bool[,] bord)
        var result = objType.InvokeMember("berekenQueens",BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
        null, instance, new object[] {   1, /* Row */ 1, /* N */ new bool[,] { {true,false} } /* bord */
        });
        // private static bool bordValidatie(int currentRow, int currentCol, bool[,] currentBord, int N)
        var result2 = objType.InvokeMember("bordValidatie", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic,
        null, instance, new object[] {   1, /* Row */ 1, /* N */ new bool[,] { {true,false} } /* bord */, 1
        });
    }

我不知道现在该怎么办。单击菜单项时,如何在当前窗口中显示NQueen程序。

在当前窗口中插入另一个项目

> NQueen 类应该是任何类型的 wpf 控件后代。如果是简单类,则无法与其容器通信。

因此,

您应该实例化 NQueens.MainWindow 而不是 NQueens.NQueen 并调用 Show 方法。