如何在打开UWP应用程序时显示生成的图像?

时间:2016-11-13 16:31:27

标签: uwp

我正在尝试生成分形图像并在加载应用时显示图像。它适用于单击按钮和调整窗口大小等事件,但不适用于首次打开应用程序时。

XAML是:

<Viewbox Grid.Row="1" x:Name="Scenario4ImageContainer"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" PointerPressed="Scenario4ImageContainer_PointerPressed">
    <Image x:Name="Scenario4Image" />
</Viewbox>

代码是:

private async void Page_Loaded(object sender, RoutedEventArgs e)
    {
        generator.Width = (int)imageColumn.ActualWidth; ;
        generator.Height = (int)imageRow.ActualHeight; ;
        generator.InitMap();
        generator.Generate();

        fractalBitmap = await colorer.Color(generator.Map);

        Scenario4Image.Source = fractalBitmap;
        fractalBitmap.Invalidate();

        //This works...
        //Scenario4Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/LockScreenLogo.scale-200.png"));
    } 

1 个答案:

答案 0 :(得分:0)

您是否在等待Windows被激活? 您可以使用TaskCompletionSource公开App.xaml.cs文件中的方法:

private static TaskCompletionSource _waitActivation =new TaskCompletionSource<bool>();

public static Task WaitWindowActivationAsync(){
    return _waitActivation.Task;
}

private void OnLaunched(...){

 ...

 Windows.Current.Activate();

_waitActivation.TrySetResult(true);

}

然后在你的处理程序中,你调用它:

private async void Page_Loaded(object sender, RoutedEventArgs e)
    {
       await App.WaitWindowActivationAsync(); 
       ...
    } 

您还可以向Task.Yield()添加等待; :

private async void Page_Loaded(object sender, RoutedEventArgs e)
    {
       await Task.Yield();
       await App.WaitWindowActivationAsync(); 
       ...
    }