gpt4 book ai didi

c# - 将 Silverlight 转换为 MVVM,几个问题

转载 作者:行者123 更新时间:2023-12-03 10:30:08 24 4
gpt4 key购买 nike

我有一个正在转换为 MVVM 的 Silverlight 项目。这是我第一次使用这种模式,我正在努力解决一些问题。

所以基本上我在页面后面的 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);
}

而且我无法弄清楚如何将其连接起来以便能够在模型中使用它?我假设 View 模型开始发挥作用,但没有任何效果。

有什么想法吗?

这是正在进行的 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>

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

最佳答案

如果您想触发命令,这将为您完成工作

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

为了在文本框中获取文件名,您必须将文本框绑定(bind)到 View 模型。这样它就会出现在 UI 上并实现 INotifyPropertyChanged。也看看这个会有帮助 Silverlight MVVM

关于c# - 将 Silverlight 转换为 MVVM,几个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7745454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com