框架需要在显示页面之前加载网站

时间:2012-08-24 18:38:13

标签: wpf frame

我有以下简单的程序,可以在两个不同的页面之间来回切换帧的上下文。第一页只是一张图片并且加载正常,但第二页是一个框架,其源设置为实时天气雷达。 http://www.wunderground.com/auto/wxmap/IN/Carmel.html。当计时器将我的帧的上下文切换到天气雷达页面时,它只会开始加载并花费前3-4秒加载页面。在我创建页面并将其分配给_nextslide之后,我检查了框架的.Navigated事件几乎触发了,那么为什么我的框架等待直到页面显示为渲染网站?如果我可以显示网站满载,那将是完美的。

XAML:

<Page>
    <Viewbox Stretch="Fill" Margin="15">
        <Frame Height="800" Width="1280" Content="{Binding SlideFrame}"/>
    </Viewbox>
</Page>

C#:

private Page _slideFrame;
private Page _nextFrame;
private DispatcherTimer _slideChangeTimer;
private int currentSlide = 1;

// Property
public Page SlideFrame
{
    get { return _slideFrame; }
    set
    {
        _slideFrame = value;
        NotifyPropertyChanged("SlideFrame");
    }
}

// ViewModel Constructor
public Page1ViewModel()
{
    this.SlideFrame = new Slide1();
    _nextFrame = new Slide2();

    _slideChangeTimer = new DispatcherTimer();
    _slideChangeTimer.Interval = TimeSpan.FromSeconds(8);
    _slideChangeTimer.Tick += new EventHandler(SlideChange_Tick);
    _slideChangeTimer.Start();
}

// 8 second timer event handler
private void SlideChange_Tick(object sender, EventArgs e)
{
    if (currentSlide == 1)
    {
        this.SlideFrame = _nextFrame;
        _nextFrame = new Slide1(false);
        currentSlide = 2;
    }
    else
    {
        this.SlideFrame = _nextFrame;
        _nextFrame = new Slide2();
        currentSlide = 1;
    }
}

1 个答案:

答案 0 :(得分:0)

不使用框架或网页,但它显示了如何在后台获取数据以不阻止计时器中的UI。如果尚未提取最后一个网页,您应该添加逻辑以等待。

<Button Grid.Row="0" Content="AmIhot" Click="onClick" Width="60" HorizontalAlignment="Left"/>
<TextBlock Grid.Row="1" x:Name="hotText" />
<TextBlock Grid.Row="2" x:Name="revolvingText" Text="{Binding Path=RevolvingText, Mode=OneWay}" />

using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;

namespace waste09
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private System.Windows.Threading.DispatcherTimer _slideChangeTimer;

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            RevolvingText = "Starting text";
            _slideChangeTimer = new System.Windows.Threading.DispatcherTimer();
            _slideChangeTimer.Interval = TimeSpan.FromSeconds(8);
            _slideChangeTimer.Tick += new EventHandler(SlideChange_Tick);
            _slideChangeTimer.Start();
        }

        // 8 second timer event handler
        private void SlideChange_Tick(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                // this will NOT block the UI
                Thread.Sleep(4000);  // simulate a long fetch
                RevolvingText = "Fetched text " + Guid.NewGuid();
                NotifyPropertyChanged("RevolvingText");
            });
            RevolvingText = "Fetching data and UI not blocked ";
            NotifyPropertyChanged("RevolvingText");               
        }

        public string RevolvingText { get; private set; }

        private void onClick(object sender, RoutedEventArgs e)
        {
            hotText.Text = "Yes you are hot " + Guid.NewGuid();       
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}