我在另一个XAML页面(使用Xamarin.Forms)中包含一个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="MyApp.ItemScreen"
xmlns:views="MyApp.TopMenu">
<ContentPage.Content>
<StackLayout>
<views:TopMenu />
</StackLayout>
</ContentPage.Content>
</ContentPage>
TopMenu C#代码在这里(带有我刚刚所做的更改):
namespace MyApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TopMenu : ContentView
{
public int userID;
public void setUserID(int myUserID)
{
userID = myUserID;
}
public TopMenu () // want to pass in data here
{
InitializeComponent ();
//lbl_Top.Text = myUserID.ToString();
}
ItemScreen C#代码在这里:
namespace MyApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ItemScreen : ContentPage
{
public ItemScreen (string itemID)
{
InitializeComponent ();
var x = new TopMenu();
x.setUserID(28);
}
}
}
如何从ItemScreen将数据传递到TopMenu?
答案 0 :(得分:1)
在XAML中分配名称
<ContentPage.Content>
<StackLayout>
<views:TopMenu x:Name="myMenu" />
</StackLayout>
</ContentPage.Content>
然后在后面的代码中
public ItemScreen (string itemID)
{
InitializeComponent ();
myMenu.setUserID(28);
}