locked
EventArgs e replace with DataObject e RRS feed

  • Question

  • I have a override method call OnDrop.

    It has DragEventArgs e

    I want my create function DataObject e can do same thing.

    Here is my previous OnDrop code is in canvas

    protected override void OnDrop(DragEventArgs e)
    {
        string xamlString = e.Data.GetData("CANVAS") as string;
        if (!String.IsNullOrEmpty(xamlString))
        {
            FrameworkElement content = XamlReader.Load(XmlReader.Create(new StringReader(xamlString))) as FrameworkElement;
        }//end if
    }

    Here is my function

    public static void create()
    {
        try
        {               
             DataObject e = new DataObject();
             string xamlString = e.GetData("CANVAS") as string;
             if (!String.IsNullOrEmpty(xamlString))
             {
                 FrameworkElement content = XamlReader.Load(XmlReader.Create(new StringReader(xamlString))) as FrameworkElement;
             }//end if
        }
        catch { }
    }

    In my OnDrop method

    xamlString has data inside

    But create()

    xamlString is null.

    How to fix it with the DataObject or other class data member?

    Tahnks


    • Edited by CY_Chen Wednesday, August 21, 2019 8:19 AM
    Wednesday, August 21, 2019 8:07 AM

All replies

  • Hi,

    try to fix your code:

      public static void create()
            {
                try
                {
                    DataObject e = new DataObject();
                    string xamlString = e.GetData(typeof(Canvas)) as string;
                    if (!String.IsNullOrEmpty(xamlString))
                    {
                        FrameworkElement content = XamlReader.Load(XmlReader.Create(new StringReader(xamlString))) as FrameworkElement;
                    }//end if
                }
                catch { }
            }

    see the following link:

    https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.dataobject.getdata?view=netframework-4.8#System_Windows_DataObject_GetData_System_Type_

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 22, 2019 9:29 AM