我试图让用户在 View 中加载图像。
我的主要观点:
<UserControl x:Class="TestApplication.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="0.3*" />
</Grid.ColumnDefinitions>
<Image
Grid.Column="0"
Stretch="Uniform"
x:Name="Preview">
<Image.Source>
<BitmapImage UriSource="pack://application:,,,/Resources/placeholder.jpg" />
</Image.Source>
</Image>
<ContentControl
Grid.Column="1"
x:Name="ImageManagement" />
</Grid>
</UserControl>
我的主要 View 模型:
namespace TestApplication.ViewModels
{
using System;
using System.ComponentModel.Composition;
using System.Windows.Controls;
using Caliburn.Micro;
using PropertyChanged;
using TestApplication.Events;
[ImplementPropertyChanged]
[Export(typeof(MainViewModel))]
public class MainViewModel : PropertyChangedBase, IHandle<FileSelectedEvent>
{
private readonly IEventAggregator events;
[ImportingConstructor]
public MainViewModel(IEventAggregator events)
{
this.events = events;
this.events.Subscribe(this);
this.ImageManagement = new ImageManagementViewModel(events);
}
public ImageManagementViewModel ImageManagement { get; set; }
public Image Preview { get; set; }
public void Handle(FileSelectedEvent message)
{
// load the selected image
}
}
}
图像占位符不显示,并且 snoop 甚至看不到它。
此外,当通过实例化
BitmapImage
加载图像时并将其设置为 Preview.Source
在 Handle
方法... Preview
属性为空,如果我先初始化它,它也永远不会显示。PropertyChanged 通过 Fody 处理。
最佳答案
感谢@Will 和@mvermef,我找到了答案。
占位符 URL 不是无效的,但在没有 pack://
的情况下它确实有效。部分。不过,我喜欢它,因为它明确表示它是资源而不是文件。
真正的问题是,Caliburn.Micro 实际上并没有绑定(bind)到控件,而是绑定(bind)到 Source
。属性(property)。切换到这个解决了这个问题:
<Image
Grid.Column="0"
Stretch="Uniform"
Source="{Binding PreviewUrl}" />
和 View 模型:
[ImportingConstructor]
public MainViewModel(IEventAggregator events)
{
this.events = events;
this.events.Subscribe(this);
this.ImageManagement = new ImageManagementViewModel(events);
this.PreviewUrl = "pack://application:,,,/Resources/placeholder.jpg";
}
public String PreviewUrl { get; set; }
public void Handle(FileSelectedEvent message)
{
this.PreviewUrl = message.FilePath;
}
关于c# - 使用 Caliburn.Micro 将 View 模型中的图像加载到 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27300524/