我无法处理这些类c#uwp

时间:2018-04-27 10:51:41

标签: c# uwp

直到现在我只有小应用程序,但现在我无法处理这些类c#uwp

我想将代码分成几个类,每个类都会处理几个参数。类会将参数值发送到主页面。但是当我尝试显示这些参数时,它们始终为零,即使文本文件显示为已修改。

我有主页

namespace airflow
{
    public class param_perimetrala
    {
        public static int ora_start_perimetrala;
        public int minut_start_perimetrala;
        public int ora_stop_perimetrala;
        public int minut_stop_perimetrala;
        public int ore_ciclu_perimetrala;
        public int minut_ciclu_perimetrala;
        public int contor_ore_perimetrala = 0;
        public int contor_minute_perimetrala = 0;
        public int contor_sec_perimetrala = 0;

        public async void readfile_perimetrala()
        {
            StorageFolder folder = ApplicationData.Current.LocalFolder;
            StorageFile perimetrala_file = await folder.CreateFileAsync("parametrii_perimetrala.txt", CreationCollisionOption.OpenIfExists);

            var readFile_perimetrala = await FileIO.ReadLinesAsync(perimetrala_file);
            int count = 0;
            foreach (var line in readFile_perimetrala)
            {
                string[] split_perimetrala = line.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                var temp = split_perimetrala[1];
                if (count == 0)
                {
                    ora_start_perimetrala = Int32.Parse(temp);
                }
                if (count == 1)
                {
                    minut_start_perimetrala = Int32.Parse(temp);
                }
                if (count == 2)
                {
                    ora_stop_perimetrala = Int32.Parse(temp);
                }

                count = count + 1;
            }
        }

        public int start_perimetrala
        {
            get { return ora_start_perimetrala; }
            set { ora_start_perimetrala = value; }
        }
    }
}

和一个班级

dplyr

如何在主页中发送ora_start_perimetrala值?

enter image description here

1 个答案:

答案 0 :(得分:1)

main_page_Loaded事件处理程序方法中,您调用read_file_perimetralaread_file_perimetralaasync void。这意味着一旦你到达await语句,并且等待实际上是等待的(不立即返回),那么该方法将把剩下的工作放在一边,直到等待的部分完成。然后它回升并运行其余部分。在将它放在一边的那段时间里,调用线程然后继续在任何需要它的地方运行。

您需要将此作为任务并等待调用,以便您可以确保在继续工作之前填充参数。

将您的readFile_perimetrala更改为:

public async Task readfile_perimetralaAsync()

将整个main_page_Loaded事件处理程序更改为如此...

private async void main_page_Loaded(object sender)
{
    param_perimetrala read = new param_perimetrala();
    await read.readfile_perimetralaAsync();
    var mesaj = new MessageDialog(read.start_perimetrala.ToString());
    var res = await mesaj.ShowAsync();
}

在您的代码中,您将值分配给方法,但我可以告诉您需要int值。

只是一些轻量级的例子来帮助我们开始理解Task和async await。不要把它作为一种资源,但只是足以让你好奇深入挖掘,因为它是一个简单但相当深刻的主题。

public class TaskExamples
{       
    public async void DoAsyncVoid()
    {
        await Task.Delay(200);
    }
    public async Task DoAsyncTask()
    {
        await Task.Delay(200);
    }

    public async Task<int> DoReturnValueTask()
    {
        await Task.Delay(200);
        return 50;
    }

    public async void CallingTasks()
    {
        DoAsyncVoid(); //This can't use await because it is 'void' so the next line is ran as soon as this command reaches the first 'true awaitable' await.
        await DoAsyncTask(); //This runs before DoAsyncVoid is complete.
        var value = await DoReturnValueTask(); //This waits until 'DoAsyncTask' is complete because it is a Task and awaited.
        await new MessageDialog(value.ToString()).ShowAsync(); //This waits until 'DoReturnValueTask' is complete and value will be 50 in this case.

        //All code here waits until the Dialog is closed because it is also awaited.
    }
}