0

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

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;
    }
}
4

1 回答 1

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));
            }
        }
    }
}
于 2012-08-24T19:42:58.913 回答