2012-04-12 77 views
1

我正在爲windows phone開發天氣應用程序。我想利用的其中一個功能是活瓷磚。我有一個後臺代理,當用戶將一個城市固定到起始頁面時運行。 固定之後,它會向互聯網發出呼叫以獲取一些天氣數據。所有這些工作都很好。定製活動磁貼更新問題

現在的問題。 根據返回的天氣數據,我想更新固定到開始屏幕的圖塊。 我有許多不同的.xaml文件(雨,雪,太陽等),代表每個瓷磚。 我首先想到的是,我想:

  1. 每瓦(CityState和溫度)
  2. 設置的兩個屬性創建瓷磚後露出2個屬性。
  3. 將圖塊保存爲IsolatedStorage作爲圖像,然後我可以使用它更新開始屏幕上的圖塊。

這裏是我必須做的,代碼:

var ctl = new Snow(); 
//just some dummy data to test 
    ctl.CityState = "Test, NY"; 
    ctl.Temp = 25; 
    ctl.Measure(new Size(173, 173)); 
    ctl.Arrange(new Rect(0, 0, 173, 173)); 
    var bmp = new WriteableBitmap(173, 173); 
    bmp.Render(ctl, null); 
    bmp.Invalidate(); 
    var iss =IsolatedStorageFile.GetUserStoreForApplication(); 
    var filename = "/Shared/ShellContent/tileTest.jpg"; 
    using (var stm = iss.CreateFile(filename)) 
    { 
    bmp.SaveJpeg(stm, 173, 173, 0, 80); 
    } 
    tile.BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute); 
    var tileToUpdate = ShellTile.ActiveTiles.FirstOrDefault(r => r.NavigationUri == uri); 
    tileToUpdate.Update(tile); 

所以,當這個運行時,它創建一個從XAML文件的新瓷磚和更新的開始屏幕,但溫度和CityState性質 不反映在新瓷磚上。在xaml中,我有2個文本塊綁定到代碼隱藏的屬性。我也使用INotifyPropertyChanged實現了 。 這裏是XAML

<UserControl 
    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" 
    x:Name="Window" 
    x:Class="ezweather.services.tiles.Snow" 
    d:DesignWidth="480" d:DesignHeight="800" Width="173" Height="173" > 

     <Canvas x:Name="Layer_1" Width="173" Height="173" Canvas.Left="0" Canvas.Top="0" > 
     <Rectangle x:Name="Rectangle" Width="173" Height="173" Canvas.Left="0" Canvas.Top="-1.52588e-005" Stretch="Fill" Fill="#FF3F6A8D"/> 
     <TextBlock x:Name="cityState" TextAlignment="Left" FontFamily="Segoe UI Semibold" FontWeight="Bold" FontSize="15" Width="Auto" Height="Auto" Canvas.Left="0" Canvas.Top="0"> 
      <TextBlock.RenderTransform> 
       <TransformGroup> 
        <MatrixTransform Matrix="1.33333,0,0,1.33333,11,139.5"/> 
       </TransformGroup> 
      </TextBlock.RenderTransform> 
      <Run Text="{Binding ElementName=Window, Path=CityState}" Foreground="#FFFFFFFF"/> 
     </TextBlock> 
     <TextBlock x:Name="temp" TextAlignment="Right" FontFamily="Segoe UI Light" FontSize="44" Width="Auto" Height="Auto" Canvas.Left="0" Canvas.Top="0"> 
      <TextBlock.RenderTransform> 
       <TransformGroup> 
        <MatrixTransform Matrix="1.33333,0,0,1.33333,87.57,42.9333"/> 
       </TransformGroup> 
      </TextBlock.RenderTransform> 
      <Run Text="{Binding ElementName=Window, Path=Temp}" Foreground="#FFFFFFFF"/> 
     </TextBlock> 
    </Canvas> 
</UserControl> 

,這裏是代碼隱藏

public partial class Snow : UserControl, INotifyPropertyChanged 
    { 
     public Snow() 
     { 
      // Required to initialize variables 
      InitializeComponent(); 
     } 
     private string _cityState; 
     private int _temp; 
     public string CityState 
     { 
      get { return _cityState; } 
      set 
      { 
       _cityState = value; 
       RaisePropertyChanged("CityState"); 
      } 
     } 
     public int Temp 
     { 
      get { return _temp; } 
      set 
      { 
       _temp = value; 
       RaisePropertyChanged("Temp"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected void RaisePropertyChanged(string property) 
     { 
      if(PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 

運行此代碼時,它實例化正確的XAML文件並將其保存到磁盤。 然後更新開始屏幕上的圖塊,但CityState和Temp數據不顯示。

我不知道爲什麼CityState和Temp數據沒有寫出圖像。 我錯過了什麼?

回答

0

我在這裏看到的主要問題是,在控件實際加載之前,您正試圖呈現圖像。

嘗試處理Control.Loaded事件中的呈現。