Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示api数据(Xamarin表单)_Xamarin_Xamarin.forms - Fatal编程技术网

显示api数据(Xamarin表单)

显示api数据(Xamarin表单),xamarin,xamarin.forms,Xamarin,Xamarin.forms,我想在屏幕2上看到API数据,而不需要点击按钮。虽然我现在运行时在屏幕1上获得了数据,但我希望在下一个屏幕上看到相同的数据,而无需像在屏幕1中那样单击下一个屏幕上的按钮 这是Screen 2 xaml的代码 <StackLayout> <Label Text="this is second page" x:Name="displaylabel" VerticalOptions="CenterAndExpand"

我想在屏幕2上看到API数据,而不需要点击按钮。虽然我现在运行时在屏幕1上获得了数据,但我希望在下一个屏幕上看到相同的数据,而无需像在屏幕1中那样单击下一个屏幕上的按钮 这是Screen 2 xaml的代码

<StackLayout>

    <Label Text="this is second page"

            x:Name="displaylabel"

        VerticalOptions="CenterAndExpand" 
        HorizontalOptions="CenterAndExpand" />


这是screen 2 xaml.cs的代码

public partial class Data : ContentPage
    {
        private static readonly HttpClient client = new HttpClient();
        // private String data;
        public String show;
        //String responseString;
        public Data(String data)
        {
            InitializeComponent();
            displaylabel.Text = show;

        }
        public async Task GetinfoAsync()
        {

            var responseString = await client.GetStringAsync("https://reqres.in/api/users/2");
            show = responseString;
           // DisplayAlert("text", responseString, "ok");
            displaylabel.Text = show;


        }
        public void view()
        {
            DisplayAlert("show", GetinfoAsync, "OK!");
        }

        private void DisplayAlert(string v1, Func<Task> getinfoAsync, string v2)
        {
            throw new NotImplementedException();
        }
公共部分类数据:ContentPage
{
私有静态只读HttpClient客户端=新HttpClient();
//私有字符串数据;
公共弦乐表演;
//弦乐;
公共数据(字符串数据)
{
初始化组件();
displaylabel.Text=show;
}
公共异步任务GetinfoAsync()
{
var responseString=await client.GetStringAsync(“https://reqres.in/api/users/2");
show=responseString;
//DisplayAlert(“文本”,responseString,“ok”);
displaylabel.Text=show;
}
公共无效视图()
{
DisplayAlert(“show”,GetinfoAsync,“OK!”);
}
私有void DisplayAlert(字符串v1,Func getinfoAsync,字符串v2)
{
抛出新的NotImplementedException();
}

您可以使用ViewModel,将属性绑定到xaml中的标签,并从页面构造函数调用函数进行更新

或者您可以尝试以下方法:

    public Data(String data)
    {
        InitializeComponent();

        Task.Run(async() => await GetinfoAsync());
    }

    public async Task GetinfoAsync()
    {

        var responseString = await 
        client.GetStringAsync("https://reqres.in/api/users/2");
        show = responseString;
       // DisplayAlert("text", responseString, "ok");

       Device.BeginInvokeOnMainThread(()=>{
          displaylabel.Text = show;
       });
    }

我一直在尝试,但没有成功。谢谢!