【问题标题】:change image source programmatically以编程方式更改图像源
【发布时间】:2012-09-07 18:55:56
【问题描述】:

我不知道如何将图像从磁盘动态加载到 Xaml 图像控件。

<Image x:Name="imgLogo" ....../>

然后在我的 c++ 课程中,我找不到任何我发现的示例。大多数都在c#中。一个烦恼是我一直看到 System.Windows.Media::ImageSource 被使用,但这不在我的框架中。

谁能给我举个例子。我来自 iOS 世界,不习惯 MS 和他们的框架。

谢谢,

【问题讨论】:

    标签: c++ xaml windows-8 microsoft-metro


    【解决方案1】:

    试试这个,它在 C# 中,但应该很容易转换:

    using Windows.UI.Xaml.Media.Imaging;
    
    <Image Margin="5" Source="{Binding BMImage}" Height="100"/>
    
    
    bmImage = new BitmapImage();
    bmImage.UriSource = new Uri(new Uri(
         *your file path*, 
         *your image name*);
    
    
    BitmapImage bmImage;
    public BitmapImage BMImage
    {
        get
        {
            return bmImage;
        }
    }
    

    取自here(我的博客)。

    要更改图像,只需更改 bmImage 的值并调用 NotifyPropertyChanged(()=&gt;BMImage);(假设您有该设置)

    请参阅here,了解 Microsoft 关于如何使用图像的示例。

    请参阅 here 以获取 Windows.UI.Xaml.Media.Imaging 命名空间。

    【讨论】:

    • 谢谢你的例子。我遇到的第一个问题是我无法在框架中处理 BitmapImage。文档说它在 System.Windows.Media.Imaging 下,但我的盒子上没有这样的位置。我输入“Windows::Media::Imaging::BitmapImage”,但它找不到 Imaging 命名空间。知道为什么吗?
    • 我确实了解绑定。我只是遇到了命名空间的问题。
    • 你有没有试过using Windows.UI.Xaml.Media.Imaging;或者C++的任何东西
    • Windows::UI::Xaml::Media::Imaging 我认为 Windows::Media::Imaging 适用于桌面应用程序。
    • MyDogIsBox 是正确的。我在看错误的框架。 BitmpapImage 位于 Xaml 分支下。谢谢!
    猜你喜欢
    • 2011-04-18
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 2014-03-28
    • 2016-10-09
    • 2011-07-24
    • 1970-01-01
    相关资源
    最近更新 更多