我正在使用 Xamarin Forms 构建一个应用程序,但仅限于 Android。当我运行应用程序时,出现此错误“只有创建视图层次结构的原始线程才能触摸其视图”。我的应用程序中有类似的页面,但这是我第一次收到此错误。我知道这是关于 UI 线程的问题,但我无法弄清楚我到底在哪里使用了错误的线程。
这是错误来源的代码:
--视图模型--
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using XamarinApp.Services;
using XamarinApp.Views.Month;
namespace XamarinApp.ViewModels.Month
{
public class HistoryViewModel : BaseViewModel
{
private readonly IMonthService _monthService;
private ObservableCollection<int> years;
private ObservableCollection<int> months;
public int year;
public int month;
public HistoryViewModel(IMonthService monthService)
{
_monthService = monthService;
Years = new ObservableCollection<int>();
Months = new ObservableCollection<int>();
Title= "History";
FindCommand = new Command(async () => await GoToYearHistory());
}
private async …Run Code Online (Sandbox Code Playgroud)