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
C# 如何从其他页面访问列表视图?(Xamarin表单)_C#_Xamarin_Xamarin.forms_Xamarin.android_Xamarin.ios - Fatal编程技术网

C# 如何从其他页面访问列表视图?(Xamarin表单)

C# 如何从其他页面访问列表视图?(Xamarin表单),c#,xamarin,xamarin.forms,xamarin.android,xamarin.ios,C#,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我想从主页修改列表视图的内容。列表视图位于另一个名为LoadResultsPage的页面中。以下是主页中调用函数的按钮: <Button Text="Load Results" Clicked="FetchData"></Button> 以下是调用的函数: public async void FetchData(object sender, System.EventArgs e) { /* string

我想从主页修改列表视图的内容。列表视图位于另一个名为LoadResultsPage的页面中。以下是主页中调用函数的按钮:

<Button Text="Load Results" Clicked="FetchData"></Button>

以下是调用的函数:

public async void FetchData(object sender, System.EventArgs e)
    {
        /* string apiUrl = null;
         if (Device.RuntimePlatform == Device.Android)
             apiUrl = "https://10.0.2.2:5001/api";
         else if (Device.RuntimePlatform == Device.iOS)
             apiUrl = "https://localhost:5001/api";
         else
             throw new Exception();*/
        await Navigation.PushAsync(new LoadResultsPage());
        var httpClient = new HttpClient();
        var response = await httpClient.GetStringAsync("http://localhost:5001/api/Calcs");
        var login = JsonConvert.DeserializeObject<List<Calc>>(response);
        Lista.ItemsSource = login;
    }
public async void FetchData(对象发送方,System.EventArgs e)
{
/*字符串apirl=null;
if(Device.RuntimePlatform==Device.Android)
apiUrl=”https://10.0.2.2:5001/api";
else if(Device.RuntimePlatform==Device.iOS)
apiUrl=”https://localhost:5001/api";
其他的
抛出新异常()*/
等待Navigation.PushAsync(新的LoadResultsPage());
var httpClient=新的httpClient();
var response=wait httpClient.GetStringAsync(“http://localhost:5001/api/Calcs");
var login=JsonConvert.DeserializeObject(响应);
Lista.ItemsSource=登录名;
}
此处无法识别Lista(位于函数底部),错误为:“名称'Lista'在当前上下文中不存在”

最后是页面LoadResultsPage的内容,我要修改的列表视图是:

<?xml version="1.0" encoding="utf-8" ?>
  <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="Calculator.Views.LoadResultsPage">
<Grid  VerticalOptions="FillAndExpand">
    <ListView x:Name="Lista" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell >
                    <StackLayout >
                        <Label Text="{Binding Id}" TextColor="Black"></Label>
                        <Label Text="{Binding Value}"></Label>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>


如何使用变量“login”中的信息填充这两个标签?

名称字段不是公共属性,因此默认情况下无法在页面外访问它

有两个选项可供您提问:

  • 一旦您获得了数据(在您的例子中是“login”),那么只有您导航到LoadResultsPage并将login作为参数传递给LoadResultsPage,并且在LoadResultsPage中,您才能访问“Lista”

  • 在LoadResultsPage中设置公共属性,在获取“登录”后,将“登录”分配给该属性,并在LoadResultsPage中将Lista.ItemSource设置为该属性


  • 请阅读有关MVVM的信息,我的建议将在全球范围内改变您的观念。它被认为不是在页面之间访问ui元素的最佳样式

    无论如何,直接回答您的问题使用一些静态模型在应用程序内全局交换数据

        public class Exchange
        {
            private static Exchange _instance;
            public static Exchange Data
            {
                get
                {
                    if (_instance == null)
                    {
                        _instance = new Exchange();
                    }
                    return _instance;
                }
            }
    
            public string Buffer { get; set; }
    
            public ListView MyListView { get; set; }
    
        }
    
    示例:
    Exchange.Data.Buffer
    可以从任何地方访问

    InitializeComponent()之后的页面构造函数中为您的ListView创建一个设置

    Exchange.Data.MyListView = Lista;
    
    现在最重要的一点是,如果您从另一个页面访问ListView,您必须在UI线程上进行访问。例如:

            Device.BeginInvokeOnMainThread(() =>
            {
                // Update the UI
                Exchange.Data.MyListView.ItemsSource = whatever;
            });
    

    此处建议为LoadResultsPage传递登录数据,然后在出现
    LoadResultsPage
    时显示数据

    例如,修改
    FetchData
    方法如下:

    public async void FetchData(object sender, System.EventArgs e)
    {
        
        var httpClient = new HttpClient();
        var response = await httpClient.GetStringAsync("http://localhost:5001/api/Calcs");
        var login = JsonConvert.DeserializeObject<List<Calc>>(response);
        //Lista.ItemsSource = login;
    
        await Navigation.PushAsync(new LoadResultsPage(login));
    }
    

    为什么不通过构造函数将调用结果传递给
    LoadResultsPage
    ?或者为什么不能调用API本身?
    public partial class DetailPage : ContentPage
    {
        public DetailPage(Model login)
        {
            InitializeComponent();
            Lista.ItemsSource = login;
        }
    }