簡體   English   中英

如何從 App.xaml 而不是 App.xaml.cs 設置 MainPage?

[英]How to set MainPage from App.xaml instead of App.xaml.cs?

我想了解如何將AppMainPage屬性設置為MyPage繼承自ContentPage 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>

另一種方法:

<Application <!--remove for simplicity-->                 
             MainPage="{StaticResource myPage}">
    <Application.Resources>
        <ResourceDictionary>
            <local:MyPage x:Key="myPage"/>
        </ResourceDictionary>
    </Application.Resources>
</Application>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM