c# - 通用 Windows 平台 (UWP) 相当于获取主应用程序 UI

标签 c# silverlight windows-phone-8 visual-studio-2015 windows-10-universal

我正在尝试将 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>

这是我的类商店,用于保存最后一个 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;
    }

最佳答案

Frame 的数据上下文确实是 NULL。这是因为框架将 View 包含在另一个内容变量中。那一个将包含您的 DataContext

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

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

关于c# - 通用 Windows 平台 (UWP) 相当于获取主应用程序 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36934489/

相关文章:

javascript - 添加特定于 Windows Phone Internet Explorer 的代码

c# - FaceBook C# SDK 6.4版本中CanvasAuthorizeAttribute的替代品是什么

c# - 如何使用 Parallel 调用 Task<T> 列表

silverlight - 在 Silverlight 5 中合并 ResourceDictonaries 中使用样式

silverlight - 检查 Silverlight 迁移到 Moonlight

c# - 处理发送到 REST Web 服务的 JSON 字符串中的 & 字符的正确方法

c# - 为简单的 Person 类实现 iNotifyPropertyChanged 会导致 VisualStudio XAML 设计器崩溃

c# - Entity Framework 上的延迟加载与急切加载性能

c# - {Binding} 的代码隐藏等效项是什么?

c# - 如何在 Windows Phone 8.1 Runtime 中获取连接和运营商信息