对于使用C#和Xamarin进行Android开发我还是很陌生。 我有一个问题,我开始使用MasterDetailPage,现在当我更改MasterDetailpage的xml时,我很难编译程序才能成功运行。
我的代码看起来像这样:
XAML
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="NavigationMasterDetail.MainPage">
<MasterDetailPage.Master>
<ContentPage Title="Menu">
<Grid BackgroundColor="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid>
<Image Source="bg.png" Aspect="AspectFill" />
<StackLayout Padding="0,20,0,0" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Image BorderColor="White" Source="profile.png" Aspect="AspectFill" WidthRequest="85" HeightRequest="85" />
<Label Text="Adam Pedley" TextColor="White" FontSize="Large" />
</StackLayout>
</Grid>
<StackLayout Margin="20,20,20,0" Grid.Row="1" Spacing="15">
<ListView x:Name="navigationDrawerList" RowHeight="60" SeparatorVisibility="None" BackgroundColor="#e8e8e8" ItemSelected="OnMenuItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<!-- Main design for our menu items -->
<StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="20,10,0,10" Spacing="20">
<Image Source="{Binding Icon}" WidthRequest="40" HeightRequest="40" VerticalOptions="Center" />
<Label Text="{Binding Title}" FontSize="Medium" VerticalOptions="Center" TextColor="Black" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</Grid>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage></NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
现在,后面的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using xfMasterDetailNavigation.MenuItems;
using xfMasterDetailNavigation.Views;
namespace xfMasterDetailNavigation
{
public partial class MainPage : MasterDetailPage
{
public List<MasterPageItem> menuList
{
get;
set;
}
public MainPage()
{
InitializeComponent();
menuList = new List<MasterPageItem>();
// Adding menu items to menuList and you can define title ,page and icon
menuList.Add(new MasterPageItem()
{
Title = "Home",
Icon = "homeicon.png",
TargetType = typeof(HelloTest)
});
menuList.Add(new MasterPageItem()
{
Title = "Contact",
Icon = "contacticon.png",
TargetType = typeof(Information)
});
menuList.Add(new MasterPageItem()
{
Title = "About",
Icon = "abouticon.png",
TargetType = typeof(AboutMe)
});
// Setting our list to be ItemSource for ListView in MainPage.xaml
navigationDrawerList.ItemsSource = menuList;
// Initial navigation, this can be used for our home page
Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(HelloTest)));
}
private void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (MasterPageItem)e.SelectedItem;
Type page = item.TargetType;
Detail = new NavigationPage((Page)Activator.CreateInstance(page));
IsPresented = false;
}
}
}
现在这是我得到的错误:
1.) InitializeComponent() does not exist in current component
2.) navigationDrawerList does not exist in current component
3.) Property BorderColor was not found in type Image
现在,我从这里https://www.c-sharpcorner.com/article/dynamic-master-detail-page-in-xamarin-forms/开始按照循序渐进的教程
现在请我想知道我在哪里弄错了。我对此很陌生
答案 0 :(得分:0)
我根据教程制作了样本。
1。)当前组件中不存在InitializeComponent()
在我的代码中,它运行良好。也许您的项目无法很好地加载。清理并重建。尝试在设备上运行,如果可以正常运行,请忽略该错误。没关系。
2。)navigationDrawerList在当前组件中不存在
navigationDrawerList已在您的Mainpage列表视图中定义。同样,删除项目的obj和bin文件夹以清理并重建。
3。)在图像类型中找不到属性BorderColor
删除此图像控件的BorderColor属性。此图像不具有BorderColor属性。删除它,一切都会好的。
有关更多详细信息,可以检查Image类:https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.image?view=xamarin-forms
我将代码示例上传到GitHub,您可以从Test1/XamarinDemo
文件夹下载以进行检查。
https://github.com/WendyZang/Test.git