Reputation: 155
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?
Upvotes: 3
Views: 7752
Reputation: 15296
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(...);
Upvotes: 2