2

Is there a way to to serialize class asynchronously? I'm asking becouse when my program is trying to do this:

public static async void Serialize(List<Zajecia> xml_List)
    {
        using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync("Plan list.xml", CreationCollisionOption.ReplaceExisting))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Zajecia>));
            using (XmlWriter xmlWriter = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true }))
            {
                serializer.Serialize(xmlWriter, xml_List);
            }
        }

    }

Class:

public class Zajecia
    {
        public string c { get; set; }
        public string p { get; set; }

        public Zajecia ()
        {}
        public Zajecia(string C, string P)
        {
            c = C;
            p = P;
        }
    }

My async serialization is used in navigation function:

 protected override async Task OnNavigatedTo(NavigationEventArgs e)
    {
            string t = e.Parameter as string;

        if (!string.IsNullOrEmpty(t))
        {
            string[] _p = new string[10];
            _p = t.Split('^');
            _lista.Add(new Zajecia(t,( _p[3]+"("+_p[5]+")")));
            Dodaj_zajecia(_p);
            await Serialize(_lista);            
        }
        else
            base.OnNavigatedTo(e);

    }

I get only this error instead of serialization of anything:

System.Runtime.InteropServices.WindowsRuntime.RuntimeClass is inaccessible due to its protection level. Only public types can be processed.

Everything is public in my program and still it doesn't help. Any ideas how to make it work?

MajQel
  • 145
  • 4
  • 17

1 Answers1

1

UPDATE 2

I have checked your project. The problem was, you declared Zajecia class within BasicPage1 class. You must declare it as separate class file or within scope of namespace.

Right Way

namespace Plan
{
    public class Zajecia
    {
     .....
    }

    public sealed partial class BasicPage1 : Plan.Common.LayoutAwarePage
    {
     .....
    }
}

Wrong Way

namespace Plan
{
    public sealed partial class BasicPage1 : Plan.Common.LayoutAwarePage
    {
        public class Zajecia
        {
         .....
        }
     .....
    }
}

UPDATE 1

Here's code for you.

public static async Task Serialize(List<Zajecia> xml_List)
{
    using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync("Plan list.xml", CreationCollisionOption.ReplaceExisting))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Zajecia>));
        using (XmlWriter xmlWriter = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true }))
        {
            serializer.Serialize(xmlWriter, xml_List);
        }
    }
}

Whenever you want to call the method use await as prefix, suppose any button click event calls that method then I will write like this.

Suppose Helper is my static class

private async void btnSerialize_Click(object sender, RoutedEventArgs e)
{
    await Helper.Serialize(MyZajeciaList);
}

Mark one thing I have used async void modifier in click event.

Suppose if I am calling method from any other method like DoWork(...), then I will use async Task instead of async void

private async Task DoWork(List<Zajecia> MyZajeciaList)
{
    await Helper.Serialize(MyZajeciaList);
}

Your code is working for me just change return type of Serialize method from void to Task. You need to call method as await Serialize(...);

Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
  • U mean I need to place await Instead of async ? – MajQel Jun 17 '13 at 09:22
  • No, you need to use `async Task` instead of `async void`. Always use `async Task` in user defined method, where `T` is return type of that method. Here method returns nothing so use only `Task`. Only event handlers can have `async void`. – Farhan Ghumra Jun 17 '13 at 10:42
  • Oh I know what u mean but there no use in returning anything of serialization.... serialization should just serialize it shouldn't return anything ... where I even dont know where can I place return. If you would paste an example or something i would be gradeful. – MajQel Jun 17 '13 at 14:37
  • you don't have to explicitly return anything from your method, even though the return type is Task. The async/await keywords are managing that for you. THe [thread here](http://stackoverflow.com/questions/12144077/async-await-when-to-return-a-task-vs-void) gives a bit more insight. – Jim O'Neil Jun 17 '13 at 14:46
  • I'm completly new in async programing so some things are hard for me to understand – MajQel Jun 17 '13 at 14:46
  • Most of us are new to it :) no apologies necessary, but it is a core concept particularly in mobile and Windows 8 development, so worth the investment to understand the model. This article is a good jumping-off spot: http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx – Jim O'Neil Jun 17 '13 at 14:49
  • Well, I still get same error nothing has changed ... I edited also to show where I call my serialization – MajQel Jun 17 '13 at 18:00
  • Can you upload your project on cloud drive (SkyDrive, Dropbox, Box, etc) So I can analyze what are you doing wrong. – Farhan Ghumra Jun 18 '13 at 02:37
  • I can send it via mail... i'm not using cloud drives – MajQel Jun 18 '13 at 09:49
  • I already did. But still no response ... didn't have a time but if need I can upload or RS or smth that u will be sure there r no viruses. – MajQel Jun 19 '13 at 07:17
  • I haven't got any mail from you. – Farhan Ghumra Jun 19 '13 at 07:24