简体   繁体   English

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

[英]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.

相关问题 通用Windows平台(UWP)条形码扫描仪应用程序 - Universal Windows Platform (UWP) barcode scanner application 支持两个主窗口的通用Windows平台(UWP)应用 - Universal Windows Platform (UWP) app supporting two main windows 打印机扩展UI可以使用通用Windows平台(UWP)吗? - Can Printer Extension UI use the Universal Windows Platform (UWP)? 在通用Windows平台(UWP)应用程序中读取USB描述符 - Reading USB Descriptors in a Universal Windows Platform (UWP) application 在通用Windows平台(UWP)应用程序中使用Microsoft.AspNet.Mvc - Use Microsoft.AspNet.Mvc in Universal Windows Platform (UWP) Application 通用Windows平台(UWP)应用程序的C#服务器/客户端应用程序 - C# Server / Client Application for Universal Windows Platform (UWP) apps 通用Windows平台(UWP)缺少属性的反射 - Reflection in universal windows platform (UWP) missing properties 通用Windows平台UWP默认数据存储 - Universal Windows Platform UWP default data stored 如何在Delphi中检测通用Windows平台(UWP) - How to detect Universal Windows Platform (UWP) in Delphi UWP(通用Windows平台)上的套接字通信 - Socket Communication on UWP (Universal Windows Platform)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM