[英]Universal Windows Platform (UWP) equivalent to get main application UI
I am trying to migrate a Windows Phone 8.1 Silverlight
app to UWP
. 我正在尝试将Windows Phone 8.1 Silverlight
应用程序迁移到UWP
。 I have an error getting the Main Application UI
. 获取Main Application UI
出错。
This is the windows Windows Phone 8.1 Silverlight
code: 这是windows Windows Phone 8.1 Silverlight
代码:
Storage storage = ((App.Current.RootVisual as PhoneApplicationFrame).DataContext as Storage);
This is what I've done for the UWP
version: 这就是我为UWP
版本所做的:
Storage storage = ((Window.Current.Content as Frame).DataContext as Storage);
I get a NullPointerException
. 我得到一个NullPointerException
。 Any ideas? 有任何想法吗?
Thanks in advance. 提前致谢。
EDIT 编辑
This is my xaml: 这是我的xaml:
<Page
x:Class="windows_phone.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="20"
Foreground="#FF000000">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0">
<WebView
x:Name="webView"
Margin="0,0,0,0"
Grid.Row="1"
/>
</Grid>
</Grid>
This is my class Store to save the last Uri: 这是我保存最后一个Uri的类商店:
namespace windows
{
public class Storage
{
#region Properties
private Uri _lastUri = null;
public Uri lastUri
{
get {return _lastUri;}
set
{
_lastUri = value;
}
}
#endregion
}
}
SECOND EDIT 第二次编辑
Code where I use Store class: 我使用Store类的代码:
public sealed partial class MainPage : Page
{
private Storage storage = null;
....
protected override void OnNavigatedTo(NavigationEventArgs e)
{
storage = (((Window.Current.Content as Frame).Content as Page).DataContext as Storage);
if (Storage.lastUri != null) webView.Navigate(Storage.lastUri);
base.OnNavigatedTo(e);
}
void webView_LoadCompleted(object sender, NavigationEventArgs e)
{
if (webView.Source.ToString().Contains("login"))
{
string id = LoadUserIdFromIsolatedStorage();
String[] data = new String[1];
data[0] = id;
if (id != null)
webView.InvokeScript("WinHelp", data);
}
progressIndicator.Visibility = Visibility.Collapsed;
Storage.lastUri = e.Uri;
}
The datacontext of the Frame is indeed NULL
. Frame的datacontext确实是NULL
。 This because the frame houses the view inside the another content variable. 这是因为框架容纳了另一个内容变量内的视图。 That one will contain your DataContext
. 那个将包含您的DataContext
。
You will need to declare the content as a page. 您需要将内容声明为页面。 Below here is an example: 下面是一个例子:
((Window.Current.Content as Frame).Content as Page).DataContext;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.