2012-09-26 16 views
0

我只想文件大小轉換成字符串格式,如「1 MB」或「2.5 GB」,我稱之爲轉換器從Q.42 library我想,我的XAML代碼可能有錯誤,請幫我弄清楚這一點。沒能獲得文件大小在字符串字節字符串轉換器在C#/ XAML Metro應用

MainPage.xaml中

<Page.Resources> 
    <local:ByteToStringConverter x:Key="BytesToString" /> 
</Page.Resources> 
<Grid> 
    <TextBlock Text="{Binding Path=Size, Converter={StaticResource BytesToString}}"/> 
</Grid> 

MainPage.xaml.cs中

public sealed partial class MainPage : Page 
{ 

    public MainPage() 
    { 
     this.InitializeComponent(); 
    } 

    protected async override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     StorageFile f = await KnownFolders.MusicLibrary.GetFileAsync("video.mp4"); 
     BasicProperties bs = await f.GetBasicPropertiesAsync(); 
     MyClass obj = new MyClass(); 
     obj.Size = bs.Size; 
    } 
} 

public class MyClass : INotifyPropertyChanged 
{ 
    private ulong _Size; 

    public ulong Size 
    { 
     get { return _Size; } 
     set { _Size = value; OnPropertyChanged("Size");} 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

ByteToStringConverter.cs

public class ByteToStringConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     string size = "0 KB"; 

     if (value != null) 
     { 

      double byteCount = 0; 

      byteCount = System.Convert.ToDouble(value); 

      if (byteCount >= 1073741824) 
       size = String.Format("{0:##.##}", byteCount/1073741824) + " GB"; 
      else if (byteCount >= 1048576) 
       size = String.Format("{0:##.##}", byteCount/1048576) + " MB"; 
      else if (byteCount >= 1024) 
       size = String.Format("{0:##.##}", byteCount/1024) + " KB"; 
      else if (byteCount > 0 && byteCount < 1024) 
       size = "1 KB"; //Bytes are unimportant ;)    
     } 

     return size; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     throw new NotImplementedException(); 
    } 
} 

回答

0

[編輯:更新以反映新的代碼]

首先,實施th e INotifyPropertyChangedMyClass中的接口,當Size發生更改時觸發PropertyChanged事件。這將確保綁定更新。

二,設置數據的MyClass實例的頁面的結合,如

<Page.DataContext> 
    <local:MyClass/> 
</Page.DataContext> 

您已表明您已經完成。第三,不是在OnNavigatedTo中創建MyClass的新實例,而是設置MainPage已綁定到的實例的屬性。例如:

protected async override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    StorageFile f = await KnownFolders.MusicLibrary.GetFileAsync("video.mp4"); 
    BasicProperties bs = await f.GetBasicPropertiesAsync(); 

    // Do not create a new instance  
    ((MyClass) DataContext).Size = bs.Size; 
} 

爲什麼? WPF中的綁定是針對特定的對象實例完成的。例如,如果控件A綁定到對象X並且對象X的屬性發生更改,則控件A將被更新。如果控件A綁定到對象X但其他對象Y的值發生更改,則不會通知控件A.

OnNavigatedTo中創建一個新對象創建了一個對象Y.相反,您想要在對象X上設置Size屬性。

要調試此:

  • 嘗試沒有轉換器首先要保證值更新然後添加轉換器。這將把任何轉換器問題與綁定問題分開。
  • 在設置器中放置一個斷點Size。如果被擊中,問題在於綁定。如果沒有,問題是沒有設置Size

非常小方點:使用ulong而不是doubleConvertulong s給出64位的精度,而doubles只給出53位的精度。你可能永遠不會看到以字節爲單位的東西,但它也可以保持它與MainPage中的屬性類型一致。

+0

數據仍然沒有得到與文本塊綁定,我試圖消除器 – Xyroid

+0

什麼是'DataContext'設置?嘗試將其設置爲頁面本身。 – akton

+0

誰是DataContext?我用'this.DataContext = this'在MainPage.xaml.cs中嘗試過,但不工作。 – Xyroid