我正在尝试将1 ContentPage
.cs绑定(合并)到另一个ContentPage
xaml页面。但我错过了一些东西。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Class1" Title=""
xmlns:local="clr-namespace:namespace;assembly:assembly">
<ContentPage.Content>
<StackLayout>
<StackLayout Orientation="Vertical">
<Label Text="Select Refering Physician" HorizontalOptions="CenterAndExpand"
TextColor="Aqua" FontSize="20">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="Handle_Handle" NumberOfTapsRequired="1" >
</TapGestureRecognizer>
</Label.GestureRecognizers>
</Label>
<ContentView x:Name="overlay"
AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
AbsoluteLayout.LayoutFlags="All"
IsVisible="False"
BackgroundColor="#C0808080"
Padding="10, 0">
<StackLayout Orientation="Vertical"
BackgroundColor="White"
HorizontalOptions="Center"
VerticalOptions="Start"
Margin="0,20,0,0" >
<Label BackgroundColor="Black" FontSize="18" TextColor="White" HorizontalOptions="Fill" Text="Search a user" />
</StackLayout>
</ContentView>
</StackLayout>
<StackLayout>
<local:ListviewPage>
</local:ListviewPage>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
上述文件的cs类
public partial class Class1 : ContentPage
{
ListviewPage objListviewPage;
public Class1()
{
InitializeComponent();
objListviewPage = new ListviewPage(); // I want to bind this page to my xaml file
}
void Handle_Handle(object sender, System.EventArgs e)
{
Navigation.PushAsync(new SearchUser());
}
}
这个课我试图绑定
public class ListviewPage : ContentPage
{
ObservableCollection<ListViewModel> ListItems { get; set; } = new ObservableCollection<ListViewModel> {
new ListViewModel{Text = "Item 1"},
new ListViewModel{Text = "Item 2"},
new ListViewModel{Text = "Item 3"},
new ListViewModel{Text = "Item 4"},
new ListViewModel{Text = "Item 5"},
new ListViewModel{Text = "Item 6"},
new ListViewModel{Text = "Item 7"},
new ListViewModel{Text = "Item 8"},
new ListViewModel{Text = "Item 9"}
};
ListView ListView { get; set; }
public ListviewPage()
{
ListView = new ListView();
ListView.ItemsSource = ListItems;
ListView.ItemTemplate = new DataTemplate(typeof(CustomCell));
ListView.ItemTapped += MenuListView_ItemTapped;
var ListViewLayout = new StackLayout();
ListViewLayout.Padding = new Thickness(10, 20);
ListViewLayout.Children.Add(ListView);
Content = ListViewLayout;
}
void MenuListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
if ((sender as ListView).SelectedItem == null)
return;
(sender as ListView).SelectedItem = null;
var item = e.Item as ListViewModel;
if (item.IsSelected)
item.IsSelected = false;
else
item.IsSelected = true;
}
}
我不想导航我希望它在同一页面中呈现
答案 0 :(得分:2)
我认为您尝试做的事情应该使用单独的视图/控件,可以在多个页面中使用,而不是尝试将页面放在页面内。
public class ListViewControl : StackLayout
{
ObservableCollection<ListViewModel> ListItems { get; set; } = new ObservableCollection<ListViewModel>
{
new ListViewModel{Text = "Item 1"},
new ListViewModel{Text = "Item 2"},
new ListViewModel{Text = "Item 3"},
new ListViewModel{Text = "Item 4"},
new ListViewModel{Text = "Item 5"},
new ListViewModel{Text = "Item 6"},
new ListViewModel{Text = "Item 7"},
new ListViewModel{Text = "Item 8"},
new ListViewModel{Text = "Item 9"}
};
ListView ListView { get; set; }
public ListViewControl()
{
ListView = new ListView();
ListView.ItemsSource = ListItems;
ListView.ItemTemplate = new DataTemplate(typeof(CustomCell));
ListView.ItemTapped += MenuListView_ItemTapped;
//NOTE: this is where you would want to set any bindings,
//either in code or by adding a xaml file to do it in there instead.
Padding = new Thickness(10, 20);
Children.Add(ListView);
}
void MenuListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
if ((sender as ListView).SelectedItem == null)
return;
(sender as ListView).SelectedItem = null;
var item = e.Item as ListViewModel;
if (item.IsSelected)
item.IsSelected = false;
else
item.IsSelected = true;
}
}
然后在RadShedulerDiagnosis
页面中使用该新类,在页面或视图上设置BindingContext,并在xaml <local:ListViewControl/>
中使用新控件。
答案 1 :(得分:0)
尝试创建新的内容页面并将代码复制到该页面。它的简单方法