位置31:28。找不到带有xamarin表单app

时间:2017-05-19 13:36:30

标签: c# xaml xamarin xamarin.forms

你好我正在开发一个xamarin表单的应用程序,它显示了设备上所有已安装应用程序的列表,你可以在我的应用程序中创建一个快捷方式来快速启动它。这一切都正常我正在解决的问题是我正在尝试制作一个卡片样式的xaml布局并将应用程序列表绑定到我的xaml当我打开应用程序的这部分时它会崩溃并给我这个错误:

  

位置31:28。找不到appsNames的MarkupExtension

编辑:我试过@Jason建议,但我仍然收到此错误

这是我更新的xaml代码:

<?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="AppName.ListInstalledAppsPage">
    <ListView x:Name="listView" HasUnevenRows="true"  ItemSelected="OnItemSelected" BackgroundColor="#d2d5d7">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <Frame  IsClippedToBounds="True"
         HasShadow="True" BackgroundColor="#d2d5d7">
                        <Frame.OutlineColor>
      <OnPlatform x:TypeArguments="Color"
                  Android="Gray"
                  iOS="Gray"/>
    </Frame.OutlineColor>

<Frame.Margin>
      <OnPlatform x:TypeArguments="Thickness"
                  Android="10" iOS="10"/>
    </Frame.Margin>

                            <Frame.Padding>
      <OnPlatform x:TypeArguments="Thickness"
                  Android="0" iOS="0"/>
    </Frame.Padding>
                                <Frame.Content>
              <Frame Padding="15,15,15,15"   OutlineColor="Gray" BackgroundColor="White">
                <Frame.Content>
 <StackLayout Padding="20,0,0,0"  Orientation="Horizontal">

                    <Label 
                           HorizontalOptions="CenterAndExpand"
                           ItemsSource="{appsNames}"
                           FontFamily="OpenSans-Light"
                           FontSize="24"/>
                  </StackLayout>
                </Frame.Content>
              </Frame>
            </Frame.Content>
          </Frame>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</ContentPage>

这是我更新后的代码:

       using AppName.Data;
using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace AppName
{
    public partial class ListInstalledAppsPage : ContentPage
    {
        public ListInstalledAppsPage()
        {
            InitializeComponent();
            var appsNames = new List<String>();
            PackageInterface pkg = DependencyService.Get<PackageInterface>();
            var apps = pkg.GetInstalledApps();
            foreach (var a in apps)
            {
                appsNames.Add(a.Name);
            }


            listView.ItemSelected += async (object sender, SelectedItemChangedEventArgs e) =>
            {
                if (e.SelectedItem == null)
                {
                    return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
                }
                string app = e.SelectedItem.ToString();
                string action = await DisplayActionSheet("Add to?", "Cancel", null, "Home", "Math", "Science", "Handwriting");
                if (Application.Current.Properties.ContainsKey(action))
                {
                    string appsList = Application.Current.Properties[action] as string;
                    if (!appsList.Contains(app))
                    {
                        if (string.IsNullOrEmpty(appsList))
                        {
                            appsList = app;
                        }
                        else
                        {
                            appsList += ";" + app;
                        }
                        Application.Current.Properties[action] = appsList;
                    }
                }
                else
                {
                    Application.Current.Properties.Add(action, app);
                }
                await Application.Current.SavePropertiesAsync();
                await Navigation.PopAsync();
                MessagingCenter.Send<ListInstalledAppsPage>(this, "Refresh");
            };

        }
    }
}

任何帮助都会很棒!

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

我假设您要在标签中显示来自appsNames的字符串?

首先,您需要将ListView的ItemsSource设置为appsNames。你可以通过绑定或你的代码来做到这一点。

<ListView ItemsSource="{Binding appsNames}" />

这假设您将页面的BindingContext设置为自身。例如在构造函数中,添加:

BindingContext = this;

或者接下来你可以在代码中分配它,如下所示:

listView.ItemsSource = appsNames;

然后这一行,在你的XAML

<Label HorizontalOptions="CenterAndExpand"
       ItemsSource="{appsNames}"
       FontFamily="OpenSans-Light"
       FontSize="24"/>

需要

<Label HorizontalOptions="CenterAndExpand"
       Text="{Binding .}"
       FontFamily="OpenSans-Light"
       FontSize="24"/>

您收到错误的原因是因为appsNames在XAML中没有任何意义,而且Label也没有ItemsSource属性。