В моем MasterDetailPage.Detail
у меня есть код по умолчанию, и я хочу внести некоторые изменения, вместо этого есть NavigationPage. Я хочу, чтобы ContentPage создал кнопку для отображения на всех моих страницах, но я не могу вызвать функцию <views:ItemsPage />
и <Button Text="Hello World!"/>
одновременно.
Код по умолчанию выглядит следующим образом:
<MasterDetailPage.Detail>
<NavigationPage>
<NavigationPage.Icon>
<OnPlatform x:TypeArguments="FileImageSource">
<On Platform="iOS" Value="tab_feed.png"/>
</OnPlatform>
</NavigationPage.Icon>
<x:Arguments>
<views:ItemsPage />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
И я хочу что-то вроде этого:
<MasterDetailPage.Detail>
<ContentPage>
<ContentPage.Content>
<Button Text="Hello World!" />
<views:ItemsPage />
</ContentPage.Content>
</ContentPage>
</MasterDetailPage.Detail>
MainPage.xaml.cs
public partial class MainPage : MasterDetailPage
{
SortedDictionary<int, Category> categorias = new SortedDictionary<int, Category>();
Dictionary<int, NavigationPage> MenuPages = new Dictionary<int, NavigationPage>();
private int posicao = 0;
public MainPage()
{
InitializeComponent();
MasterBehavior = MasterBehavior.Popover;
//MenuPages.Add((int)MenuItemType.Browse, (NavigationPage)Detail);
}
public async Task NavigateFromMenu(int id)
{
if (!MenuPages.ContainsKey(id))
{
switch (id)
{
case (int)MenuItemType.Browse:
MenuPages.Add(id, new NavigationPage(categorias[posicao].Page));
break;
case (int)MenuItemType.About:
MenuPages.Add(id, new NavigationPage(new AboutPage()));
break;
}
}
var newPage = MenuPages[id];
if (newPage != null && Detail != newPage)
{
Detail = newPage;
if (Device.RuntimePlatform == Device.Android)
await Task.Delay(100);
IsPresented = false;
}
}
}
ItemPage.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RestaurantManagerUI.Views.ItemsPage"
Title="{Binding Title}"
x:Name="BrowseItemsPage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Add" Clicked="AddItem_Clicked">
<ToolbarItem.Icon>
<OnPlatform x:TypeArguments="FileImageSource">
<On Platform="UWP" Value="add.png"/>
</OnPlatform>
</ToolbarItem.Icon>
</ToolbarItem>
</ContentPage.ToolbarItems>
<StackLayout>
<ListView x:Name="ItemsListView"
ItemsSource="{Binding Items}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10">
<Label Text="{Binding Id}"
LineBreakMode="NoWrap"
Style="{DynamicResource ListItemTextStyle}"
FontSize="16" />
<Label Text="{Binding Name}"
LineBreakMode="NoWrap"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
ItemPage.xaml.cs
public partial class ItemsPage : ContentPage
{
Dictionary<int, Models.Category> _categorias;
ItemsViewModel viewModel;
public ItemsPage()
{
InitializeComponent();
BindingContext = viewModel = new ItemsViewModel();
}
public ItemsPage(Dictionary<int, Models.Category> categorias)
{
InitializeComponent();
_categorias = categorias;
BindingContext = viewModel = new ItemsViewModel();
}
async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
{
var item = args.SelectedItem as Models.Category;
if (item == null)
return;
var selected = _categorias.GetOrAdd(item.Id, item);
}
async void AddItem_Clicked(object sender, EventArgs e)
{
await Navigation.PushModalAsync(new NavigationPage(new NewItemPage()));
}
protected override void OnAppearing()
{
base.OnAppearing();
if (viewModel.Items.Count == 0)
viewModel.LoadItemsCommand.Execute(null);
}
}
public static class DictionaryGetOrAdd
{
public static V GetOrAdd<K, V>(this Dictionary<K, V> dict, K key, V value)
{
if (!dict.ContainsKey(key))
dict.Add(key, value);
return value;
}
}
Но в этом последнем коде я получаю две ошибки: «Свойство« Содержимое »установлено более одного раза» и «Свойство« Содержимое »не поддерживает значения типа« ItemsPage »» ».
Как мне это сделать?