如何从 App.xaml 而不是 App.xaml.cs 设置 MainPage?

最短胡子定理

我想学习如何将 的MainPage属性设置AppMyPageContentPage. App.xaml.cs它可以很容易地完成如下。

public partial class App : Application
{

    public App()
    {
        InitializeComponent();

        MainPage = new MyPage();
    }

}

如何在 XAML 中做同样的事情?

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             mc:Ignorable="d"
             x:Class="MyProject.App">

    <!-- Others have been removed only for simplicity! -->
    <Application.MainPage>
       <!-- I don't know what I have to do here. -->       
    </Application.MainPage>
</Application>
杰森

你需要声明一个本地命名空间

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         mc:Ignorable="d"
         xmlns:local="clr-namespace:MyNamespace"
         x:Class="MyProject.App">


<Application.MainPage>
   <local:MyPage />    
</Application.MainPage>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将参数从 app.xaml.cs 传递到 MainPage

如何在app.xaml.cs的MainPage中使用具有可变appbar枢轴的方法

以Xamarin形式对App.xaml.cs中MainPage的开头进行动画处理

如何将XAML元素作为全局变量绑定到App.XAML.CS?

无法初始化MainPage.xaml.cs中的AudioGraph并无法通过MainPage.xaml.cs上的滑块设置频率

从Xamarin中的MainPage.xaml.cs文件到MainPage.xaml文件获取int变量的值

如何访问Mainpage.xaml.cs中ItemViewModel类中定义的属性

从 app.xaml /wpf 设置图像源

如何在app.xaml.cs文件中动态更改应用程序的主题

如何从 App.xaml.cs 中关闭的窗口获取信息?

如何在xamarin.forms中的app.xaml.cs中处理System.TypeLoadException?

如何处理Main.cs中的App.XAML事件?

类在MainPage.xaml.cs上实例化2次

如何在app.xaml.cs中启动消息框结果,但如何在另一个.xaml.cs页面上检索结果

在App.xaml.cs中的Controltemplate中访问Textblock

App.xaml.cs ctor 在应调用 OnResume() 时调用

新窗口中App.xaml.cs中的引用对象

App.xaml.cs中的Xamarin Forms Update值

在Windows Phone 8上从App.xaml.cs运行任务

如何通过 WPF 中的 App.Current 从主 App.xaml.cs 类中获取非静态字段?

如何在 xaml 中添加命名空间,以便我可以在 MainPage.cs 中使用名为 ViewModel 的类?

如何在Xamarin表单中的MainPage.xaml中将条目的text属性访问到MainActivity.cs?

如何从代码访问位于<phone:PanoramaItem.HeaderTemplate>的DataTemplate标记中的文本框控件-背后(MainPage.xaml.cs)

如何在App.xaml中设置全局自定义字体

如何在app.xaml中将datagrid SelectedIndex设置为-1

如果启动窗口在不同的项目/库中,如何在 App.xaml 中设置 StartupUri?

App.xaml对象引用未设置为对象的实例

以App.xaml样式动态设置颜色值

将 ToolbarItems 添加到 Xamarin.Forms 项目中的 MainPage.xaml.cs