从另一个类调用MainActivity方法时出错

时间:2019-04-29 16:02:53

标签: c# android xamarin

我的MainActivity.cs中有以下api调用-

    public async Task DoSomething()
    {
        var progressDialog = ShowLoading(_instance);

        await Task.Factory.StartNew(() =>
        {
            var objectResponse = string.Empty;
            string _apiUrl = "https://my-web-service-endpoint";

            var request = (HttpWebRequest)WebRequest.Create(_apiUrl);
            request.Method = "GET";

            using (var response = _request.HttpWebResponse(request))
            {
                objectResponse = _request.HttpWebResponseBody(response);
            }

            progressDialog.Dismiss();
        });
    }

我通过点击SliderControl.cs-

中的按钮来调用此方法
    public void loadTestView(View view)
    {
        Button btnDoSomething = view.FindViewById<Button>(Resource.Id.btnDoSomething);
        if (btnDoSomething != null)
        {
            btnDoSomething .Click += async (sender, e) =>
            {
                 await MainActivity.GetInstance().DoSomething();
            };
        }
    }

但是loadTestView中没有调用MainActivity.cs,而是当已使用者滑动到应用程序中的特定视图并在SliderControl.cs中进行处理时被调用,它的设置类似于-< / p>

public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
            {
                var selectedView = Resource.Layout.Locator;

                if (position == 0)
                {
                    selectedView = Resource.Layout.Main;
                }
                if (position == 1)
                {
                    selectedView = Resource.Layout.TestView;
                }

                View view = LayoutInflater.From(container.Context).Inflate(selectedView, container, false);
                container.AddView(view);

                if (position == 0)
                {
                    loadMainView(view);
                }
                else if (position == 1)
                {
                    loadTestView(view);
                }

                return view;
            }

当我在MainActivity.cs中设置不同的按钮单击事件逻辑时,一切工作正常,但是,如上设置按钮单击登录时,遇到错误-

Only the original thread that created a view hierarchy can touch its views. 

奇怪的是,加载程序实际上确实可以看到,但是当我进入await Task.Factory.StartNew(() =>行时,错误就会到达。

我尝试将行改为使用Task.Run,但出现相同的错误,并且在Device.BeginInvokeOnMainThread上找到了一些信息,但都认为这是针对Xamarin Forms的。我该如何使用它?

1 个答案:

答案 0 :(得分:0)

您应该使用UI线程来更新UI /或涉及该UI的任何任务:

Xamarin表单示例:

Device.BeginInvokeOnMainThread (() => {
  label.Text = "Async operation completed";
});

John写了很棒的例子来描述如何使用异步/等待模式。您可能想在这里https://johnthiriet.com/configure-await/#

了解更多信息

更新:对于Xamarin Native-

问题:

new System.Threading.Thread(new System.Threading.ThreadStart(() => {
    lblTest.Text = "updated in thread"; 
    // Doesn't work because you can't modify UILabel on background thread!
})).Start();

解决方案:

new System.Threading.Thread(new System.Threading.ThreadStart(() => {
    InvokeOnMainThread (() => {
        label1.Text = "updated in thread"; 
        // this works! We are invoking in Main thread.
    });
})).Start();
相关问题