将 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>
基本上我希望它在用户选择要上传的文件后触发。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为你完成工作,
如果你想触发一个命令,这将在你的构造函数后面的代码中
例如在你的 ViewModel
和 RelayCommand
中,为了获取文本框中的文件名,你必须将文本框绑定到视图模型。这样它将出现在 UI 上并实现 INotifyPropertyChanged。另外看看这个,它会很有帮助 Silverlight MVVM
If you want to fire a command this would do the work for you
in your code behind Constructor for example
and in your ViewModel
And the RelayCommand
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