3

我对这ElementHost件事有很大的问题。有人可以看看我有什么,让我知道发生了什么或为什么没有显示?一切似乎都正常,只是我看不到ElementHost屏幕上的显示/刷新(用户控件)。

Test.cs.designer

this.TestHost.Dock = System.Windows.Forms.DockStyle.Fill;
this.TestHost.Location = new System.Drawing.Point(5, 5);
this.TestHost.Name = "TestHost";
this.TestHost.Size = new System.Drawing.Size(1139, 213);
this.TestHost.TabIndex = 1;
this.TestHost.Text = "elementHost1";
this.TestHost.Child = this.testview1;
this.TestHost.Visible = true;

测试.cs

private void btnTest_Click(object sender, EventArgs e)
{
    LoadView(item);
}

public void LoadView(Person item)
{
    TestHost.Child = this.testview1; //name given when drag/drop .. should still reference wpf TestView xaml

    try
    {
        if (item != null)
        {
            // TestView is WPF xaml/View                
            TestView view = new TestView();
            view = (TestView) TestHost.Child;
            //TestViewModel is a viewmodel

            TestViewModel vm = (TestViewModel) view.DataContext;
            //load items from viewmodel             
            vm.LoadItems(item);
        }   

        TestHost.Visible = true;
    }
    catch(Exception ex)
    {
        ex.ToString();
    }
}
4

1 回答 1

1

从您的代码看来,您将 elementhost 子项设置为 Class 而不是它的实例(不会编译)。我的问题是TestView 实例来自哪里?

TestHost.Child = TestView;

要调试,请在 try/catch 块中更改“ex.ToString()”以显示消息框;如果发生异常,您会注意到它。

于 2012-07-30T08:44:30.683 回答