picoe / Eto

Cross platform GUI framework for desktop and mobile applications in .NET
Other
3.57k stars 325 forks source link

using of virtual property causes System.Reflection.TargetInvocationException #48

Closed evgeny-k closed 12 years ago

evgeny-k commented 12 years ago

A first chance exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll

Additional information: Property accessor 'MyImage' on object 'etotest.MyTreeGridItem' threw the following exception:'Object does not match target type.'

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Eto; using Eto.IO; using Eto.Forms; using Eto.Drawing; using System.Threading;

namespace etotest { class Program { [STAThread] static void Main(string[] args) { Generator generator; if (EtoEnvironment.Platform.IsWindows) { generator = Generator.GetGenerator("Eto.Platform.Wpf.Generator, Eto.Platform.Wpf"); } else if (EtoEnvironment.Platform.IsMac) { generator = Generator.GetGenerator("Eto.Platform.Mac.Generator, Eto.Platform.Mac"); } else { generator = Generator.GetGenerator("Eto.Platform.GtkSharp.Generator, Eto.Platform.Gtk"); }

        var app = new TestApplication(generator);
        app.Run(args);
    }
}

public class TestApplication : Application
{
    public TestApplication(Generator generator)
        : base(generator)
    {
        this.Name = "Test Application";
    }

    public override void OnInitialized(EventArgs e)
    {
        this.MainForm = new MainForm();
        HandleEvent(Application.TerminatingEvent);

        base.OnInitialized(e);

        // show the main form
        this.MainForm.Show();
    }

}

public class MyTreeGridItem : TreeGridItem
{ 
   public virtual Icon MyImage  { get; set; }
   public String MyCaption { get; set; }
}

public class MyTreeGridItem1 : MyTreeGridItem
{
    public override Icon MyImage { get; set; }
}

public class MainForm : Form
{
    int[] arr = new int[] { 5, 5 };
    TreeGridView tv;
    TreeGridItem it;
    Icon ic;

    void CreateItem1(TreeGridItem aParent, string aBaseName, int aLevel)
    {
        var lName = "[" + aLevel + "]" + aBaseName;
        var lItem = new MyTreeGridItem { MyImage = ic, MyCaption = lName };
        lItem.Expanded = false;

        aParent.Children.Add(lItem);
    }

    void CreateItem(TreeGridItem aParent, string aBaseName, int aLevel)
    {
        var lName = "[" + aLevel + "]" + aBaseName;
        var lItem = new MyTreeGridItem1 { MyImage = ic, MyCaption = lName };
        lItem.Expanded = true;

        aParent.Children.Add(lItem);
        if (aLevel < arr.Length)
            for (int i = 0; i < arr[aLevel]; i++)
                CreateItem1(lItem, aBaseName + i.ToString(), aLevel + 1);
    }

    void CreateTree()
    {
        TreeGridItem lRoot = new TreeGridItem();
        CreateItem(lRoot, "Name", 0);
        lRoot.Expanded = true;

        Application.Instance.Invoke(delegate
        {
            tv.DataStore = lRoot;
        });
    }

    public MainForm()
    {
        // black.ico is added into project as "Embedded resource"
        ic = Icon.FromResource("DocsEditor.black.ico");
        Size = new Size(350, 600);
        var fMainSplitter = new Splitter { Orientation = SplitterOrientation.Horizontal, FixedPanel = SplitterFixedPanel.Panel1 };
        this.AddDockedControl(fMainSplitter, new Padding(5));
        fMainSplitter.Panel1 = new Panel();
        var lPanelLayout = new TableLayout(fMainSplitter.Panel1 as Panel, 1, 1);
        lPanelLayout.Padding = new Padding(0);

        tv = new TreeGridView();
        tv.Columns.Add(new GridColumn { DataCell = new ImageTextCell("MyImage", "MyCaption") });
        lPanelLayout.Add(tv, 0, 0, true, true);

        fMainSplitter.Panel2 = new Panel();
        lPanelLayout = new TableLayout(fMainSplitter.Panel2 as Panel, 1, 1);
        lPanelLayout.Padding = new Padding(0);
        var btn = new Button { Text = "create tree" };
        lPanelLayout.Add(btn, 0, 0, false, false);
        btn.Click += (sender, e) => { CreateTree(); };

        fMainSplitter.Position = 150;

    }
}

}

cwensley commented 12 years ago

What platform throws the exception?