Universal Windows: 如何访问另一个Page中的元素?
Universal Windows: How to access the elements in another Page?
我的 UWP 项目中有 2 个页面。
在接下来的页面中,我宣布一个 WebView 对象。
<Page
x:Class="UniversalMarkdownTestApp.WebViewPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UniversalMarkdownTestApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<WebView Name="WebContent" Grid.Row="0" x:FieldModifier="public"/>
</Grid>
</Page>
现在我想在其他页面访问这个对象。我怎样才能做到这一点?
您可以通过以下代码在其他页面访问此对象:
MainPage mainpage = new MainPage();
var webv = mainpage.WebContent;
但是正如AbsoluteSith所说,这个对象的实例已经创建,如果你想把它添加到新的页面中,你不能直接添加。
您需要使用此对象的 属性 值来创建新对象。请看以下代码:
MainPage mainpage = new MainPage();
var webv = mainpage.WebContent;
WebView wv = new WebView();
wv.Source = webv.Source;
grid.Children.Add(wv);
还有另一种方法可以在其他页面中获取对象。您可以在导航时将对象作为参数传递。见以下代码:
rootFrame.Navigate(typeof(BlankPage),WebContent);
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
WebView wv = new WebView();
wv.Source = ((WebView)e.Parameter).Source;
gd.Children.Add(wv);
}
在这里,我仍然不确定你为什么要这样做。一般来说,我不建议在页面之间获取控件。也许,如果你能告诉我们你的需求,或者你想要达到什么样的效果,也许会有更好的解决方案。
我的 UWP 项目中有 2 个页面。
在接下来的页面中,我宣布一个 WebView 对象。
<Page
x:Class="UniversalMarkdownTestApp.WebViewPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UniversalMarkdownTestApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<WebView Name="WebContent" Grid.Row="0" x:FieldModifier="public"/>
</Grid>
</Page>
现在我想在其他页面访问这个对象。我怎样才能做到这一点?
您可以通过以下代码在其他页面访问此对象:
MainPage mainpage = new MainPage();
var webv = mainpage.WebContent;
但是正如AbsoluteSith所说,这个对象的实例已经创建,如果你想把它添加到新的页面中,你不能直接添加。
您需要使用此对象的 属性 值来创建新对象。请看以下代码:
MainPage mainpage = new MainPage();
var webv = mainpage.WebContent;
WebView wv = new WebView();
wv.Source = webv.Source;
grid.Children.Add(wv);
还有另一种方法可以在其他页面中获取对象。您可以在导航时将对象作为参数传递。见以下代码:
rootFrame.Navigate(typeof(BlankPage),WebContent);
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
WebView wv = new WebView();
wv.Source = ((WebView)e.Parameter).Source;
gd.Children.Add(wv);
}
在这里,我仍然不确定你为什么要这样做。一般来说,我不建议在页面之间获取控件。也许,如果你能告诉我们你的需求,或者你想要达到什么样的效果,也许会有更好的解决方案。