【问题标题】:Position 31:28. MarkupExtension not found for appsNames with xamarin forms app位置 31:28。未找到带有 xamarin 表单应用程序的应用程序名称的 MarkupExtension
【发布时间】:2017-05-19 13:36:30
【问题描述】:

您好,我正在开发一个以 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");
            };

        }
    }
}

任何帮助都会很棒!

提前致谢!!!

【问题讨论】:

  • 如果您完全在后面的代码中创建您的 ListView,那么您为什么还要有一个 XAML 页面呢?此外,Label 没有 ItemsSource 属性,并且您用于 Label 的绑定语法似乎完全无效。
  • @Jason 我有 xaml 页面的原因是,我可以使用卡片样式布局,女巫没有编码在代码中,关于如何让它工作有任何想法吗?感谢您的快速回复! :)
  • 不要在后面的代码中分配内容 - 这只会覆盖 XAML 中的所有内容。不要在后面的代码中创建新的 ListView,而是使用对 XAML ListView 的引用。修复损坏的标签。
  • @Jason 我尝试了你的建议,但我仍然遇到同样的错误,请检查更新的代码,让我知道接下来要尝试什么,感谢您迄今为止的所有帮助! :)
  • 您的标签上仍有无效的 ItemsSource 属性

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

我假设您想在标签中显示来自 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 属性。

【讨论】:

    猜你喜欢
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 2016-01-27
    相关资源
    最近更新 更多