Silverlight MVVM Prism 和 OpenFileDialog
我目前正在开发 SilverLight 3 应用程序。 我正在使用 MVVM 模式和 Prism。 除了以下项目之外,我一切正常。 根据我的观点之一,我必须使用 OpenFileDialog。 我尝试在 ViewModel 中执行此操作,却发现 SilverLight 的安全模型禁止这样做,因为它只允许由用户启动。 我已将 OpenFileDialog 代码移至视图的代码隐藏中。 但这是我的问题。 虽然我已绑定到设置为 TwoWay 的源,但它没有命中 ViewModel 中属性的设置器。
具有绑定的图像控件示例:
<Image x:Name="imgCard" Height="283" Width="463" Canvas.Left="8" Canvas.Top="8" OpacityMask="White" Source="{Binding Path=CardImage, Mode=TwoWay}"/>
按钮 用户使用:
<Button x:Name="btnUpload" Height="20" Width="122" Canvas.Left="8" Canvas.Top="319" Content="Upload Image" Click="btnUpload_Click" />
单击事件:
private void btnUpload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "PNG Files(*.png)|*.png";
ofd.ShowDialog();
using (Stream stream = ofd.File.OpenRead())
{
BitmapImage image = new BitmapImage();
image.SetSource(stream);
imgCard.Source = image;
}
}
我的 ViewModel 正在实现 INotifyPropertyChanged 并具有以下属性。
BitmapSource CardImage
{
get
{
return _imageSource;
}
set
{
_imageSource = value;
NotifyPropertyChanged("CardImage");
}
}
如果我在 Setter 上设置一个断点。 它永远不会击中它。
I am currently working on a SilverLight 3 Application. I am using MVVM Pattern and Prism. I have everything working except the following item. On one of my views I have to use an OpenFileDialog. I attempted to do this in the ViewModel only to find out the security model of SilverLight prohibits it because it is only allowed to be user initiated. I've since moved the OpenFileDialog code to the code-behind of the View. Here is my problem though. Although I have binding to the source set to TwoWay it is not hitting the setter of the property in my ViewModel.
Example of Image control with binding:
<Image x:Name="imgCard" Height="283" Width="463" Canvas.Left="8" Canvas.Top="8" OpacityMask="White" Source="{Binding Path=CardImage, Mode=TwoWay}"/>
Button Used by user:
<Button x:Name="btnUpload" Height="20" Width="122" Canvas.Left="8" Canvas.Top="319" Content="Upload Image" Click="btnUpload_Click" />
Click Event:
private void btnUpload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "PNG Files(*.png)|*.png";
ofd.ShowDialog();
using (Stream stream = ofd.File.OpenRead())
{
BitmapImage image = new BitmapImage();
image.SetSource(stream);
imgCard.Source = image;
}
}
My ViewModel is implementing the INotifyPropertyChanged and has the following property.
BitmapSource CardImage
{
get
{
return _imageSource;
}
set
{
_imageSource = value;
NotifyPropertyChanged("CardImage");
}
}
If I put a break point on the Setter. It never hits it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至少在 Silverlight 2 中,我认为以下规则可以解释为什么您会看到这种行为。 “如果绑定了依赖属性,并且在代码中将该属性显式设置为一个值,则绑定将被删除。” (来源 )
也许这对于 Silverlight 3 来说已经改变了? 在这种情况下,我没有什么建议。
At least in Silverlight 2, I think the following rule might explain why you are seeing this behavior. "If a Dependency Property is bound and in code the property is set to a value explicitly, the binding is removed." (source)
Maybe this has changed for Silverlight 3? In that case, I have no suggestions.
好吧,这是一个黑客,但它有效。 因为我必须从 UI 触发 OpenFileDialog,所以我可以直接反向连接到 DataContext 来更新属性,而不是更新控件。 这有效并且仍然按照我期望的方式呈现用户界面。
注意:HACK 直到我找到更好的方法。
Ok this is a hack but it works. Because I have to fire the OpenFileDialog from the UI I can instead of updating the control directly reverse tether to the DataContext to update the property. This works and still renders the UI the way I expect.
NOTE: HACK Until I find a better way.