2011-10-12 71 views
1

我有一個Silverlight項目轉換爲MVVM。這是我第一次使用這種模式,並且我正在努力應對某些事情。將Silverlight轉換爲MVVM,幾個問題

所以基本上我在網頁後面的XAML代碼有這樣的:

OpenFileDialog ofd = new OpenFileDialog(); 
      if ((bool)ofd.ShowDialog()) 
      { 
       _fileName = ofd.File.Name; 
       FileStream fs = ofd.File.OpenRead(); 
       fileSize = (double)fs.Length; 
       txtFileName.Text = fileName; 
       index = 0; 
       sendData = 0; 

       byte[] file = new byte[fs.Length]; 
       fs.Read(file, 0, file.Length); 
       //convertToChunks(file); 

       prgUpload.Maximum = fileChunks.Count; 
       prgUpload.Value = 0; 
       //uploadChunks(index); 
      } 

我無法弄清楚如何連接起來,以便能夠使用模型?我認爲視角模型發揮作用,但沒有任何工作。

有什麼想法?

這裏正在進行XAML的工作:

<Grid x:Name="LayoutRoot" Width="475" Height="340"> 
    <Canvas Margin="8,8,0,0" Background="White" Height="320" VerticalAlignment="Top" HorizontalAlignment="Left" Width="475"> 
     <Button Width="75" Canvas.Left="380" Canvas.Top="43" Content="Browse" x:Name="btnBrowse" /> 
     <TextBox Canvas.Left="25" IsReadOnly="True" Canvas.Top="43" TextWrapping="Wrap" Width="350" Text="{Binding Path=FileUploadName}" x:Name="txtFileName" /> 
     <ProgressBar Height="10" Width="350" Canvas.Left="25" Canvas.Top="99" x:Name="prgUpload" /> 

     <my:Label Content="Please select a file to upload" Name="lblError" Canvas.Left="25" Canvas.Top="23" RenderTransformOrigin="0.133,-0.063" Width="220"/> 
     <my:Label x:Name="lblProgress" Canvas.Left="25" Canvas.Top="78" RenderTransformOrigin="0.133,-0.063" Width="220"/> 
    </Canvas> 
</Grid> 

基本上我希望它激發用戶選擇要上傳的文件後。

+0

當你想發射事件?你可以添加你的XAML在你想要連接視圖模型 –

+0

視圖綁定到ViewModels有一個模型。我相信你想通過你的ViewModel生成一個新的模型,並從你的視圖發送數據。 – SQLMason

+0

我瞭解視圖 - > viewmodel - >模型。但我只是沒有得到語法或某些東西來完成這一切。希望有人能夠在每個階段給我一個例子。 – PixelMuse

回答

2

如果你想火的命令,這將做你

<Button Width="75" Canvas.Left="380" Canvas.Top="43" Content="Browse" x:Name="btnBrowse" 
    Command={Binding OpenFileCommand} /> 

的工作在後面的構造你的代碼,例如

partial class MainWindow 
{ 
    public MainWindow() 
    { 
    InitializeComponent(); 
    this.DataContext=new MainViewModel(); 
    } 
} 

,並在您的視圖模型

public ICommand OpenFileCommand { get; set; } 

    public MainViewModel() 
    { 
     OpenFileCommand = new RelayCommand(OpenDialog) { IsEnabled = true }; 

    } 

    private void OpenDialog() 
    { 
     OpenFileDialog ofd = new OpenFileDialog(); 
     if ((bool)ofd.ShowDialog()) 
     { 
      _fileName = ofd.File.Name; 
      FileStream fs = ofd.File.OpenRead(); 
      fileSize = (double)fs.Length; 
      //txtFileName.Text = fileName;// Apply Binding 
      index = 0; 
      sendData = 0; 

      byte[] file = new byte[fs.Length]; 
      fs.Read(file, 0, file.Length); 
      //convertToChunks(file); 

      prgUpload.Maximum = fileChunks.Count; 
      prgUpload.Value = 0; 
      //uploadChunks(index); 
     } 
    } 

和RelayCommand

public class RelayCommand:ICommand 
{ 
    private bool _isEnabled; 
    public bool IsEnabled 
    { 
     get { return _isEnabled; } 
     set 
     { 
      if (value != _isEnabled) 
      { 
       _isEnabled = value; 
       if (CanExecuteChanged != null) 
       { 
        CanExecuteChanged(this, EventArgs.Empty); 
       } 
      } 
     } 
    } 
    private Action _handler; 
    public RelayCommand(Action handler) 
    { 
     _handler = handler; 
    } 


    public bool CanExecute(object parameter) 
    { 
     return IsEnabled; 
    } 

    public event EventHandler CanExecuteChanged; 

    public void Execute(object parameter) 
    { 
     _handler(); 
    } 
} 

爲了在文本框中獲取文件名,您必須將文本框綁定到視圖模型。以便它將出現在UI上並且還實現INotifyPropertyChanged。另外看看這將有幫助Silverlight MVVM

+0

這是最優秀的。感謝您的時間。 – PixelMuse