[英]How to call a function that's in MainPage.xaml.cs from App.xaml.cs
[英]How to set MainPage from App.xaml instead of App.xaml.cs?
我想了解如何將App
的MainPage
屬性設置為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.