C# 通用Windows平台(UWP)等效于获取主应用程序UI

C# 通用Windows平台(UWP)等效于获取主应用程序UI,c#,silverlight,windows-phone-8,visual-studio-2015,windows-10-universal,C#,Silverlight,Windows Phone 8,Visual Studio 2015,Windows 10 Universal,我正在尝试将Windows Phone 8.1 Silverlight应用程序迁移到UWP。获取主应用程序UI时出错 这是windowswindows Phone 8.1 Silverlightcode: Storage storage = ((App.Current.RootVisual as PhoneApplicationFrame).DataContext as Storage); 这就是我为UWP版本所做的: Storage storage = ((Window.Current.Co

我正在尝试将
Windows Phone 8.1 Silverlight
应用程序迁移到
UWP
。获取
主应用程序UI
时出错

这是windows
windows Phone 8.1 Silverlight
code:

Storage storage = ((App.Current.RootVisual as PhoneApplicationFrame).DataContext as Storage);
这就是我为
UWP
版本所做的:

Storage storage = ((Window.Current.Content as Frame).DataContext as Storage);
我得到一个
NullPointerException
。有什么想法吗

提前谢谢

编辑

这是我的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>
第二次编辑

我使用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;
    }

帧的datacontext实际上是
NULL
。这是因为框架将视图包含在另一个内容变量中。这个将包含您的
DataContext

您需要将内容声明为页面。下面是一个例子:

((Window.Current.Content as Frame).Content as Page).DataContext;

如果我理解正确,您想获取活动帧的DataContext吗?是的,正是这样。这是从代码隐藏文件还是单独的类中检索的?不,该应用程序是一个带有webView的简单页面。我刚刚修改了示例。我忘了加些东西。如果仍然没有帮助,请在您的问题中添加额外的信息。比如你在应用程序中使用它的确切位置(例如:MainPage.xaml.cs)以及你需要它做什么。有了这个,我可以改进我的答案。我添加了一个额外的信息。我已经写了我的xaml。应用程序只包含一个名为MainPage的页面。那么您在哪里声明您的DataContext?我没有在XAMLI中看到它。我再次编辑了我的问题,也许现在你明白了为什么我没有在XAML中编写上下文。所以你有一个模型/视图模型作为DataContext。您是在页面的构造器上设置DataContext还是在onLoaded中设置DataContext?请把代码也贴出来。我认为问题在于此,而不是检索数据上下文
((Window.Current.Content as Frame).Content as Page).DataContext;