【发布时间】: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