将 Silverlight 转换为 MVVM,几个问题

发布于 2024-12-09 12:42:33 字数 1799 浏览 0 评论 0原文

我有一个 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>

基本上我希望它在用户选择要上传的文件后触发。

I have a Silverlight project I'm converting to MVVM. It's my first time using the pattern and I'm struggling with something.

So basically I had this in the XAML code behind page:

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

And I cannot figure out how to wire it up to be able to use that in the model? I assume the viewmodel comes into play, but nothing is working.

Any thoughts?

Here is the work in progress 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>

Basically I want it to fire after the user selects a file to upload.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神爱温柔 2024-12-16 12:42:33

为你完成工作,

<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();
   }
}

例如在你的 ViewModel

   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

If you want to fire a command this would do the work for you

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

in your code behind Constructor for example

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

and in your ViewModel

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

And the 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();
   }
}

in order to get the filename in your textbox you have to bind the textbox to to the view model. so that it will appear on the UI and also implement INotifyPropertyChanged. Also Look at this it would be helpful Silverlight MVVM

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文