我正在构建Xamarin.Forms应用程序。
我的观点:
<?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="TestApp.Mobile.Views.MainView">
<Grid>
<Frame x:Name="TheFrame" />
</Grid>
</ContentPage>
然后在App.cs中我想访问TheFrame
。像这样:
MainView mainWindow = new MainView ();
someFunction(mainWindow.TheFrame);
但是!这就是自动生成的MainView.cs的样子:
public partial class MainView : ContentPage {
private Frame MainFrame;
private void InitializeComponent() {
this.LoadFromXaml(typeof(MainView));
MainFrame = this.FindByName<Frame>("MainFrame");
}
}
为什么?如何在App.cs中访问TheFrame
?
答案 0 :(得分:0)
您只能使用Frame
属性从代码隐藏中访问x:Name
。为什么需要从App.cs
访问它?
您可以在Frame
中声明App.cs
,然后将其添加到您的页面,这样就可以从任何地方访问它。