通用 Windows 平台 (UWP) 中获取主应用程序 UI 的等效方法

3

我正在尝试将一个 Windows Phone 8.1 Silverlight 应用程序迁移到 UWP。我在获取 Main Application UI 时遇到了错误。

这是 Windows Windows Phone 8.1 Silverlight 代码:

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类,用于保存最后一个Uri:

 namespace windows
{
public class Storage
{
    #region Properties
    private Uri _lastUri = null;
    public Uri lastUri 
    { 
        get {return _lastUri;} 
        set
        {
            _lastUri = value;
        }
    }
    #endregion
}
}

第二次编辑

我使用 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? - Tom Droste
是的,就是那样。 - IrApp
这是从代码后台文件还是单独的类中检索的? - Tom Droste
不,这个应用程序只是一个带有webView的简单页面。 - IrApp
1个回答

2

Frame的数据上下文确实是NULL。这是因为该框架将视图放在另一个内容变量中。那一个会包含你的DataContext

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

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

我刚刚稍微修改了一下这个例子。我忘记添加了一些东西。如果还是没有帮助,请在你的问题中添加额外的信息。比如你在应用程序中的具体使用位置(例如:MainPage.xaml.cs),以及你需要它做什么。有了这些信息,我可以改进我的答案。 - Tom Droste
我已经添加了额外的信息。我已经写好了我的XAML文件。这个应用程序只包含一个被称为MainPage的页面。 - IrApp
那么你在哪里声明你的DataContext?我在XAML中没有看到它。 - Tom Droste
我添加了使用这个Store类的代码,它与我的8.1应用程序完全相同,并且可以正常工作。 - IrApp
你完全正确!!我添加了数据上下文,现在它像魔法一样运行。现在我想知道为什么在8.1版本中也能工作,但是...没关系!!非常感谢你! - IrApp
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接