【问题标题】:"Placeholder does not exist in current context"“当前上下文中不存在占位符”
【发布时间】:2018-11-26 00:26:17
【问题描述】:

我正在尝试在 Xamarin 中制作导航栏。我在 MainPage.xaml 中创建了一个带有占位符 x:name 的上下文视图,当我在 xaml.cs 页面中调用它时,它说“占位符在当前上下文中不存在”请记住,我对 Xamarin 真的很陌生.任何帮助将不胜感激。提前致谢

MainPage.xaml

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="1"/>
            <RowDefinition Height="44"/>
        </Grid.RowDefinitions>
        <ContentView x:Name="PlaceHolder" Grid.Row="0"/>
        <BoxView BackgroundColor="#DCDCDC" Grid.Row="1"/>

MainPage.xaml.cs

public partial class HomePage : ContentPage
    {
        void Icon1_Tapped (object sender, System.EventArgs e)
        {
            var page = new HomePage();
            PlaceHolder.ContentView = page.Content;

        }

    }
}

【问题讨论】:

  • 你的文件叫MainPage,但是类是HomePage?这本身并没有错,但它让我觉得你的课程设置不正确。
  • MainPage.xaml中x:Class的值是多少?
  • 嗨,杰森,感谢您的回复。为了简单起见,我只是写了“MainPage”,为此道歉。一切都在主页中完成。 “PlaceHolder”的两个实例是整个项目中仅有的两个。 “x”也是如此,我没有其他实例。
  • 请发布 XAML 的全部内容及其背后的代码

标签: c# xamarin


【解决方案1】:

您的Page 课程只是缺少对InitializeComponent 的调用。这是对页面部分类的方法调用,它将在内部对 XAML 文件进行一些处理(可以找到更多信息 here)。

public partial class HomePage : ContentPage
{
    public HomePage()
    {
        InitializeComponent();
    } 

    void Icon1_Tapped (object sender, System.EventArgs e)
    {
        var page = new HomePage();
        PlaceHolder.ContentView = page.Content;

    }
}

另外请确保 xaml 文件的类类型设置正确。如果你使用ContentPage,你应该有这样的东西:

<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="YourNamespace.HomePage">

<!-- your content -->

</ContentPage

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2019-08-05
    • 2020-07-31
    • 2021-01-16
    • 1970-01-01
    • 2018-06-07
    相关资源
    最近更新 更多