在通用 Windows 平台上导航

Navigation On Universal Windows platform

我正在开发一个 Windows 通用应用程序,它使用 webview 托管一个网络应用程序。 步骤如下。

  1. 创建空白通用 window 应用。创建启动画面。放 启动画面作为起始页。毕竟activity我想 导航具有 Web 视图控件的主页。
  2. 将 url 示例 "http:www.google.come" 设置为 Web 视图的来源。一切正常,但主页需要时间,我希望在加载之前看到相同的启动画面。
  3. 我正在使用的导航代码 this.Frame.Navigate(typeof(主页));

完整源代码

public sealed partial class ExtentedSpash : Page
{
    public ProgressMessage Progress;
    public ExtentedSpash()
    {
        this.InitializeComponent();
        Progress = ProgressMessage.GetMessage();
        DataContext = Progress;
        Window.Current.Activate();
        Loaded += Splash_Loaded;
    }

    private async void Splash_Loaded(object sender, RoutedEventArgs e)
    {
        await Initialize();
        Window.Current.Activate();

        await ClearBrowserCache();
        Window.Current.Activate();

        //Task.WaitAll(TaskList.ToArray());
        await StartApplication();
    }


    public async Task Initialize()
    {
        Progress.ActionMessage = "Initialize the controls";
        await Task.Delay(TimeSpan.FromSeconds(10));
    }
    public async Task ClearBrowserCache()
    {
        Progress.ActionMessage = "Clear Browser Cache";
        await Task.Delay(TimeSpan.FromSeconds(10));
    }

    public async Task StartApplication()
    {
        Progress.ActionMessage = "Loading";
        await Task.Delay(TimeSpan.FromSeconds(10));
        this.Frame.Navigate(typeof(MainPage));
    }

    private void btnMain_Click(object sender, RoutedEventArgs e)
    {

    }
}
public class ProgressMessage : INotifyPropertyChanged
{
    private string statusMessage;

    public string StatusMessage
    {
        get { return statusMessage; }

        set
        {
            statusMessage = value;
            RaiseProperChanged();
        }
    }

    private string actionMessage;

    public string ActionMessage
    {
        get { return actionMessage; }

        set
        {
            actionMessage = value;  
            RaiseProperChanged();
        }
    }
    private bool showProgress;

    public bool ShowProgress
    {
        get { return showProgress; }
        set { showProgress = value;
            RaiseProperChanged();
        }
    }


    public static ProgressMessage GetMessage()
    {

        var msg = new ProgressMessage()
        {
            StatusMessage = "Initializing Application",
            ActionMessage = "One moment please...",
            showProgress = true
        };

        return msg;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaiseProperChanged(
       [CallerMemberName] string caller = "")
    {

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
        }

    }


}

}

我希望 "On Loading" 消息应显示到它完全加载应用程序。

正如我们所讨论的,如果您只想在 WebView 的源未完全加载时覆盖它,您可以这样做:

<Page.Resources>
    <Storyboard x:Key="MyTextSTD" x:Name="MyTextSTD" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="tbbrush" Storyboard.TargetProperty="Color" Duration="0:0:10">
            <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Red" />
            <LinearColorKeyFrame KeyTime="0:0:5" Value="Blue" />
            <LinearColorKeyFrame KeyTime="0:0:10" Value="Purple" />
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <WebView Source="https://msdn.microsoft.com/library/windows/apps/xaml/mt244352.aspx" NavigationCompleted="WebView_NavigationCompleted">
    </WebView>
    <Grid x:Name="loadingGrid" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Visibility="Visible">
        <TextBlock Text="On Loading..." FontSize="50" VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBlock.Foreground>
                <SolidColorBrush x:Name="tbbrush" />
            </TextBlock.Foreground>
        </TextBlock>
    </Grid>
</Grid>

后面的代码:

public MainPage()
{
    this.InitializeComponent();
    MyTextSTD.Begin();
}

private void WebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
    loadingGrid.Visibility = Visibility.Collapsed;
}

这里很简单,我用一个TextBlock和一些彩色动画来显示消息。当 WebView 的源完全加载时,此消息将消失。