2011-09-22 47 views
3

我在Silverlight中很新,而且我的應用程序有一些問題。我將應用程序日期保留在數據庫和IsolatedStorage中。Silverlight刷新一個圖像取決於存儲在IsolatedStorage中的變量

我在用戶界面的頂部有一個圖像控件,用戶可以隨時看到應用程序的當前日期。我使用圖像,因爲我創建了一些時尚的圖像來以mm.yyyy格式表示日期。

我設置的炫魅Authentication_LoggedIn()的圖像控制的URI:

//setez luna curenta in isolatedStorage 
    adminUtilizContext.GetSetariParticulare(4, 0, (op) => 
    { 
     foreach (var item in op.Value) 
     { 
      if (IsolatedStorageSettings.ApplicationSettings.Contains("lunaCurenta")) 
       IsolatedStorageSettings.ApplicationSettings["lunaCurenta"] = item.Substring(2); 
      else 
       IsolatedStorageSettings.ApplicationSettings.Add("lunaCurenta", item.Substring(2)); 

      Uri uri; 
      uri = new Uri("/Indeco.SIEF;component/Images/Calendar/"+item.Substring(2)+".png", UriKind.RelativeOrAbsolute); 
      dataLuna.Source = new BitmapImage(uri);      
     } 
    }, null); 

的XAML看起來是這樣的:

<StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">     
     <Image Name="dataLuna" HorizontalAlignment="Right"/>         
    </StackPanel> 

它工作正常,但問題出現在用戶更改時當前日期。在數據庫和IsolatedStorage中正確存儲。我正在更改圖片來源,但直到手動刷新頁面時纔會顯示新圖片。

您能告訴我如何自動執行此操作,而無需手動刷新頁面!

此致敬禮。

用戶更改日期的代碼位於相應用戶界面(CurrentConfigurations.xaml)的ViewModel(CurrentConfigurationViewModel.cs)中。有一個包含月份的組合框,SelectedItemChanged自動更新數據庫和IsolatedStorage.That是我已經把我在之前的評論中看到的代碼放在了一起。今年也有一個NumericUpDown控件,它們的工作原理是相同的。但是現在讓我們來談談本月,然後我會在這一年自己做:D! 再次感謝 A print screen with the change date UI and the code i wrote for updating the Image source for the up-right corner (date) 正如你在這幅圖中看到的,當用戶登錄應用日期是2011年4月(右上),並且在我修改了月份之後,它顯示了舊日期,並且我在Db驗證了隔離存儲看起來沒問題。你可以看到我寫的代碼來更新Image源碼。

+1

+1用於提供代碼/ XAML和良好的細節。 –

回答

2

您可以嘗試設置一個包含圖像的屬性,然後將圖像源綁定到此屬性。你的類需要實現INotifyPropertyChanged接口,然後你可以通知你的Image控件你的Property Changed,它會重新加載Image。 我希望這可以幫助。 :)

如果這是您的代碼,那麼您正在生成一個全新的MainPage並將其設置爲dataLuna ImageSource,而不是原始頁面dataLuna控件。

我很高興我能幫到你。

+0

直接從代碼隱藏設置ImageSource,因爲它們應該像綁定一樣工作(這只是舊式代碼而不是MVVM風格)。看起來問題的原因是別的。 –

+0

用真正的答案(來自你的評論)更新你的問題,我會upvote它(並刪除我自己的答案)。令人驚訝的是,當你看到實際的代碼時它有多簡單:) –

+0

是的,如果你看到代碼並知道它應該做什麼以及它實際做了什麼,糾正它就容易得多。 :) – BigL

1

我的建議是從發生日期變化的頁面/控件中觸發一個事件。在主頁面中,您可以訂閱活動並重新加載圖像。希望這可以幫助。 !

+0

這裏的問題是一個簡單的新手錯誤: - 創建一個頁面的新實例,給一個屬性賦值,然後拋出整個頁面:) –

0

大家好你們,感謝的再次你的興趣我已經解決了我的問題,這樣的:

var mp = ((Application.Current.RootVisual as ContentControl).Content as UserControl).Content as Indeco.SIEF.MainPage; 
     Debug.Assert(mp != null); 

Uri uri; 
uri = new Uri("/Indeco.SIEF;component/Images/Calendar/" + id.ToString() + ".png", UriKind.RelativeOrAbsolute); 
mp.dataLuna.Source = new BitmapImage(uri); 
相關問題