【问题标题】:Prevent local copy of XAML images阻止 XAML 图像的本地副本
【发布时间】:2014-10-13 13:28:17
【问题描述】:

我在 XAML 中设置两个图像元素的来源。我怎样才能让它们指向同一个对象,以便如果其中一个图像的来源发生变化,那么第二个图像会自动更改。目前通过设置 XAML 图像的来源,它们都维护自己的本地副本。 例如,我有两个图像元素

<Image x:Name="abc" /> and <Image x:name="def"/>

我设置 abc.Source = "xyz" 和 def.Source = "xyz"。现在(abc 和 def)都有自己的图像“xyz”副本。我怎样才能让它们指向同一个图像对象。这样就没有必要维护 2 份“xyz”了。

【问题讨论】:

  • 非常感谢代码示例
  • @Maximus 请立即查看
  • 请在您的示例中更加具体。你是如何设置源的?如果你例如从 URI 初始化,然后 WPF 进行内部缓存,实际上只加载一次图像并在所有图像控件之间共享它。因此,也许您认为他们“拥有自己的副本”的假设一开始就是不对的。

标签: c# wpf xaml user-interface wpf-controls


【解决方案1】:

不太确定我是否理解正确。我想您可能希望能够更改一次 ImageSource 并自动更新两个图像,而不是在每次更改时明确设置每张图片的 Source。那将是 Binding 的描述。

这是该案例的一个实现:

视图模型:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Input;
namespace WpfApplication1
{
    public class MyViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private string _source;
        public string Source { get { return _source; } set { _source = value; OnPropertyChanged("Source"); } }

        public ICommand ChangeImageCommand { get; private set; }

        private List<string> _pics = new List<string>()
        {
            "/Themes/Evernote.png",
            "/Themes/Skype.png", 
            "/Themes/Twitter.png"
        };

        private int i = 0;

        public MyViewModel()
        {
            this.Source = _pics[i++];
            this.ChangeImageCommand = new ActionCommand(ChangeImage);
        }

        private void ChangeImage()
        {
            this.Source = _pics[i++ % _pics.Count];
        }
    }

    public class ActionCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;
        private Action _action;
        public ActionCommand(Action action) { _action = action; }
        public bool CanExecute(object parameter) { return true; }
        public void Execute(object parameter) { if (_action != null) _action(); }
    }
}

Xaml:

<Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication1"            
            Title="MainWindow" Height="350" Width="500">

    <Window.DataContext>
        <local:MyViewModel />
    </Window.DataContext>
    <StackPanel Background="Maroon">
        <Image x:Name="Image1" Source="{Binding Source}" Margin="20" Width="100" />
        <Image x:Name="Image2" Source="{Binding Source}" Margin="20" Width="100" />
        <Button Command="{Binding ChangeImageCommand}" Content="Change Image" HorizontalAlignment="Center" />
    </StackPanel>
</Window>

【讨论】:

    【解决方案2】:

    您可以在 ResourceDictionary 中将图像定义为 StaticResource,并在所有视图中重复使用它

    <BitmapImage x:Key="MyImage" UriSource="path/to/MyImage.png" />
    
    <Image x:Name="MyImage1" Source="{StaticResource MyImage}"/>
    <Image x:Name="MyImage2" Source="{StaticResource MyImage}"/>
    

    【讨论】:

    • MyImage1.Source = MyImage2.Source = new BitmapImage(new Uri(...));
    • @Maximus 请再次检查问题。我已经编辑过了。你们都假设错了。
    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 2020-04-11
      • 2014-12-18
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 2021-06-20
      • 2010-12-23
      相关资源
      最近更新 更多